6 use base qw(Role::Tiny);
8 require Moo::sification;
10 BEGIN { *INFO = \%Role::Tiny::INFO }
14 sub _install_tracked {
15 my ($target, $name, $code) = @_;
16 $INFO{$target}{exports}{$name} = $code;
17 _install_coderef "${target}::${name}" => "Moo::Role::${name}" => $code;
24 if ($Moo::MAKERS{$target} and $Moo::MAKERS{$target}{is_class}) {
25 die "Cannot import Moo::Role into a Moo class";
27 $INFO{$target} ||= {};
28 # get symbol table reference
29 my $stash = do { no strict 'refs'; \%{"${target}::"} };
30 _install_tracked $target => has => sub {
31 my ($name_proto, %spec) = @_;
32 my $name_isref = ref $name_proto eq 'ARRAY';
33 foreach my $name ($name_isref ? @$name_proto : $name_proto) {
34 my $spec_ref = $name_isref ? +{%spec} : \%spec;
35 ($INFO{$target}{accessor_maker} ||= do {
36 require Method::Generate::Accessor;
37 Method::Generate::Accessor->new
38 })->generate_method($target, $name, $spec_ref);
39 push @{$INFO{$target}{attributes}||=[]}, $name, $spec_ref;
40 $me->_maybe_reset_handlemoose($target);
43 # install before/after/around subs
44 foreach my $type (qw(before after around)) {
45 _install_tracked $target => $type => sub {
46 require Class::Method::Modifiers;
47 push @{$INFO{$target}{modifiers}||=[]}, [ $type => @_ ];
48 $me->_maybe_reset_handlemoose($target);
51 _install_tracked $target => requires => sub {
52 push @{$INFO{$target}{requires}||=[]}, @_;
53 $me->_maybe_reset_handlemoose($target);
55 _install_tracked $target => with => sub {
56 $me->apply_roles_to_package($target, @_);
57 $me->_maybe_reset_handlemoose($target);
59 return if $INFO{$target}{is_role}; # already exported into this package
60 $INFO{$target}{is_role} = 1;
61 *{_getglob("${target}::meta")} = $me->can('meta');
62 # grab all *non-constant* (stash slot is not a scalarref) subs present
63 # in the symbol table and store their refaddrs (no need to forcibly
64 # inflate constant subs into real subs) - also add '' to here (this
65 # is used later) with a map to the coderefs in case of copying or re-use
66 my @not_methods = ('', map { *$_{CODE}||() } grep !ref($_), values %$stash);
67 @{$INFO{$target}{not_methods}={}}{@not_methods} = @not_methods;
69 $Role::Tiny::APPLIED_TO{$target} = { $target => undef };
71 if ($INC{'Moo/HandleMoose.pm'}) {
72 Moo::HandleMoose::inject_fake_metaclass_for($target);
76 # duplicate from Moo::Object
78 require Moo::HandleMoose::FakeMetaClass;
79 my $class = ref($_[0])||$_[0];
80 bless({ name => $class }, 'Moo::HandleMoose::FakeMetaClass');
85 _unimport_coderefs($target, $INFO{$target});
88 sub _maybe_reset_handlemoose {
89 my ($class, $target) = @_;
90 if ($INC{"Moo/HandleMoose.pm"}) {
91 Moo::HandleMoose::maybe_reinject_fake_metaclass_for($target);
95 sub _inhale_if_moose {
96 my ($self, $role) = @_;
102 and $meta = Class::MOP::class_of($role)
103 and $meta->isa('Moose::Meta::Role')
106 Mouse::Util->can('find_meta')
107 and $meta = Mouse::Util::find_meta($role)
108 and $meta->isa('Mouse::Meta::Role')
111 $INFO{$role}{methods} = {
112 map +($_ => $role->can($_)),
113 grep !$meta->get_method($_)->isa('Class::MOP::Method::Meta'),
114 $meta->get_method_list
116 $Role::Tiny::APPLIED_TO{$role} = {
117 map +($_->name => 1), $meta->calculate_all_roles
119 $INFO{$role}{requires} = [ $meta->get_required_method_list ];
120 $INFO{$role}{attributes} = [
122 my $spec = { %{$meta->get_attribute($_)} };
126 my $get_constraint = do {
127 my $pkg = $meta->isa('Mouse::Meta::Role')
128 ? 'Mouse::Util::TypeConstraints'
129 : 'Moose::Util::TypeConstraints';
131 $pkg->can('find_or_create_isa_type_constraint');
134 my $tc = $get_constraint->($spec->{isa});
135 my $check = $tc->_compiled_type_constraint;
138 &$check or die "Type constraint failed for $_[0]"
141 if ($spec->{coerce}) {
143 # Mouse has _compiled_type_coercion straight on the TC object
144 $spec->{coerce} = $tc->${\(
145 $tc->can('coercion')||sub { $_[0] }
146 )}->_compiled_type_coercion;
150 }), $meta->get_attribute_list
152 my $mods = $INFO{$role}{modifiers} = [];
153 foreach my $type (qw(before after around)) {
154 # Mouse pokes its own internals so we have to fall back to doing
155 # the same thing in the absence of the Moose API method
156 my $map = $meta->${\(
157 $meta->can("get_${type}_method_modifiers_map")
158 or sub { shift->{"${type}_method_modifiers"} }
160 foreach my $method (keys %$map) {
161 foreach my $mod (@{$map->{$method}}) {
162 push @$mods, [ $type => $method => $mod ];
166 require Class::Method::Modifiers if @$mods;
167 $INFO{$role}{inhaled_from_moose} = 1;
171 sub _maybe_make_accessors {
172 my ($self, $role, $target) = @_;
174 if ($INFO{$role}{inhaled_from_moose}
176 and $m = Moo->_accessor_maker_for($target)
177 and ref($m) ne 'Method::Generate::Accessor') {
178 $self->_make_accessors($role, $target);
182 sub _make_accessors_if_moose {
183 my ($self, $role, $target) = @_;
184 if ($INFO{$role}{inhaled_from_moose}) {
185 $self->_make_accessors($role, $target);
189 sub _make_accessors {
190 my ($self, $role, $target) = @_;
191 my $acc_gen = ($Moo::MAKERS{$target}{accessor} ||= do {
192 require Method::Generate::Accessor;
193 Method::Generate::Accessor->new
195 my $con_gen = $Moo::MAKERS{$target}{constructor};
196 my @attrs = @{$INFO{$role}{attributes}||[]};
197 while (my ($name, $spec) = splice @attrs, 0, 2) {
198 # needed to ensure we got an index for an arrayref based generator
200 $spec = $con_gen->all_attribute_specs->{$name};
202 $acc_gen->generate_method($target, $name, $spec);
206 sub apply_roles_to_package {
207 my ($me, $to, @roles) = @_;
208 foreach my $role (@roles) {
209 $me->_inhale_if_moose($role);
211 $me->SUPER::apply_roles_to_package($to, @roles);
214 sub apply_single_role_to_package {
215 my ($me, $to, $role) = @_;
216 die "${role} is not a Moo::Role" unless my $info = $INFO{$role};
217 $me->_inhale_if_moose($role);
218 $me->_handle_constructor($to, $INFO{$role}{attributes});
219 $me->_maybe_make_accessors($role, $to);
220 $me->SUPER::apply_single_role_to_package($to, $role);
223 sub create_class_with_roles {
224 my ($me, $superclass, @roles) = @_;
227 '__WITH__', $superclass, my $compose_name = join '__AND__', @roles
230 return $new_name if $Role::Tiny::COMPOSED{class}{$new_name};
232 foreach my $role (@roles) {
233 $me->_inhale_if_moose($role);
238 and $m = Moo->_accessor_maker_for($superclass)
239 and ref($m) ne 'Method::Generate::Accessor') {
240 # old fashioned way time.
241 *{_getglob("${new_name}::ISA")} = [ $superclass ];
242 $me->apply_roles_to_package($new_name, @roles);
248 $me->SUPER::create_class_with_roles($superclass, @roles);
250 foreach my $role (@roles) {
251 die "${role} is not a Role::Tiny" unless my $info = $INFO{$role};
254 $Moo::MAKERS{$new_name} = {};
256 $me->_handle_constructor(
257 $new_name, [ map @{$INFO{$_}{attributes}||[]}, @roles ], $superclass
263 sub _composable_package_for {
264 my ($self, $role) = @_;
265 my $composed_name = 'Role::Tiny::_COMPOSABLE::'.$role;
266 return $composed_name if $Role::Tiny::COMPOSED{role}{$composed_name};
267 $self->_make_accessors_if_moose($role, $composed_name);
268 $self->SUPER::_composable_package_for($role);
271 sub _install_single_modifier {
272 my ($me, @args) = @_;
273 _install_modifier(@args);
276 sub _handle_constructor {
277 my ($me, $to, $attr_info, $superclass) = @_;
278 return unless $attr_info && @$attr_info;
280 push @{$INFO{$to}{attributes}||=[]}, @$attr_info;
282 # only fiddle with the constructor if the target is a Moo class
284 and my $con = Moo->_constructor_maker_for($to, $superclass)) {
285 # shallow copy of the specs since the constructor will assign an index
286 $con->register_attribute_specs(map ref() ? { %$_ } : $_, @$attr_info);
295 Moo::Role - Minimal Object Orientation support for Roles
319 # bar gets imported, but not foo
328 C<Moo::Role> builds upon L<Role::Tiny>, so look there for most of the
329 documentation on how this works. The main addition here is extra bits to make
330 the roles more "Moosey;" which is to say, it adds L</has>.
332 =head1 IMPORTED SUBROUTINES
334 See L<Role::Tiny/IMPORTED SUBROUTINES> for all the other subroutines that are
335 imported by this module.
343 Declares an attribute for the class to be composed into. See
344 L<Moo/has> for all options.
348 See L<Moo> for support and contact informations.
352 See L<Moo> for authors.
354 =head1 COPYRIGHT AND LICENSE
356 See L<Moo> for the copyright and license.