5 use base qw(Role::Tiny);
7 require Moo::sification;
9 BEGIN { *INFO = \%Role::Tiny::INFO }
13 sub _install_tracked {
14 my ($target, $name, $code) = @_;
15 $INFO{$target}{exports}{$name} = $code;
16 _install_coderef "${target}::${name}" => "Moo::Role::${name}" => $code;
23 return if $INFO{$target}; # already exported into this package
25 # get symbol table reference
26 my $stash = do { no strict 'refs'; \%{"${target}::"} };
27 _install_tracked $target => has => sub {
28 my ($name, %spec) = @_;
29 ($INFO{$target}{accessor_maker} ||= do {
30 require Method::Generate::Accessor;
31 Method::Generate::Accessor->new
32 })->generate_method($target, $name, \%spec);
33 push @{$INFO{$target}{attributes}||=[]}, $name, \%spec;
34 $me->_maybe_reset_handlemoose($target);
36 # install before/after/around subs
37 foreach my $type (qw(before after around)) {
38 _install_tracked $target => $type => sub {
39 require Class::Method::Modifiers;
40 push @{$INFO{$target}{modifiers}||=[]}, [ $type => @_ ];
41 $me->_maybe_reset_handlemoose($target);
44 _install_tracked $target => requires => sub {
45 push @{$INFO{$target}{requires}||=[]}, @_;
46 $me->_maybe_reset_handlemoose($target);
48 _install_tracked $target => with => sub {
49 $me->apply_roles_to_package($target, @_);
50 $me->_maybe_reset_handlemoose($target);
52 # grab all *non-constant* (stash slot is not a scalarref) subs present
53 # in the symbol table and store their refaddrs (no need to forcibly
54 # inflate constant subs into real subs) - also add '' to here (this
55 # is used later) with a map to the coderefs in case of copying or re-use
56 my @not_methods = ('', map { *$_{CODE}||() } grep !ref($_), values %$stash);
57 @{$INFO{$target}{not_methods}={}}{@not_methods} = @not_methods;
59 $Role::Tiny::APPLIED_TO{$target} = { $target => undef };
61 if ($INC{'Moo/HandleMoose.pm'}) {
62 Moo::HandleMoose::inject_fake_metaclass_for($target);
68 _unimport_coderefs($target, $INFO{$target});
71 sub _maybe_reset_handlemoose {
72 my ($class, $target) = @_;
73 if ($INC{"Moo/HandleMoose.pm"}) {
74 Moo::HandleMoose::maybe_reinject_fake_metaclass_for($target);
78 sub _inhale_if_moose {
79 my ($self, $role) = @_;
85 and $meta = Class::MOP::class_of($role)
89 and $meta = Mouse::Util::find_meta($role)
92 $INFO{$role}{methods} = {
93 map +($_ => $role->can($_)),
94 grep !$meta->get_method($_)->isa('Class::MOP::Method::Meta'),
95 $meta->get_method_list
97 $Role::Tiny::APPLIED_TO{$role} = {
98 map +($_->name => 1), $meta->calculate_all_roles
100 $INFO{$role}{requires} = [ $meta->get_required_method_list ];
101 $INFO{$role}{attributes} = [
102 map +($_ => $meta->get_attribute($_)), $meta->get_attribute_list
104 my $mods = $INFO{$role}{modifiers} = [];
105 foreach my $type (qw(before after around)) {
106 # Mouse pokes its own internals so we have to fall back to doing
107 # the same thing in the absence of the Moose API method
108 my $map = $meta->${\(
109 $meta->can("get_${type}_method_modifiers_map")
110 or sub { shift->{"${type}_method_modifiers"} }
112 foreach my $method (keys %$map) {
113 foreach my $mod (@{$map->{$method}}) {
114 push @$mods, [ $type => $method => $mod ];
118 require Class::Method::Modifiers if @$mods;
119 $INFO{$role}{inhaled_from_moose} = 1;
123 sub _maybe_make_accessors {
124 my ($self, $role, $target) = @_;
126 if ($INFO{$role}{inhaled_from_moose}
128 and $m = Moo->_accessor_maker_for($target)
129 and ref($m) ne 'Method::Generate::Accessor') {
130 $self->_make_accessors($role, $target);
134 sub _make_accessors_if_moose {
135 my ($self, $role, $target) = @_;
136 if ($INFO{$role}{inhaled_from_moose}) {
137 $self->_make_accessors($role, $target);
141 sub _make_accessors {
142 my ($self, $role, $target) = @_;
143 my $acc_gen = ($Moo::MAKERS{$target}{accessor} ||= do {
144 require Method::Generate::Accessor;
145 Method::Generate::Accessor->new
147 my $con_gen = $Moo::MAKERS{$target}{constructor};
148 my @attrs = @{$INFO{$role}{attributes}||[]};
149 while (my ($name, $spec) = splice @attrs, 0, 2) {
150 # needed to ensure we got an index for an arrayref based generator
152 $spec = $con_gen->all_attribute_specs->{$name};
154 $acc_gen->generate_method($target, $name, $spec);
158 sub apply_roles_to_package {
159 my ($me, $to, @roles) = @_;
160 $me->_inhale_if_moose($_) for @roles;
161 $me->SUPER::apply_roles_to_package($to, @roles);
164 sub apply_single_role_to_package {
165 my ($me, $to, $role) = @_;
166 $me->_inhale_if_moose($role);
167 $me->_handle_constructor($to, $INFO{$role}{attributes});
168 $me->_maybe_make_accessors($role, $to);
169 $me->SUPER::apply_single_role_to_package($to, $role);
172 sub create_class_with_roles {
173 my ($me, $superclass, @roles) = @_;
176 '__WITH__', $superclass, my $compose_name = join '__AND__', @roles
179 return $new_name if $Role::Tiny::COMPOSED{class}{$new_name};
181 $me->_inhale_if_moose($_) for @roles;
185 and $m = Moo->_accessor_maker_for($superclass)
186 and ref($m) ne 'Method::Generate::Accessor') {
187 # old fashioned way time.
188 *{_getglob("${new_name}::ISA")} = [ $superclass ];
189 $me->apply_roles_to_package($new_name, @roles);
195 $me->SUPER::create_class_with_roles($superclass, @roles);
197 foreach my $role (@roles) {
198 die "${role} is not a Role::Tiny" unless my $info = $INFO{$role};
201 $Moo::MAKERS{$new_name} = {};
203 $me->_handle_constructor(
204 $new_name, [ map @{$INFO{$_}{attributes}||[]}, @roles ], $superclass
210 sub _composable_package_for {
211 my ($self, $role) = @_;
212 my $composed_name = 'Role::Tiny::_COMPOSABLE::'.$role;
213 return $composed_name if $Role::Tiny::COMPOSED{role}{$composed_name};
214 $self->_make_accessors_if_moose($role, $composed_name);
215 $self->SUPER::_composable_package_for($role);
218 sub _install_single_modifier {
219 my ($me, @args) = @_;
220 _install_modifier(@args);
223 sub _handle_constructor {
224 my ($me, $to, $attr_info, $superclass) = @_;
225 return unless $attr_info && @$attr_info;
227 push @{$INFO{$to}{attributes}||=[]}, @$attr_info;
229 # only fiddle with the constructor if the target is a Moo class
231 and my $con = Moo->_constructor_maker_for($to, $superclass)) {
232 # shallow copy of the specs since the constructor will assign an index
233 $con->register_attribute_specs(map ref() ? { %$_ } : $_, @$attr_info);
242 Moo::Role - Minimal Object Orientation support for Roles
266 # bar gets imported, but not foo
275 C<Moo::Role> builds upon L<Role::Tiny>, so look there for most of the
276 documentation on how this works. The main addition here is extra bits to make
277 the roles more "Moosey;" which is to say, it adds L</has>.
279 =head1 IMPORTED SUBROUTINES
281 See L<Role::Tiny/IMPORTED SUBROUTINES> for all the other subroutines that are
282 imported by this module.
290 Declares an attribute for the class to be composed into. See
291 L<Moo/has> for all options.
295 See L<Moo> for authors.
297 =head1 COPYRIGHT AND LICENSE
299 See L<Moo> for the copyright and license.