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_proto, %spec) = @_;
29 my $name_isref = ref $name_proto eq 'ARRAY';
30 foreach my $name ($name_isref ? @$name_proto : $name_proto) {
31 my $spec_ref = $name_isref ? +{%spec} : \%spec;
32 ($INFO{$target}{accessor_maker} ||= do {
33 require Method::Generate::Accessor;
34 Method::Generate::Accessor->new
35 })->generate_method($target, $name, $spec_ref);
36 push @{$INFO{$target}{attributes}||=[]}, $name, $spec_ref;
37 $me->_maybe_reset_handlemoose($target);
40 # install before/after/around subs
41 foreach my $type (qw(before after around)) {
42 _install_tracked $target => $type => sub {
43 require Class::Method::Modifiers;
44 push @{$INFO{$target}{modifiers}||=[]}, [ $type => @_ ];
45 $me->_maybe_reset_handlemoose($target);
48 _install_tracked $target => requires => sub {
49 push @{$INFO{$target}{requires}||=[]}, @_;
50 $me->_maybe_reset_handlemoose($target);
52 _install_tracked $target => with => sub {
53 $me->apply_roles_to_package($target, @_);
54 $me->_maybe_reset_handlemoose($target);
56 # grab all *non-constant* (stash slot is not a scalarref) subs present
57 # in the symbol table and store their refaddrs (no need to forcibly
58 # inflate constant subs into real subs) - also add '' to here (this
59 # is used later) with a map to the coderefs in case of copying or re-use
60 my @not_methods = ('', map { *$_{CODE}||() } grep !ref($_), values %$stash);
61 @{$INFO{$target}{not_methods}={}}{@not_methods} = @not_methods;
63 $Role::Tiny::APPLIED_TO{$target} = { $target => undef };
65 if ($INC{'Moo/HandleMoose.pm'}) {
66 Moo::HandleMoose::inject_fake_metaclass_for($target);
72 _unimport_coderefs($target, $INFO{$target});
75 sub _maybe_reset_handlemoose {
76 my ($class, $target) = @_;
77 if ($INC{"Moo/HandleMoose.pm"}) {
78 Moo::HandleMoose::maybe_reinject_fake_metaclass_for($target);
82 sub _inhale_if_moose {
83 my ($self, $role) = @_;
89 and $meta = Class::MOP::class_of($role)
93 and $meta = Mouse::Util::find_meta($role)
96 $INFO{$role}{methods} = {
97 map +($_ => $role->can($_)),
98 grep !$meta->get_method($_)->isa('Class::MOP::Method::Meta'),
99 $meta->get_method_list
101 $Role::Tiny::APPLIED_TO{$role} = {
102 map +($_->name => 1), $meta->calculate_all_roles
104 $INFO{$role}{requires} = [ $meta->get_required_method_list ];
105 $INFO{$role}{attributes} = [
107 my $spec = { %{$meta->get_attribute($_)} };
111 my $get_constraint = do {
112 my $pkg = $meta->isa('Mouse::Meta::Role')
113 ? 'Mouse::Util::TypeConstraints'
114 : 'Moose::Util::TypeConstraints';
116 $pkg->can('find_or_create_isa_type_constraint');
119 my $tc = $get_constraint->($spec->{isa});
120 my $check = $tc->_compiled_type_constraint;
123 &$check or die "Type constraint failed for $_[0]"
126 if ($spec->{coerce}) {
128 # Mouse has _compiled_type_coercion straight on the TC object
129 $spec->{coerce} = $tc->${\(
130 $tc->can('coercion')||sub { $_[0] }
131 )}->_compiled_type_coercion;
135 }), $meta->get_attribute_list
137 my $mods = $INFO{$role}{modifiers} = [];
138 foreach my $type (qw(before after around)) {
139 # Mouse pokes its own internals so we have to fall back to doing
140 # the same thing in the absence of the Moose API method
141 my $map = $meta->${\(
142 $meta->can("get_${type}_method_modifiers_map")
143 or sub { shift->{"${type}_method_modifiers"} }
145 foreach my $method (keys %$map) {
146 foreach my $mod (@{$map->{$method}}) {
147 push @$mods, [ $type => $method => $mod ];
151 require Class::Method::Modifiers if @$mods;
152 $INFO{$role}{inhaled_from_moose} = 1;
156 sub _maybe_make_accessors {
157 my ($self, $role, $target) = @_;
159 if ($INFO{$role}{inhaled_from_moose}
161 and $m = Moo->_accessor_maker_for($target)
162 and ref($m) ne 'Method::Generate::Accessor') {
163 $self->_make_accessors($role, $target);
167 sub _make_accessors_if_moose {
168 my ($self, $role, $target) = @_;
169 if ($INFO{$role}{inhaled_from_moose}) {
170 $self->_make_accessors($role, $target);
174 sub _make_accessors {
175 my ($self, $role, $target) = @_;
176 my $acc_gen = ($Moo::MAKERS{$target}{accessor} ||= do {
177 require Method::Generate::Accessor;
178 Method::Generate::Accessor->new
180 my $con_gen = $Moo::MAKERS{$target}{constructor};
181 my @attrs = @{$INFO{$role}{attributes}||[]};
182 while (my ($name, $spec) = splice @attrs, 0, 2) {
183 # needed to ensure we got an index for an arrayref based generator
185 $spec = $con_gen->all_attribute_specs->{$name};
187 $acc_gen->generate_method($target, $name, $spec);
191 sub apply_roles_to_package {
192 my ($me, $to, @roles) = @_;
193 $me->_inhale_if_moose($_) for @roles;
194 $me->SUPER::apply_roles_to_package($to, @roles);
197 sub apply_single_role_to_package {
198 my ($me, $to, $role) = @_;
199 $me->_inhale_if_moose($role);
200 $me->_handle_constructor($to, $INFO{$role}{attributes});
201 $me->_maybe_make_accessors($role, $to);
202 $me->SUPER::apply_single_role_to_package($to, $role);
205 sub create_class_with_roles {
206 my ($me, $superclass, @roles) = @_;
209 '__WITH__', $superclass, my $compose_name = join '__AND__', @roles
212 return $new_name if $Role::Tiny::COMPOSED{class}{$new_name};
214 $me->_inhale_if_moose($_) for @roles;
218 and $m = Moo->_accessor_maker_for($superclass)
219 and ref($m) ne 'Method::Generate::Accessor') {
220 # old fashioned way time.
221 *{_getglob("${new_name}::ISA")} = [ $superclass ];
222 $me->apply_roles_to_package($new_name, @roles);
228 $me->SUPER::create_class_with_roles($superclass, @roles);
230 foreach my $role (@roles) {
231 die "${role} is not a Role::Tiny" unless my $info = $INFO{$role};
234 $Moo::MAKERS{$new_name} = {};
236 $me->_handle_constructor(
237 $new_name, [ map @{$INFO{$_}{attributes}||[]}, @roles ], $superclass
243 sub _composable_package_for {
244 my ($self, $role) = @_;
245 my $composed_name = 'Role::Tiny::_COMPOSABLE::'.$role;
246 return $composed_name if $Role::Tiny::COMPOSED{role}{$composed_name};
247 $self->_make_accessors_if_moose($role, $composed_name);
248 $self->SUPER::_composable_package_for($role);
251 sub _install_single_modifier {
252 my ($me, @args) = @_;
253 _install_modifier(@args);
256 sub _handle_constructor {
257 my ($me, $to, $attr_info, $superclass) = @_;
258 return unless $attr_info && @$attr_info;
260 push @{$INFO{$to}{attributes}||=[]}, @$attr_info;
262 # only fiddle with the constructor if the target is a Moo class
264 and my $con = Moo->_constructor_maker_for($to, $superclass)) {
265 # shallow copy of the specs since the constructor will assign an index
266 $con->register_attribute_specs(map ref() ? { %$_ } : $_, @$attr_info);
275 Moo::Role - Minimal Object Orientation support for Roles
299 # bar gets imported, but not foo
308 C<Moo::Role> builds upon L<Role::Tiny>, so look there for most of the
309 documentation on how this works. The main addition here is extra bits to make
310 the roles more "Moosey;" which is to say, it adds L</has>.
312 =head1 IMPORTED SUBROUTINES
314 See L<Role::Tiny/IMPORTED SUBROUTINES> for all the other subroutines that are
315 imported by this module.
323 Declares an attribute for the class to be composed into. See
324 L<Moo/has> for all options.
328 See L<Moo> for authors.
330 =head1 COPYRIGHT AND LICENSE
332 See L<Moo> for the copyright and license.