6 use base qw(Role::Tiny);
8 require Moo::sification;
10 BEGIN { *INFO = \%Role::Tiny::INFO }
15 sub _install_tracked {
16 my ($target, $name, $code) = @_;
17 $INFO{$target}{exports}{$name} = $code;
18 _install_coderef "${target}::${name}" => "Moo::Role::${name}" => $code;
25 if ($Moo::MAKERS{$target} and $Moo::MAKERS{$target}{is_class}) {
26 die "Cannot import Moo::Role into a Moo class";
28 $INFO{$target} ||= {};
29 # get symbol table reference
30 my $stash = do { no strict 'refs'; \%{"${target}::"} };
31 _install_tracked $target => has => sub {
32 my ($name_proto, %spec) = @_;
33 my $name_isref = ref $name_proto eq 'ARRAY';
34 foreach my $name ($name_isref ? @$name_proto : $name_proto) {
35 my $spec_ref = $name_isref ? +{%spec} : \%spec;
36 ($INFO{$target}{accessor_maker} ||= do {
37 require Method::Generate::Accessor;
38 Method::Generate::Accessor->new
39 })->generate_method($target, $name, $spec_ref);
40 push @{$INFO{$target}{attributes}||=[]}, $name, $spec_ref;
41 $me->_maybe_reset_handlemoose($target);
44 # install before/after/around subs
45 foreach my $type (qw(before after around)) {
46 _install_tracked $target => $type => sub {
47 require Class::Method::Modifiers;
48 push @{$INFO{$target}{modifiers}||=[]}, [ $type => @_ ];
49 $me->_maybe_reset_handlemoose($target);
52 _install_tracked $target => requires => sub {
53 push @{$INFO{$target}{requires}||=[]}, @_;
54 $me->_maybe_reset_handlemoose($target);
56 _install_tracked $target => with => sub {
57 $me->apply_roles_to_package($target, @_);
58 $me->_maybe_reset_handlemoose($target);
60 return if $INFO{$target}{is_role}; # already exported into this package
61 $INFO{$target}{is_role} = 1;
62 *{_getglob("${target}::meta")} = $me->can('meta');
63 # grab all *non-constant* (stash slot is not a scalarref) subs present
64 # in the symbol table and store their refaddrs (no need to forcibly
65 # inflate constant subs into real subs) - also add '' to here (this
66 # is used later) with a map to the coderefs in case of copying or re-use
67 my @not_methods = ('', map { *$_{CODE}||() } grep !ref($_), values %$stash);
68 @{$INFO{$target}{not_methods}={}}{@not_methods} = @not_methods;
70 $Role::Tiny::APPLIED_TO{$target} = { $target => undef };
72 if ($INC{'Moo/HandleMoose.pm'}) {
73 Moo::HandleMoose::inject_fake_metaclass_for($target);
77 # duplicate from Moo::Object
79 require Moo::HandleMoose::FakeMetaClass;
80 my $class = ref($_[0])||$_[0];
81 bless({ name => $class }, 'Moo::HandleMoose::FakeMetaClass');
86 _unimport_coderefs($target, $INFO{$target});
89 sub _maybe_reset_handlemoose {
90 my ($class, $target) = @_;
91 if ($INC{"Moo/HandleMoose.pm"}) {
92 Moo::HandleMoose::maybe_reinject_fake_metaclass_for($target);
96 sub _inhale_if_moose {
97 my ($self, $role) = @_;
103 and $meta = Class::MOP::class_of($role)
104 and $meta->isa('Moose::Meta::Role')
107 Mouse::Util->can('find_meta')
108 and $meta = Mouse::Util::find_meta($role)
109 and $meta->isa('Mouse::Meta::Role')
112 $INFO{$role}{methods} = {
113 map +($_ => $role->can($_)),
114 grep !$meta->get_method($_)->isa('Class::MOP::Method::Meta'),
115 $meta->get_method_list
117 $Role::Tiny::APPLIED_TO{$role} = {
118 map +($_->name => 1), $meta->calculate_all_roles
120 $INFO{$role}{requires} = [ $meta->get_required_method_list ];
121 $INFO{$role}{attributes} = [
123 my $attr = $meta->get_attribute($_);
124 my $is_mouse = $meta->isa('Mouse::Meta::Role');
125 my $spec = { %{ $is_mouse ? $attr : $attr->original_options } };
129 my $get_constraint = do {
131 ? 'Mouse::Util::TypeConstraints'
132 : 'Moose::Util::TypeConstraints';
134 $pkg->can('find_or_create_isa_type_constraint');
137 my $tc = $get_constraint->($spec->{isa});
138 my $check = $tc->_compiled_type_constraint;
141 &$check or die "Type constraint failed for $_[0]"
144 if ($spec->{coerce}) {
146 # Mouse has _compiled_type_coercion straight on the TC object
147 $spec->{coerce} = $tc->${\(
148 $tc->can('coercion')||sub { $_[0] }
149 )}->_compiled_type_coercion;
153 }), $meta->get_attribute_list
155 my $mods = $INFO{$role}{modifiers} = [];
156 foreach my $type (qw(before after around)) {
157 # Mouse pokes its own internals so we have to fall back to doing
158 # the same thing in the absence of the Moose API method
159 my $map = $meta->${\(
160 $meta->can("get_${type}_method_modifiers_map")
161 or sub { shift->{"${type}_method_modifiers"} }
163 foreach my $method (keys %$map) {
164 foreach my $mod (@{$map->{$method}}) {
165 push @$mods, [ $type => $method => $mod ];
169 require Class::Method::Modifiers if @$mods;
170 $INFO{$role}{inhaled_from_moose} = 1;
174 sub _maybe_make_accessors {
175 my ($self, $role, $target) = @_;
177 if ($INFO{$role}{inhaled_from_moose}
179 and $m = Moo->_accessor_maker_for($target)
180 and ref($m) ne 'Method::Generate::Accessor') {
181 $self->_make_accessors($role, $target);
185 sub _make_accessors_if_moose {
186 my ($self, $role, $target) = @_;
187 if ($INFO{$role}{inhaled_from_moose}) {
188 $self->_make_accessors($role, $target);
192 sub _make_accessors {
193 my ($self, $role, $target) = @_;
194 my $acc_gen = ($Moo::MAKERS{$target}{accessor} ||= do {
195 require Method::Generate::Accessor;
196 Method::Generate::Accessor->new
198 my $con_gen = $Moo::MAKERS{$target}{constructor};
199 my @attrs = @{$INFO{$role}{attributes}||[]};
200 while (my ($name, $spec) = splice @attrs, 0, 2) {
201 # needed to ensure we got an index for an arrayref based generator
203 $spec = $con_gen->all_attribute_specs->{$name};
205 $acc_gen->generate_method($target, $name, $spec);
209 sub apply_roles_to_package {
210 my ($me, $to, @roles) = @_;
211 foreach my $role (@roles) {
212 $me->_inhale_if_moose($role);
214 $me->SUPER::apply_roles_to_package($to, @roles);
217 sub apply_single_role_to_package {
218 my ($me, $to, $role) = @_;
219 die "${role} is not a Moo::Role" unless my $info = $INFO{$role};
220 $me->_inhale_if_moose($role);
221 $me->_handle_constructor($to, $INFO{$role}{attributes});
222 $me->_maybe_make_accessors($role, $to);
223 $me->SUPER::apply_single_role_to_package($to, $role);
226 sub create_class_with_roles {
227 my ($me, $superclass, @roles) = @_;
230 '__WITH__', $superclass, my $compose_name = join '__AND__', @roles
233 return $new_name if $Role::Tiny::COMPOSED{class}{$new_name};
235 foreach my $role (@roles) {
236 $me->_inhale_if_moose($role);
241 and $m = Moo->_accessor_maker_for($superclass)
242 and ref($m) ne 'Method::Generate::Accessor') {
243 # old fashioned way time.
244 *{_getglob("${new_name}::ISA")} = [ $superclass ];
245 $me->apply_roles_to_package($new_name, @roles);
251 $me->SUPER::create_class_with_roles($superclass, @roles);
253 foreach my $role (@roles) {
254 die "${role} is not a Role::Tiny" unless my $info = $INFO{$role};
257 $Moo::MAKERS{$new_name} = {};
259 $me->_handle_constructor(
260 $new_name, [ map @{$INFO{$_}{attributes}||[]}, @roles ]
266 sub apply_roles_to_object {
267 my ($me, $object, @roles) = @_;
268 my $new = $me->SUPER::apply_roles_to_object($object, @roles);
270 my $apply_defaults = $APPLY_DEFAULTS{ref $new} ||= do {
271 my %attrs = map { @{$INFO{$_}{attributes}||[]} } @roles;
275 and my $con_gen = Moo->_constructor_maker_for(ref $new)
276 and my $m = Moo->_accessor_maker_for(ref $new)) {
279 my $specs = $con_gen->all_attribute_specs;
283 foreach my $name ( keys %attrs ) {
284 my $spec = $specs->{$name};
285 if ($m->has_eager_default($name, $spec)) {
287 = $m->generate_simple_has('$_[0]', $name, $spec);
289 = $m->generate_use_default('$_[0]', $name, $spec, $has);
292 @captures{keys %$has_cap, keys %$pop_cap}
293 = (values %$has_cap, values %$pop_cap);
296 Sub::Quote::quote_sub($assign, \%captures);
302 $new->$apply_defaults;
306 sub _composable_package_for {
307 my ($self, $role) = @_;
308 my $composed_name = 'Role::Tiny::_COMPOSABLE::'.$role;
309 return $composed_name if $Role::Tiny::COMPOSED{role}{$composed_name};
310 $self->_make_accessors_if_moose($role, $composed_name);
311 $self->SUPER::_composable_package_for($role);
314 sub _install_single_modifier {
315 my ($me, @args) = @_;
316 _install_modifier(@args);
319 sub _handle_constructor {
320 my ($me, $to, $attr_info) = @_;
321 return unless $attr_info && @$attr_info;
323 push @{$INFO{$to}{attributes}||=[]}, @$attr_info;
325 # only fiddle with the constructor if the target is a Moo class
327 and my $con = Moo->_constructor_maker_for($to)) {
328 # shallow copy of the specs since the constructor will assign an index
329 $con->register_attribute_specs(map ref() ? { %$_ } : $_, @$attr_info);
338 Moo::Role - Minimal Object Orientation support for Roles
362 # bar gets imported, but not foo
371 C<Moo::Role> builds upon L<Role::Tiny>, so look there for most of the
372 documentation on how this works. The main addition here is extra bits to make
373 the roles more "Moosey;" which is to say, it adds L</has>.
375 =head1 IMPORTED SUBROUTINES
377 See L<Role::Tiny/IMPORTED SUBROUTINES> for all the other subroutines that are
378 imported by this module.
386 Declares an attribute for the class to be composed into. See
387 L<Moo/has> for all options.
391 See L<Moo> for support and contact information.
395 See L<Moo> for authors.
397 =head1 COPYRIGHT AND LICENSE
399 See L<Moo> for the copyright and license.