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 $attr = $meta->get_attribute($_);
123 my $is_mouse = $meta->isa('Mouse::Meta::Role');
124 my $spec = { %{ $is_mouse ? $attr : $attr->original_options } };
128 my $get_constraint = do {
130 ? 'Mouse::Util::TypeConstraints'
131 : 'Moose::Util::TypeConstraints';
133 $pkg->can('find_or_create_isa_type_constraint');
136 my $tc = $get_constraint->($spec->{isa});
137 my $check = $tc->_compiled_type_constraint;
140 &$check or die "Type constraint failed for $_[0]"
143 if ($spec->{coerce}) {
145 # Mouse has _compiled_type_coercion straight on the TC object
146 $spec->{coerce} = $tc->${\(
147 $tc->can('coercion')||sub { $_[0] }
148 )}->_compiled_type_coercion;
152 }), $meta->get_attribute_list
154 my $mods = $INFO{$role}{modifiers} = [];
155 foreach my $type (qw(before after around)) {
156 # Mouse pokes its own internals so we have to fall back to doing
157 # the same thing in the absence of the Moose API method
158 my $map = $meta->${\(
159 $meta->can("get_${type}_method_modifiers_map")
160 or sub { shift->{"${type}_method_modifiers"} }
162 foreach my $method (keys %$map) {
163 foreach my $mod (@{$map->{$method}}) {
164 push @$mods, [ $type => $method => $mod ];
168 require Class::Method::Modifiers if @$mods;
169 $INFO{$role}{inhaled_from_moose} = 1;
173 sub _maybe_make_accessors {
174 my ($self, $role, $target) = @_;
176 if ($INFO{$role}{inhaled_from_moose}
178 and $m = Moo->_accessor_maker_for($target)
179 and ref($m) ne 'Method::Generate::Accessor') {
180 $self->_make_accessors($role, $target);
184 sub _make_accessors_if_moose {
185 my ($self, $role, $target) = @_;
186 if ($INFO{$role}{inhaled_from_moose}) {
187 $self->_make_accessors($role, $target);
191 sub _make_accessors {
192 my ($self, $role, $target) = @_;
193 my $acc_gen = ($Moo::MAKERS{$target}{accessor} ||= do {
194 require Method::Generate::Accessor;
195 Method::Generate::Accessor->new
197 my $con_gen = $Moo::MAKERS{$target}{constructor};
198 my @attrs = @{$INFO{$role}{attributes}||[]};
199 while (my ($name, $spec) = splice @attrs, 0, 2) {
200 # needed to ensure we got an index for an arrayref based generator
202 $spec = $con_gen->all_attribute_specs->{$name};
204 $acc_gen->generate_method($target, $name, $spec);
208 sub apply_roles_to_package {
209 my ($me, $to, @roles) = @_;
210 foreach my $role (@roles) {
211 $me->_inhale_if_moose($role);
213 $me->SUPER::apply_roles_to_package($to, @roles);
216 sub apply_single_role_to_package {
217 my ($me, $to, $role) = @_;
218 die "${role} is not a Moo::Role" unless my $info = $INFO{$role};
219 $me->_inhale_if_moose($role);
220 $me->_handle_constructor($to, $INFO{$role}{attributes});
221 $me->_maybe_make_accessors($role, $to);
222 $me->SUPER::apply_single_role_to_package($to, $role);
225 sub create_class_with_roles {
226 my ($me, $superclass, @roles) = @_;
229 '__WITH__', $superclass, my $compose_name = join '__AND__', @roles
232 return $new_name if $Role::Tiny::COMPOSED{class}{$new_name};
234 foreach my $role (@roles) {
235 $me->_inhale_if_moose($role);
240 and $m = Moo->_accessor_maker_for($superclass)
241 and ref($m) ne 'Method::Generate::Accessor') {
242 # old fashioned way time.
243 *{_getglob("${new_name}::ISA")} = [ $superclass ];
244 $me->apply_roles_to_package($new_name, @roles);
250 $me->SUPER::create_class_with_roles($superclass, @roles);
252 foreach my $role (@roles) {
253 die "${role} is not a Role::Tiny" unless my $info = $INFO{$role};
256 $Moo::MAKERS{$new_name} = {};
258 $me->_handle_constructor(
259 $new_name, [ map @{$INFO{$_}{attributes}||[]}, @roles ]
265 sub _composable_package_for {
266 my ($self, $role) = @_;
267 my $composed_name = 'Role::Tiny::_COMPOSABLE::'.$role;
268 return $composed_name if $Role::Tiny::COMPOSED{role}{$composed_name};
269 $self->_make_accessors_if_moose($role, $composed_name);
270 $self->SUPER::_composable_package_for($role);
273 sub _install_single_modifier {
274 my ($me, @args) = @_;
275 _install_modifier(@args);
278 sub _handle_constructor {
279 my ($me, $to, $attr_info) = @_;
280 return unless $attr_info && @$attr_info;
282 push @{$INFO{$to}{attributes}||=[]}, @$attr_info;
284 # only fiddle with the constructor if the target is a Moo class
286 and my $con = Moo->_constructor_maker_for($to)) {
287 # shallow copy of the specs since the constructor will assign an index
288 $con->register_attribute_specs(map ref() ? { %$_ } : $_, @$attr_info);
297 Moo::Role - Minimal Object Orientation support for Roles
321 # bar gets imported, but not foo
330 C<Moo::Role> builds upon L<Role::Tiny>, so look there for most of the
331 documentation on how this works. The main addition here is extra bits to make
332 the roles more "Moosey;" which is to say, it adds L</has>.
334 =head1 IMPORTED SUBROUTINES
336 See L<Role::Tiny/IMPORTED SUBROUTINES> for all the other subroutines that are
337 imported by this module.
345 Declares an attribute for the class to be composed into. See
346 L<Moo/has> for all options.
350 See L<Moo> for support and contact information.
354 See L<Moo> for authors.
356 =head1 COPYRIGHT AND LICENSE
358 See L<Moo> for the copyright and license.