5 use base qw(Role::Tiny);
7 require Moo::sification;
9 BEGIN { *INFO = \%Role::Tiny::INFO }
16 return if $INFO{$target}; # already exported into this package
17 # get symbol table reference
18 my $stash = do { no strict 'refs'; \%{"${target}::"} };
19 _install_coderef "${target}::has" => sub {
20 my ($name, %spec) = @_;
21 ($INFO{$target}{accessor_maker} ||= do {
22 require Method::Generate::Accessor;
23 Method::Generate::Accessor->new
24 })->generate_method($target, $name, \%spec);
25 push @{$INFO{$target}{attributes}||=[]}, $name, \%spec;
27 if ($INC{'Moo/HandleMoose.pm'}) {
28 Moo::HandleMoose::inject_fake_metaclass_for($target);
30 goto &Role::Tiny::import;
33 sub _inhale_if_moose {
34 my ($self, $role) = @_;
36 if (!$INFO{$role} and $INC{"Moose.pm"}) {
37 if (my $meta = Class::MOP::class_of($role)) {
38 $INFO{$role}{methods} = {
39 map +($_ => $role->can($_)),
40 grep !$meta->get_method($_)->isa('Class::MOP::Method::Meta'),
41 $meta->get_method_list
43 $Role::Tiny::APPLIED_TO{$role} = {
44 map +($_->name => 1), $meta->calculate_all_roles
46 $INFO{$role}{requires} = [ $meta->get_required_method_list ];
47 $INFO{$role}{attributes} = [
48 map +($_ => $meta->get_attribute($_)), $meta->get_attribute_list
50 my $mods = $INFO{$role}{modifiers} = [];
51 foreach my $type (qw(before after around)) {
52 my $map = $meta->${\"get_${type}_method_modifiers_map"};
53 foreach my $method (keys %$map) {
54 foreach my $mod (@{$map->{$method}}) {
55 push @$mods, [ $type => $method => $mod ];
59 require Class::Method::Modifiers if @$mods;
60 $INFO{$role}{inhaled_from_moose} = 1;
65 sub _maybe_make_accessors {
66 my ($self, $role, $target) = @_;
68 if ($INFO{$role}{inhaled_from_moose}
69 or $m = Moo->_accessor_maker_for($target)
70 and ref($m) ne 'Method::Generate::Accessor') {
71 $self->_make_accessors($role, $target);
75 sub _make_accessors_if_moose {
76 my ($self, $role, $target) = @_;
77 if ($INFO{$role}{inhaled_from_moose}) {
78 $self->_make_accessors($role, $target);
83 my ($self, $role, $target) = @_;
84 my $acc_gen = ($Moo::MAKERS{$target}{accessor} ||= do {
85 require Method::Generate::Accessor;
86 Method::Generate::Accessor->new
88 my $con_gen = $Moo::MAKERS{$target}{constructor};
89 my @attrs = @{$INFO{$role}{attributes}||[]};
90 while (my ($name, $spec) = splice @attrs, 0, 2) {
91 # needed to ensure we got an index for an arrayref based generator
93 $spec = $con_gen->all_attribute_specs->{$name};
95 $acc_gen->generate_method($target, $name, $spec);
99 sub apply_roles_to_package {
100 my ($me, $to, @roles) = @_;
101 $me->_inhale_if_moose($_) for @roles;
102 $me->SUPER::apply_roles_to_package($to, @roles);
105 sub apply_single_role_to_package {
106 my ($me, $to, $role) = @_;
107 $me->_inhale_if_moose($role);
108 $me->_handle_constructor($to, $INFO{$role}{attributes});
109 $me->_maybe_make_accessors($role, $to);
110 $me->SUPER::apply_single_role_to_package($to, $role);
113 sub create_class_with_roles {
114 my ($me, $superclass, @roles) = @_;
117 '__WITH__', $superclass, my $compose_name = join '__AND__', @roles
120 return $new_name if $Role::Tiny::COMPOSED{class}{$new_name};
122 $me->_inhale_if_moose($_) for @roles;
125 if ($m = Moo->_accessor_maker_for($superclass)
126 and ref($m) ne 'Method::Generate::Accessor') {
127 # old fashioned way time.
128 *{_getglob("${new_name}::ISA")} = [ $superclass ];
129 $me->apply_roles_to_package($new_name, @roles);
135 $me->SUPER::create_class_with_roles($superclass, @roles);
137 foreach my $role (@roles) {
138 die "${role} is not a Role::Tiny" unless my $info = $INFO{$role};
141 $Moo::MAKERS{$new_name} = {};
143 $me->_handle_constructor(
144 $new_name, [ map @{$INFO{$_}{attributes}||[]}, @roles ], $superclass
150 sub _composable_package_for {
151 my ($self, $role) = @_;
152 my $composed_name = 'Role::Tiny::_COMPOSABLE::'.$role;
153 return $composed_name if $Role::Tiny::COMPOSED{role}{$composed_name};
154 $self->_make_accessors_if_moose($role, $composed_name);
155 $self->SUPER::_composable_package_for($role);
158 sub _install_single_modifier {
159 my ($me, @args) = @_;
160 _install_modifier(@args);
163 sub _handle_constructor {
164 my ($me, $to, $attr_info, $superclass) = @_;
165 return unless $attr_info && @$attr_info;
167 push @{$INFO{$to}{attributes}||=[]}, @$attr_info;
169 # only fiddle with the constructor if the target is a Moo class
171 and my $con = Moo->_constructor_maker_for($to, $superclass)) {
172 # shallow copy of the specs since the constructor will assign an index
173 $con->register_attribute_specs(map ref() ? { %$_ } : $_, @$attr_info);
182 Moo::Role - Minimal Object Orientation support for Roles
206 # bar gets imported, but not foo
215 C<Moo::Role> builds upon L<Role::Tiny>, so look there for most of the
216 documentation on how this works. The main addition here is extra bits to make
217 the roles more "Moosey;" which is to say, it adds L</has>.
219 =head1 IMPORTED SUBROUTINES
221 See L<Role::Tiny/IMPORTED SUBROUTINES> for all the other subroutines that are
222 imported by this module.
230 Declares an attribute for the class to be composed into. See
231 L<Moo/has> for all options.
235 See L<Moo> for authors.
237 =head1 COPYRIGHT AND LICENSE
239 See L<Moo> for the copyright and license.