exclude union roles and same-role-as-self from metaclass inflation
[gitmo/Moo.git] / lib / Moo / Role.pm
1 package Moo::Role;
2
3 use strictures 1;
4 use Moo::_Utils;
5 use base qw(Role::Tiny);
6
7 require Moo::sification;
8
9 BEGIN { *INFO = \%Role::Tiny::INFO }
10
11 our %INFO;
12
13 sub import {
14   my $target = caller;
15   strictures->import;
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;
26   };
27   if ($INC{'Moo/HandleMoose.pm'}) {
28     Moo::HandleMoose::inject_fake_metaclass_for($target);
29   }
30   goto &Role::Tiny::import;
31 }
32
33 sub _inhale_if_moose {
34   my ($self, $role) = @_;
35   _load_module($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
42       };
43       $Role::Tiny::APPLIED_TO{$role} = {
44         map +($_->name => 1), $meta->calculate_all_roles
45       };
46       $INFO{$role}{requires} = [ $meta->get_required_method_list ];
47       $INFO{$role}{attributes} = [
48         map +($_ => $meta->get_attribute($_)), $meta->get_attribute_list
49       ];
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 ];
56           }
57         }
58       }
59       require Class::Method::Modifiers if @$mods;
60       $INFO{$role}{inhaled_from_moose} = 1;
61     }
62   }
63 }
64
65 sub _maybe_make_accessors {
66   my ($self, $role, $target) = @_;
67   my $m;
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);
72   }
73 }
74
75 sub _make_accessors_if_moose {
76   my ($self, $role, $target) = @_;
77   if ($INFO{$role}{inhaled_from_moose}) {
78     $self->_make_accessors($role, $target);
79   }
80 }
81
82 sub _make_accessors {
83   my ($self, $role, $target) = @_;
84   my $acc_gen = ($Moo::MAKERS{$target}{accessor} ||= do {
85     require Method::Generate::Accessor;
86     Method::Generate::Accessor->new
87   });
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
92     if ($con_gen) {
93       $spec = $con_gen->all_attribute_specs->{$name};
94     }
95     $acc_gen->generate_method($target, $name, $spec);
96   }
97 }
98
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);
103 }
104
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);
111 }
112
113 sub create_class_with_roles {
114   my ($me, $superclass, @roles) = @_;
115
116   my $new_name = join(
117     '__WITH__', $superclass, my $compose_name = join '__AND__', @roles
118   );
119
120   return $new_name if $Role::Tiny::COMPOSED{class}{$new_name};
121
122   $me->_inhale_if_moose($_) for @roles;
123
124   my $m;
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);
130     return $new_name;
131   }
132
133   require Sub::Quote;
134
135   $me->SUPER::create_class_with_roles($superclass, @roles);
136
137   foreach my $role (@roles) {
138     die "${role} is not a Role::Tiny" unless my $info = $INFO{$role};
139   }
140
141   $Moo::MAKERS{$new_name} = {};
142
143   $me->_handle_constructor(
144     $new_name, [ map @{$INFO{$_}{attributes}||[]}, @roles ], $superclass
145   );
146
147   return $new_name;
148 }
149
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);
156 }
157
158 sub _install_single_modifier {
159   my ($me, @args) = @_;
160   _install_modifier(@args);
161 }
162
163 sub _handle_constructor {
164   my ($me, $to, $attr_info, $superclass) = @_;
165   return unless $attr_info && @$attr_info;
166   if ($INFO{$to}) {
167     push @{$INFO{$to}{attributes}||=[]}, @$attr_info;
168   } else {
169     # only fiddle with the constructor if the target is a Moo class
170     if ($INC{"Moo.pm"}
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);
174     }
175   }
176 }
177
178 1;
179
180 =head1 NAME
181
182 Moo::Role - Minimal Object Orientation support for Roles
183
184 =head1 SYNOPSIS
185
186  package My::Role;
187
188  use Moo::Role;
189
190  sub foo { ... }
191
192  sub bar { ... }
193
194  has baz => (
195    is => 'ro',
196  );
197
198  1;
199
200 else where
201
202  package Some::Class;
203
204  use Moo;
205
206  # bar gets imported, but not foo
207  with('My::Role');
208
209  sub foo { ... }
210
211  1;
212
213 =head1 DESCRIPTION
214
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>.
218
219 =head1 IMPORTED SUBROUTINES
220
221 See L<Role::Tiny/IMPORTED SUBROUTINES> for all the other subroutines that are
222 imported by this module.
223
224 =head2 has
225
226  has attr => (
227    is => 'ro',
228  );
229
230 Declares an attribute for the class to be composed into.  See
231 L<Moo/has> for all options.
232
233 =head1 AUTHORS
234
235 See L<Moo> for authors.
236
237 =head1 COPYRIGHT AND LICENSE
238
239 See L<Moo> for the copyright and license.
240
241 =cut