Commit | Line | Data |
d245e471 |
1 | package Moo::Role; |
2 | |
3 | use strictures 1; |
4 | use Moo::_Utils; |
5 | use base qw(Role::Tiny); |
6 | |
a047096b |
7 | require Moo::sification; |
8 | |
d245e471 |
9 | BEGIN { *INFO = \%Role::Tiny::INFO } |
10 | |
11 | our %INFO; |
12 | |
108f8ddc |
13 | sub _install_tracked { |
14 | my ($target, $name, $code) = @_; |
15 | $INFO{$target}{exports}{$name} = $code; |
16 | _install_coderef "${target}::${name}" => "Moo::Role::${name}" => $code; |
17 | } |
18 | |
d245e471 |
19 | sub import { |
20 | my $target = caller; |
6c49212f |
21 | my ($me) = @_; |
d245e471 |
22 | strictures->import; |
1791ba32 |
23 | if ($Moo::MAKERS{$target} and $Moo::MAKERS{$target}{is_class}) { |
24 | die "Cannot import Moo::Role into a Moo class"; |
25 | } |
fa93bfb2 |
26 | $INFO{$target} ||= {}; |
d245e471 |
27 | # get symbol table reference |
28 | my $stash = do { no strict 'refs'; \%{"${target}::"} }; |
108f8ddc |
29 | _install_tracked $target => has => sub { |
1d17c7c1 |
30 | my ($name_proto, %spec) = @_; |
31 | my $name_isref = ref $name_proto eq 'ARRAY'; |
32 | foreach my $name ($name_isref ? @$name_proto : $name_proto) { |
33 | my $spec_ref = $name_isref ? +{%spec} : \%spec; |
34 | ($INFO{$target}{accessor_maker} ||= do { |
35 | require Method::Generate::Accessor; |
36 | Method::Generate::Accessor->new |
37 | })->generate_method($target, $name, $spec_ref); |
38 | push @{$INFO{$target}{attributes}||=[]}, $name, $spec_ref; |
39 | $me->_maybe_reset_handlemoose($target); |
40 | } |
d245e471 |
41 | }; |
6c49212f |
42 | # install before/after/around subs |
43 | foreach my $type (qw(before after around)) { |
108f8ddc |
44 | _install_tracked $target => $type => sub { |
6c49212f |
45 | require Class::Method::Modifiers; |
46 | push @{$INFO{$target}{modifiers}||=[]}, [ $type => @_ ]; |
47 | $me->_maybe_reset_handlemoose($target); |
48 | }; |
49 | } |
108f8ddc |
50 | _install_tracked $target => requires => sub { |
6c49212f |
51 | push @{$INFO{$target}{requires}||=[]}, @_; |
52 | $me->_maybe_reset_handlemoose($target); |
53 | }; |
108f8ddc |
54 | _install_tracked $target => with => sub { |
6c49212f |
55 | $me->apply_roles_to_package($target, @_); |
56 | $me->_maybe_reset_handlemoose($target); |
57 | }; |
fa93bfb2 |
58 | return if $INFO{$target}{is_role}; # already exported into this package |
59 | $INFO{$target}{is_role} = 1; |
60 | *{_getglob("${target}::meta")} = $me->can('meta'); |
6c49212f |
61 | # grab all *non-constant* (stash slot is not a scalarref) subs present |
62 | # in the symbol table and store their refaddrs (no need to forcibly |
63 | # inflate constant subs into real subs) - also add '' to here (this |
64 | # is used later) with a map to the coderefs in case of copying or re-use |
65 | my @not_methods = ('', map { *$_{CODE}||() } grep !ref($_), values %$stash); |
66 | @{$INFO{$target}{not_methods}={}}{@not_methods} = @not_methods; |
67 | # a role does itself |
68 | $Role::Tiny::APPLIED_TO{$target} = { $target => undef }; |
69 | |
7f9775b1 |
70 | if ($INC{'Moo/HandleMoose.pm'}) { |
71 | Moo::HandleMoose::inject_fake_metaclass_for($target); |
72 | } |
6c49212f |
73 | } |
74 | |
fa93bfb2 |
75 | # duplicate from Moo::Object |
76 | sub meta { |
77 | require Moo::HandleMoose::FakeMetaClass; |
78 | my $class = ref($_[0])||$_[0]; |
79 | bless({ name => $class }, 'Moo::HandleMoose::FakeMetaClass'); |
80 | } |
81 | |
108f8ddc |
82 | sub unimport { |
83 | my $target = caller; |
84 | _unimport_coderefs($target, $INFO{$target}); |
85 | } |
86 | |
6c49212f |
87 | sub _maybe_reset_handlemoose { |
88 | my ($class, $target) = @_; |
89 | if ($INC{"Moo/HandleMoose.pm"}) { |
90 | Moo::HandleMoose::maybe_reinject_fake_metaclass_for($target); |
91 | } |
d245e471 |
92 | } |
93 | |
a84066c7 |
94 | sub _inhale_if_moose { |
95 | my ($self, $role) = @_; |
96 | _load_module($role); |
c100c04c |
97 | my $meta; |
98 | if (!$INFO{$role} |
99 | and ( |
100 | $INC{"Moose.pm"} |
101 | and $meta = Class::MOP::class_of($role) |
c316ebe5 |
102 | and $meta->isa('Moose::Meta::Role') |
c100c04c |
103 | ) |
104 | or ( |
96b09f21 |
105 | Mouse::Util->can('find_meta') |
c100c04c |
106 | and $meta = Mouse::Util::find_meta($role) |
c316ebe5 |
107 | and $meta->isa('Mouse::Meta::Role') |
c100c04c |
108 | ) |
109 | ) { |
110 | $INFO{$role}{methods} = { |
111 | map +($_ => $role->can($_)), |
112 | grep !$meta->get_method($_)->isa('Class::MOP::Method::Meta'), |
113 | $meta->get_method_list |
114 | }; |
115 | $Role::Tiny::APPLIED_TO{$role} = { |
116 | map +($_->name => 1), $meta->calculate_all_roles |
117 | }; |
118 | $INFO{$role}{requires} = [ $meta->get_required_method_list ]; |
119 | $INFO{$role}{attributes} = [ |
a668eb67 |
120 | map +($_ => do { |
121 | my $spec = { %{$meta->get_attribute($_)} }; |
f96bb37c |
122 | |
a668eb67 |
123 | if ($spec->{isa}) { |
f96bb37c |
124 | |
125 | my $get_constraint = do { |
126 | my $pkg = $meta->isa('Mouse::Meta::Role') |
127 | ? 'Mouse::Util::TypeConstraints' |
128 | : 'Moose::Util::TypeConstraints'; |
129 | _load_module($pkg); |
130 | $pkg->can('find_or_create_isa_type_constraint'); |
131 | }; |
132 | |
133 | my $tc = $get_constraint->($spec->{isa}); |
a668eb67 |
134 | my $check = $tc->_compiled_type_constraint; |
f96bb37c |
135 | |
136 | $spec->{isa} = sub { |
137 | &$check or die "Type constraint failed for $_[0]" |
138 | }; |
139 | |
a668eb67 |
140 | if ($spec->{coerce}) { |
f96bb37c |
141 | |
142 | # Mouse has _compiled_type_coercion straight on the TC object |
143 | $spec->{coerce} = $tc->${\( |
144 | $tc->can('coercion')||sub { $_[0] } |
145 | )}->_compiled_type_coercion; |
a668eb67 |
146 | } |
147 | } |
148 | $spec; |
149 | }), $meta->get_attribute_list |
c100c04c |
150 | ]; |
151 | my $mods = $INFO{$role}{modifiers} = []; |
152 | foreach my $type (qw(before after around)) { |
153 | # Mouse pokes its own internals so we have to fall back to doing |
154 | # the same thing in the absence of the Moose API method |
155 | my $map = $meta->${\( |
156 | $meta->can("get_${type}_method_modifiers_map") |
157 | or sub { shift->{"${type}_method_modifiers"} } |
158 | )}; |
159 | foreach my $method (keys %$map) { |
160 | foreach my $mod (@{$map->{$method}}) { |
161 | push @$mods, [ $type => $method => $mod ]; |
a84066c7 |
162 | } |
163 | } |
a84066c7 |
164 | } |
c100c04c |
165 | require Class::Method::Modifiers if @$mods; |
166 | $INFO{$role}{inhaled_from_moose} = 1; |
a84066c7 |
167 | } |
168 | } |
169 | |
a41e15c3 |
170 | sub _maybe_make_accessors { |
171 | my ($self, $role, $target) = @_; |
172 | my $m; |
173 | if ($INFO{$role}{inhaled_from_moose} |
e9290d4a |
174 | or $INC{"Moo.pm"} |
175 | and $m = Moo->_accessor_maker_for($target) |
a41e15c3 |
176 | and ref($m) ne 'Method::Generate::Accessor') { |
177 | $self->_make_accessors($role, $target); |
178 | } |
179 | } |
180 | |
a84066c7 |
181 | sub _make_accessors_if_moose { |
182 | my ($self, $role, $target) = @_; |
183 | if ($INFO{$role}{inhaled_from_moose}) { |
a41e15c3 |
184 | $self->_make_accessors($role, $target); |
185 | } |
186 | } |
187 | |
188 | sub _make_accessors { |
189 | my ($self, $role, $target) = @_; |
190 | my $acc_gen = ($Moo::MAKERS{$target}{accessor} ||= do { |
191 | require Method::Generate::Accessor; |
192 | Method::Generate::Accessor->new |
193 | }); |
db10ae2d |
194 | my $con_gen = $Moo::MAKERS{$target}{constructor}; |
a41e15c3 |
195 | my @attrs = @{$INFO{$role}{attributes}||[]}; |
196 | while (my ($name, $spec) = splice @attrs, 0, 2) { |
db10ae2d |
197 | # needed to ensure we got an index for an arrayref based generator |
198 | if ($con_gen) { |
199 | $spec = $con_gen->all_attribute_specs->{$name}; |
200 | } |
a41e15c3 |
201 | $acc_gen->generate_method($target, $name, $spec); |
a84066c7 |
202 | } |
203 | } |
204 | |
4a79464d |
205 | sub apply_roles_to_package { |
206 | my ($me, $to, @roles) = @_; |
141b507a |
207 | foreach my $role (@roles) { |
208 | $me->_inhale_if_moose($role); |
209 | } |
4a79464d |
210 | $me->SUPER::apply_roles_to_package($to, @roles); |
211 | } |
212 | |
6893ea30 |
213 | sub apply_single_role_to_package { |
369a4c50 |
214 | my ($me, $to, $role) = @_; |
c316ebe5 |
215 | die "${role} is not a Moo::Role" unless my $info = $INFO{$role}; |
a84066c7 |
216 | $me->_inhale_if_moose($role); |
d245e471 |
217 | $me->_handle_constructor($to, $INFO{$role}{attributes}); |
a41e15c3 |
218 | $me->_maybe_make_accessors($role, $to); |
219 | $me->SUPER::apply_single_role_to_package($to, $role); |
d245e471 |
220 | } |
221 | |
222 | sub create_class_with_roles { |
223 | my ($me, $superclass, @roles) = @_; |
224 | |
c69190f1 |
225 | my $new_name = join( |
226 | '__WITH__', $superclass, my $compose_name = join '__AND__', @roles |
227 | ); |
228 | |
d245e471 |
229 | return $new_name if $Role::Tiny::COMPOSED{class}{$new_name}; |
230 | |
141b507a |
231 | foreach my $role (@roles) { |
232 | $me->_inhale_if_moose($role); |
233 | } |
a84066c7 |
234 | |
64284a1b |
235 | my $m; |
e9290d4a |
236 | if ($INC{"Moo.pm"} |
237 | and $m = Moo->_accessor_maker_for($superclass) |
64284a1b |
238 | and ref($m) ne 'Method::Generate::Accessor') { |
239 | # old fashioned way time. |
240 | *{_getglob("${new_name}::ISA")} = [ $superclass ]; |
241 | $me->apply_roles_to_package($new_name, @roles); |
242 | return $new_name; |
243 | } |
244 | |
faa9ce11 |
245 | require Sub::Quote; |
d245e471 |
246 | |
247 | $me->SUPER::create_class_with_roles($superclass, @roles); |
248 | |
249 | foreach my $role (@roles) { |
250 | die "${role} is not a Role::Tiny" unless my $info = $INFO{$role}; |
251 | } |
252 | |
c69190f1 |
253 | $Moo::MAKERS{$new_name} = {}; |
254 | |
d245e471 |
255 | $me->_handle_constructor( |
873df570 |
256 | $new_name, [ map @{$INFO{$_}{attributes}||[]}, @roles ], $superclass |
d245e471 |
257 | ); |
258 | |
259 | return $new_name; |
260 | } |
261 | |
a84066c7 |
262 | sub _composable_package_for { |
263 | my ($self, $role) = @_; |
264 | my $composed_name = 'Role::Tiny::_COMPOSABLE::'.$role; |
265 | return $composed_name if $Role::Tiny::COMPOSED{role}{$composed_name}; |
266 | $self->_make_accessors_if_moose($role, $composed_name); |
267 | $self->SUPER::_composable_package_for($role); |
268 | } |
269 | |
dccea57d |
270 | sub _install_single_modifier { |
271 | my ($me, @args) = @_; |
272 | _install_modifier(@args); |
d245e471 |
273 | } |
274 | |
275 | sub _handle_constructor { |
c4570291 |
276 | my ($me, $to, $attr_info, $superclass) = @_; |
57d402ef |
277 | return unless $attr_info && @$attr_info; |
d245e471 |
278 | if ($INFO{$to}) { |
57d402ef |
279 | push @{$INFO{$to}{attributes}||=[]}, @$attr_info; |
d245e471 |
280 | } else { |
281 | # only fiddle with the constructor if the target is a Moo class |
282 | if ($INC{"Moo.pm"} |
c4570291 |
283 | and my $con = Moo->_constructor_maker_for($to, $superclass)) { |
a41e15c3 |
284 | # shallow copy of the specs since the constructor will assign an index |
57d402ef |
285 | $con->register_attribute_specs(map ref() ? { %$_ } : $_, @$attr_info); |
d245e471 |
286 | } |
287 | } |
288 | } |
289 | |
290 | 1; |
bce933ec |
291 | |
0b6e5fff |
292 | =head1 NAME |
293 | |
294 | Moo::Role - Minimal Object Orientation support for Roles |
bce933ec |
295 | |
296 | =head1 SYNOPSIS |
297 | |
298 | package My::Role; |
299 | |
300 | use Moo::Role; |
301 | |
302 | sub foo { ... } |
303 | |
304 | sub bar { ... } |
305 | |
306 | has baz => ( |
307 | is => 'ro', |
308 | ); |
309 | |
310 | 1; |
311 | |
52e8f144 |
312 | And elsewhere: |
bce933ec |
313 | |
314 | package Some::Class; |
315 | |
316 | use Moo; |
317 | |
318 | # bar gets imported, but not foo |
319 | with('My::Role'); |
320 | |
321 | sub foo { ... } |
322 | |
323 | 1; |
324 | |
325 | =head1 DESCRIPTION |
326 | |
327 | C<Moo::Role> builds upon L<Role::Tiny>, so look there for most of the |
328 | documentation on how this works. The main addition here is extra bits to make |
329 | the roles more "Moosey;" which is to say, it adds L</has>. |
330 | |
331 | =head1 IMPORTED SUBROUTINES |
332 | |
333 | See L<Role::Tiny/IMPORTED SUBROUTINES> for all the other subroutines that are |
334 | imported by this module. |
335 | |
336 | =head2 has |
337 | |
338 | has attr => ( |
339 | is => 'ro', |
340 | ); |
341 | |
342 | Declares an attribute for the class to be composed into. See |
343 | L<Moo/has> for all options. |
40f3e3aa |
344 | |
072d158f |
345 | =head1 SUPPORT |
346 | |
347 | See L<Moo> for support and contact informations. |
348 | |
40f3e3aa |
349 | =head1 AUTHORS |
350 | |
351 | See L<Moo> for authors. |
352 | |
353 | =head1 COPYRIGHT AND LICENSE |
354 | |
355 | See L<Moo> for the copyright and license. |