| 1 | use strict; | ||
| 2 | use warnings; | ||
| 3 | |||
| 4 | package Role::Tiny::MonkeyPatch; | ||
| 5 | |||
| 6 | # ABSTRACT: monkey patch roles into existing classes | ||
| 7 | |||
| 8 | use Role::Tiny; | ||
| 9 | use Sub::Util qw(set_subname); | ||
| 10 | use Carp; | ||
| 11 | |||
| 12 | sub monkey_patch { | ||
| 13 | # ----------------------------------------- | ||
| 14 | # shameless copy paste from Mojo::Util | ||
| 15 | # ----------------------------------------- | ||
| 16 | my ($class, %patch) = @_; | ||
| 17 | no strict 'refs'; | ||
| 18 | no warnings 'redefine'; | ||
| 19 | *{"${class}::$_"} = set_subname("${class}::$_", $patch{$_}) for keys %patch; | ||
| 20 | } | ||
| 21 | |||
| 22 | sub monkey_patch_roles { | ||
| 23 | my $package = shift; | ||
| 24 | |||
| 25 | my $class = ref $package || $package || ""; | ||
| 26 | |||
| 27 | # print "$class\n"; | ||
| 28 | |||
| 29 | croak (sprintf "%s does not look like a correct class name", ($class)) | ||
| 30 | if ($class) !~ /^\w(?:[\w:']*\w)?$/; | ||
| 31 | |||
| 32 | croak (sprintf "Could not load class %s", ($class)) | ||
| 33 | unless ($class->can('new') || eval "require $class; 1"); | ||
| 34 | |||
| 35 | if ($class->can('with_roles')) { | ||
| 36 | warn (sprintf "Class %s already has roles", ($class)); | ||
| 37 | return; | ||
| 38 | } | ||
| 39 | monkey_patch $package, "with_roles", sub { with_roles(@_) }; | ||
| 40 | } | ||
| 41 | |||
| 42 | sub with_roles { | ||
| 43 | my $class = shift; | ||
| 44 | |||
| 45 | my $p = ((ref $class) || $class) . "::Role::"; | ||
| 46 | |||
| 47 | my @roles = map { s/^\+/$p/r } @_; | ||
| 48 | |||
| 49 | if (ref $class) { | ||
| 50 | # print STDERR "object"; | ||
| 51 | Role::Tiny->apply_roles_to_object($class, @roles); | ||
| 52 | return $class | ||
| 53 | } else { | ||
| 54 | # print STDERR "class"; | ||
| 55 | Role::Tiny->apply_roles_to_package($class, @roles); | ||
| 56 | return $class | ||
| 57 | } | ||
| 58 | } | ||
| 59 | |||
| 60 | sub import { | ||
| 61 | my ($caller) = caller(); | ||
| 62 | no strict "refs"; | ||
| 63 | *{$caller . "::monkey_patch_roles"} = *monkey_patch_roles; | ||
| 64 | # print STDERR "$caller\n"; | ||
| 65 | use strict "refs"; | ||
| 66 | my ($class, @packages) = @_; | ||
| 67 | for my $package (@packages) { | ||
| 68 | monkey_patch_roles($package) | ||
| 69 | } | ||
| 70 | } | ||
| 71 | |||
| 72 | 1; | ||
| 73 | |||
| 74 | =pod | ||
| 75 | |||
| 76 | =head1 Role::Tiny::MonkeyPatch | ||
| 77 | |||
| 78 | =head2 NAME | ||
| 79 | |||
| 80 | Role::Tiny::MonkeyPatch - monkeypatch roles into existing classes | ||
| 81 | |||
| 82 | =head2 SYNOPSIS | ||
| 83 | |||
| 84 | use Role::Tiny::MonkeyPatch qw/Some::Existing::Class/; | ||
| 85 | |||
| 86 | my $something = Some::Existing::Class->new->withroles('+Some::Role'); | ||
| 87 | |||
| 88 | $something->some_method_provided_by_role | ||
| 89 | |||
| 90 | or | ||
| 91 | |||
| 92 | use Role::Tiny::MonkeyPatch; | ||
| 93 | |||
| 94 | my $something = Some::Existing::Class->new; | ||
| 95 | |||
| 96 | monkey_patch_roles($something); | ||
| 97 | |||
| 98 | $something->some_method_provided_by_role | ||
| 99 | |||
| 100 | or | ||
| 101 | |||
| 102 | use Role::Tiny::MonkeyPatch; | ||
| 103 | |||
| 104 | monkey_patch_roles(Some::Existing::Class); | ||
| 105 | |||
| 106 | my $something = Some::Existing::Class->new; | ||
| 107 | |||
| 108 | $something->some_method_provided_by_role | ||
| 109 | |||
| 110 | |||
| 111 | =head2 DESCRIPTION | ||
| 112 | |||
| 113 | Role::Tiny::MonkeyPatch monkey patches Role::Tiny-based roles onto existing modules | ||
| 114 | |||
| 115 | =head2 FUNCTIONS | ||
| 116 | |||
| 117 | Role::Tiny::MonkeyPatch exports only one function | ||
| 118 | |||
| 119 | =head3 monkey_patch_roles | ||
| 120 | |||
| 121 | monkey_patch_roles(Some::Class); | ||
| 122 | |||
| 123 | or | ||
| 124 | |||
| 125 | my $obj = Some::Class->new; | ||
| 126 | monkey_patch_roles($obj); | ||
| 127 | |||
| 128 | monkey patch roles onto an existing class or object | ||
| 129 | |||
| 130 | |||
| 131 | |||
| 132 | =head2 CAVEATS AND NOTES | ||
| 133 | |||
| 134 | Be aware that Mojo::Base roles only work on hash-based classes | ||
| 135 | |||
| 136 | =head2 SEE ALSO | ||
| 137 | |||
| 138 | =over | ||
| 139 | |||
| 140 | =item Role::Tiny | ||
| 141 | |||
| 142 | =item Mojo::Util | ||
| 143 | |||
| 144 | =back | ||
| 145 | |||
| 146 | =head2 AUTHORS | ||
| 147 | |||
| 148 | Simone Cesano | ||
| 149 | |||
| 150 | =head2 COPYRIGHT AND LICENSE | ||
| 151 | |||
| 152 | This software is copyright (c) 2023 by Simone Cesano. | ||
| 153 | |||
| 154 | This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. |