Commit | Line | Data |
e185c027 |
1 | |
2 | package Moose::Meta::Role; |
3 | |
4 | use strict; |
5 | use warnings; |
6 | use metaclass; |
7 | |
21f1e231 |
8 | use Scalar::Util 'blessed'; |
b6a00b82 |
9 | use Carp 'confess'; |
9f2230e9 |
10 | use Sub::Name 'subname'; |
963c24e1 |
11 | use Devel::GlobalDestruction 'in_global_destruction'; |
bdabd620 |
12 | |
dbe21639 |
13 | our $VERSION = '0.79'; |
e606ae5f |
14 | $VERSION = eval $VERSION; |
d44714be |
15 | our $AUTHORITY = 'cpan:STEVAN'; |
e185c027 |
16 | |
8ee73eeb |
17 | use Moose::Meta::Class; |
39b3bc94 |
18 | use Moose::Meta::Role::Method; |
d67145ed |
19 | use Moose::Meta::Role::Method::Required; |
8ee73eeb |
20 | |
68efb014 |
21 | use base 'Class::MOP::Module'; |
80572233 |
22 | |
fb1e11d5 |
23 | ## ------------------------------------------------------------------ |
24 | ## NOTE: |
25 | ## I normally don't do this, but I am doing |
26 | ## a whole bunch of meta-programmin in this |
27 | ## module, so it just makes sense. For a clearer |
d03bd989 |
28 | ## picture of what is going on in the next |
29 | ## several lines of code, look at the really |
fb1e11d5 |
30 | ## big comment at the end of this file (right |
31 | ## before the POD). |
32 | ## - SL |
33 | ## ------------------------------------------------------------------ |
80572233 |
34 | |
21716c07 |
35 | my $META = __PACKAGE__->meta; |
36 | |
37 | ## ------------------------------------------------------------------ |
38 | ## attributes ... |
e185c027 |
39 | |
21716c07 |
40 | # NOTE: |
41 | # since roles are lazy, we hold all the attributes |
fb1e11d5 |
42 | # of the individual role in 'statis' until which |
43 | # time when it is applied to a class. This means |
44 | # keeping a lot of things in hash maps, so we are |
21716c07 |
45 | # using a little of that meta-programmin' magic |
d03bd989 |
46 | # here an saving lots of extra typin. And since |
fb1e11d5 |
47 | # many of these attributes above require similar |
48 | # functionality to support them, so we again use |
49 | # the wonders of meta-programmin' to deliver a |
21716c07 |
50 | # very compact solution to this normally verbose |
51 | # problem. |
52 | # - SL |
53 | |
54 | foreach my $action ( |
fb1e11d5 |
55 | { |
56 | name => 'excluded_roles_map', |
57 | attr_reader => 'get_excluded_roles_map' , |
21716c07 |
58 | methods => { |
fb1e11d5 |
59 | add => 'add_excluded_roles', |
60 | get_list => 'get_excluded_roles_list', |
61 | existence => 'excludes_role', |
21716c07 |
62 | } |
63 | }, |
fb1e11d5 |
64 | { |
65 | name => 'required_methods', |
21716c07 |
66 | attr_reader => 'get_required_methods_map', |
67 | methods => { |
21716c07 |
68 | remove => 'remove_required_methods', |
69 | get_list => 'get_required_method_list', |
70 | existence => 'requires_method', |
71 | } |
d03bd989 |
72 | }, |
21716c07 |
73 | { |
fb1e11d5 |
74 | name => 'attribute_map', |
21716c07 |
75 | attr_reader => 'get_attribute_map', |
76 | methods => { |
77 | get => 'get_attribute', |
78 | get_list => 'get_attribute_list', |
79 | existence => 'has_attribute', |
80 | remove => 'remove_attribute', |
81 | } |
82 | } |
83 | ) { |
fb1e11d5 |
84 | |
21716c07 |
85 | my $attr_reader = $action->{attr_reader}; |
86 | my $methods = $action->{methods}; |
fb1e11d5 |
87 | |
88 | # create the attribute |
89 | $META->add_attribute($action->{name} => ( |
90 | reader => $attr_reader, |
91 | default => sub { {} } |
92 | )); |
93 | |
94 | # create some helper methods |
21716c07 |
95 | $META->add_method($methods->{add} => sub { |
96 | my ($self, @values) = @_; |
fb1e11d5 |
97 | $self->$attr_reader->{$_} = undef foreach @values; |
21716c07 |
98 | }) if exists $methods->{add}; |
fb1e11d5 |
99 | |
21716c07 |
100 | $META->add_method($methods->{get_list} => sub { |
101 | my ($self) = @_; |
fb1e11d5 |
102 | keys %{$self->$attr_reader}; |
103 | }) if exists $methods->{get_list}; |
104 | |
21716c07 |
105 | $META->add_method($methods->{get} => sub { |
106 | my ($self, $name) = @_; |
fb1e11d5 |
107 | $self->$attr_reader->{$name} |
108 | }) if exists $methods->{get}; |
109 | |
21716c07 |
110 | $META->add_method($methods->{existence} => sub { |
111 | my ($self, $name) = @_; |
fb1e11d5 |
112 | exists $self->$attr_reader->{$name} ? 1 : 0; |
113 | }) if exists $methods->{existence}; |
114 | |
21716c07 |
115 | $META->add_method($methods->{remove} => sub { |
116 | my ($self, @values) = @_; |
117 | delete $self->$attr_reader->{$_} foreach @values; |
fb1e11d5 |
118 | }) if exists $methods->{remove}; |
21716c07 |
119 | } |
d79e62fd |
120 | |
b05518b2 |
121 | $META->add_attribute( |
122 | 'method_metaclass', |
123 | reader => 'method_metaclass', |
124 | default => 'Moose::Meta::Role::Method', |
125 | ); |
126 | |
77ed8b8d |
127 | $META->add_attribute( |
128 | 'required_method_metaclass', |
129 | reader => 'required_method_metaclass', |
130 | default => 'Moose::Meta::Role::Method::Required', |
131 | ); |
132 | |
21716c07 |
133 | ## some things don't always fit, so they go here ... |
80572233 |
134 | |
21716c07 |
135 | sub add_attribute { |
136 | my $self = shift; |
137 | my $name = shift; |
70ea9161 |
138 | unless ( defined $name && $name ) { |
139 | require Moose; |
140 | Moose->throw_error("You must provide a name for the attribute"); |
141 | } |
21716c07 |
142 | my $attr_desc; |
143 | if (scalar @_ == 1 && ref($_[0]) eq 'HASH') { |
144 | $attr_desc = $_[0]; |
145 | } |
146 | else { |
147 | $attr_desc = { @_ }; |
148 | } |
149 | $self->get_attribute_map->{$name} = $attr_desc; |
150 | } |
e185c027 |
151 | |
16d721b3 |
152 | sub add_required_methods { |
153 | my $self = shift; |
154 | |
155 | for (@_) { |
156 | my $method = $_; |
c9d7e396 |
157 | if (!blessed($method)) { |
16d721b3 |
158 | $method = $self->required_method_metaclass->new( |
159 | name => $method, |
160 | ); |
161 | } |
162 | $self->get_required_methods_map->{$method->name} = $method; |
163 | } |
164 | } |
165 | |
21716c07 |
166 | ## ------------------------------------------------------------------ |
167 | ## method modifiers |
168 | |
21716c07 |
169 | # NOTE: |
fb1e11d5 |
170 | # the before/around/after method modifiers are |
21716c07 |
171 | # stored by name, but there can be many methods |
172 | # then associated with that name. So again we have |
173 | # lots of similar functionality, so we can do some |
174 | # meta-programmin' and save some time. |
175 | # - SL |
176 | |
177 | foreach my $modifier_type (qw[ before around after ]) { |
fb1e11d5 |
178 | |
179 | my $attr_reader = "get_${modifier_type}_method_modifiers_map"; |
d03bd989 |
180 | |
fb1e11d5 |
181 | # create the attribute ... |
182 | $META->add_attribute("${modifier_type}_method_modifiers" => ( |
183 | reader => $attr_reader, |
184 | default => sub { {} } |
d03bd989 |
185 | )); |
fb1e11d5 |
186 | |
187 | # and some helper methods ... |
21716c07 |
188 | $META->add_method("get_${modifier_type}_method_modifiers" => sub { |
189 | my ($self, $method_name) = @_; |
fb1e11d5 |
190 | #return () unless exists $self->$attr_reader->{$method_name}; |
21716c07 |
191 | @{$self->$attr_reader->{$method_name}}; |
192 | }); |
fb1e11d5 |
193 | |
21716c07 |
194 | $META->add_method("has_${modifier_type}_method_modifiers" => sub { |
195 | my ($self, $method_name) = @_; |
196 | # NOTE: |
fb1e11d5 |
197 | # for now we assume that if it exists,.. |
21716c07 |
198 | # it has at least one modifier in it |
199 | (exists $self->$attr_reader->{$method_name}) ? 1 : 0; |
fb1e11d5 |
200 | }); |
201 | |
21716c07 |
202 | $META->add_method("add_${modifier_type}_method_modifier" => sub { |
203 | my ($self, $method_name, $method) = @_; |
fb1e11d5 |
204 | |
205 | $self->$attr_reader->{$method_name} = [] |
21716c07 |
206 | unless exists $self->$attr_reader->{$method_name}; |
fb1e11d5 |
207 | |
21716c07 |
208 | my $modifiers = $self->$attr_reader->{$method_name}; |
fb1e11d5 |
209 | |
21716c07 |
210 | # NOTE: |
fb1e11d5 |
211 | # check to see that we aren't adding the |
212 | # same code twice. We err in favor of the |
21716c07 |
213 | # first on here, this may not be as expected |
214 | foreach my $modifier (@{$modifiers}) { |
215 | return if $modifier == $method; |
216 | } |
fb1e11d5 |
217 | |
21716c07 |
218 | push @{$modifiers} => $method; |
219 | }); |
fb1e11d5 |
220 | |
21716c07 |
221 | } |
1331430a |
222 | |
21716c07 |
223 | ## ------------------------------------------------------------------ |
224 | ## override method mofidiers |
0558683c |
225 | |
fb1e11d5 |
226 | $META->add_attribute('override_method_modifiers' => ( |
227 | reader => 'get_override_method_modifiers_map', |
228 | default => sub { {} } |
229 | )); |
230 | |
21716c07 |
231 | # NOTE: |
fb1e11d5 |
232 | # these are a little different because there |
21716c07 |
233 | # can only be one per name, whereas the other |
234 | # method modifiers can have multiples. |
235 | # - SL |
0558683c |
236 | |
21716c07 |
237 | sub add_override_method_modifier { |
238 | my ($self, $method_name, $method) = @_; |
239 | (!$self->has_method($method_name)) |
c245d69b |
240 | || Moose->throw_error("Cannot add an override of method '$method_name' " . |
4c0b3599 |
241 | "because there is a local version of '$method_name'"); |
fb1e11d5 |
242 | $self->get_override_method_modifiers_map->{$method_name} = $method; |
21716c07 |
243 | } |
0558683c |
244 | |
21716c07 |
245 | sub has_override_method_modifier { |
246 | my ($self, $method_name) = @_; |
247 | # NOTE: |
fb1e11d5 |
248 | # for now we assume that if it exists,.. |
21716c07 |
249 | # it has at least one modifier in it |
fb1e11d5 |
250 | (exists $self->get_override_method_modifiers_map->{$method_name}) ? 1 : 0; |
21716c07 |
251 | } |
0558683c |
252 | |
21716c07 |
253 | sub get_override_method_modifier { |
254 | my ($self, $method_name) = @_; |
fb1e11d5 |
255 | $self->get_override_method_modifiers_map->{$method_name}; |
21716c07 |
256 | } |
0558683c |
257 | |
21716c07 |
258 | ## general list accessor ... |
80572233 |
259 | |
21716c07 |
260 | sub get_method_modifier_list { |
261 | my ($self, $modifier_type) = @_; |
fb1e11d5 |
262 | my $accessor = "get_${modifier_type}_method_modifiers_map"; |
21716c07 |
263 | keys %{$self->$accessor}; |
264 | } |
e185c027 |
265 | |
e606ae5f |
266 | sub reset_package_cache_flag { (shift)->{'_package_cache_flag'} = undef } |
267 | sub update_package_cache_flag { |
268 | my $self = shift; |
269 | $self->{'_package_cache_flag'} = Class::MOP::check_package_cache_flag($self->name); |
270 | } |
271 | |
272 | |
273 | |
21716c07 |
274 | ## ------------------------------------------------------------------ |
80572233 |
275 | ## subroles |
276 | |
278ca49c |
277 | $META->add_attribute('roles' => ( |
21716c07 |
278 | reader => 'get_roles', |
279 | default => sub { [] } |
280 | )); |
281 | |
80572233 |
282 | sub add_role { |
283 | my ($self, $role) = @_; |
284 | (blessed($role) && $role->isa('Moose::Meta::Role')) |
c245d69b |
285 | || Moose->throw_error("Roles must be instances of Moose::Meta::Role"); |
80572233 |
286 | push @{$self->get_roles} => $role; |
e606ae5f |
287 | $self->reset_package_cache_flag; |
80572233 |
288 | } |
289 | |
b8aeb4dc |
290 | sub calculate_all_roles { |
291 | my $self = shift; |
292 | my %seen; |
fb1e11d5 |
293 | grep { |
294 | !$seen{$_->name}++ |
295 | } ($self, map { |
296 | $_->calculate_all_roles |
297 | } @{ $self->get_roles }); |
b8aeb4dc |
298 | } |
299 | |
80572233 |
300 | sub does_role { |
301 | my ($self, $role_name) = @_; |
302 | (defined $role_name) |
c245d69b |
303 | || Moose->throw_error("You must supply a role name to look for"); |
bdabd620 |
304 | # if we are it,.. then return true |
305 | return 1 if $role_name eq $self->name; |
306 | # otherwise.. check our children |
80572233 |
307 | foreach my $role (@{$self->get_roles}) { |
bdabd620 |
308 | return 1 if $role->does_role($role_name); |
80572233 |
309 | } |
310 | return 0; |
311 | } |
312 | |
21716c07 |
313 | ## ------------------------------------------------------------------ |
fb1e11d5 |
314 | ## methods |
1331430a |
315 | |
fb1e11d5 |
316 | sub get_method_map { |
093b12c2 |
317 | my $self = shift; |
e606ae5f |
318 | |
319 | my $current = Class::MOP::check_package_cache_flag($self->name); |
320 | |
321 | if (defined $self->{'_package_cache_flag'} && $self->{'_package_cache_flag'} == $current) { |
322 | return $self->{'methods'} ||= {}; |
323 | } |
324 | |
325 | $self->{_package_cache_flag} = $current; |
326 | |
327 | my $map = $self->{'methods'} ||= {}; |
fb1e11d5 |
328 | |
329 | my $role_name = $self->name; |
330 | my $method_metaclass = $self->method_metaclass; |
331 | |
534000fd |
332 | my $all_code = $self->get_all_package_symbols('CODE'); |
fb1e11d5 |
333 | |
534000fd |
334 | foreach my $symbol (keys %{ $all_code }) { |
335 | my $code = $all_code->{$symbol}; |
fb1e11d5 |
336 | |
e606ae5f |
337 | next if exists $map->{$symbol} && |
338 | defined $map->{$symbol} && |
339 | $map->{$symbol}->body == $code; |
340 | |
fb1e11d5 |
341 | my ($pkg, $name) = Class::MOP::get_code_info($code); |
e55d4727 |
342 | my $meta = Class::MOP::class_of($pkg); |
fb1e11d5 |
343 | |
e55d4727 |
344 | if ($meta && $meta->isa('Moose::Meta::Role')) { |
345 | my $role = $meta->name; |
fb1e11d5 |
346 | next unless $self->does_role($role); |
347 | } |
348 | else { |
2887c827 |
349 | # NOTE: |
d03bd989 |
350 | # in 5.10 constant.pm the constants show up |
2887c827 |
351 | # as being in the right package, but in pre-5.10 |
d03bd989 |
352 | # they show up as constant::__ANON__ so we |
2887c827 |
353 | # make an exception here to be sure that things |
354 | # work as expected in both. |
355 | # - SL |
356 | unless ($pkg eq 'constant' && $name eq '__ANON__') { |
357 | next if ($pkg || '') ne $role_name || |
358 | (($name || '') ne '__ANON__' && ($pkg || '') ne $role_name); |
d03bd989 |
359 | } |
fb1e11d5 |
360 | } |
d03bd989 |
361 | |
1b2aea39 |
362 | $map->{$symbol} = $method_metaclass->wrap( |
363 | $code, |
364 | package_name => $role_name, |
d03bd989 |
365 | name => $name |
1b2aea39 |
366 | ); |
fb1e11d5 |
367 | } |
368 | |
d03bd989 |
369 | return $map; |
093b12c2 |
370 | } |
371 | |
d03bd989 |
372 | sub get_method { |
fb1e11d5 |
373 | my ($self, $name) = @_; |
c4538447 |
374 | $self->get_method_map->{$name}; |
fb1e11d5 |
375 | } |
40e89659 |
376 | |
fb1e11d5 |
377 | sub has_method { |
378 | my ($self, $name) = @_; |
379 | exists $self->get_method_map->{$name} ? 1 : 0 |
e185c027 |
380 | } |
381 | |
8c5f7ce4 |
382 | # FIXME this is copy-pasted from Class::MOP::Class |
e606ae5f |
383 | # refactor to inherit from some common base |
384 | sub wrap_method_body { |
385 | my ( $self, %args ) = @_; |
386 | |
87e63626 |
387 | ('CODE' eq ref $args{body}) |
c245d69b |
388 | || Moose->throw_error("Your code block must be a CODE reference"); |
e606ae5f |
389 | |
87e63626 |
390 | $self->method_metaclass->wrap( |
e606ae5f |
391 | package_name => $self->name, |
392 | %args, |
87e63626 |
393 | ); |
e606ae5f |
394 | } |
395 | |
396 | sub add_method { |
397 | my ($self, $method_name, $method) = @_; |
398 | (defined $method_name && $method_name) |
c245d69b |
399 | || Moose->throw_error("You must define a method name"); |
e606ae5f |
400 | |
401 | my $body; |
402 | if (blessed($method)) { |
403 | $body = $method->body; |
87e63626 |
404 | if ($method->package_name ne $self->name) { |
e606ae5f |
405 | $method = $method->clone( |
406 | package_name => $self->name, |
d03bd989 |
407 | name => $method_name |
e606ae5f |
408 | ) if $method->can('clone'); |
409 | } |
410 | } |
411 | else { |
412 | $body = $method; |
413 | $method = $self->wrap_method_body( body => $body, name => $method_name ); |
414 | } |
415 | |
416 | $method->attach_to_class($self); |
417 | |
418 | $self->get_method_map->{$method_name} = $method; |
419 | |
420 | my $full_method_name = ($self->name . '::' . $method_name); |
421 | $self->add_package_symbol( |
422 | { sigil => '&', type => 'CODE', name => $method_name }, |
9f2230e9 |
423 | subname($full_method_name => $body) |
e606ae5f |
424 | ); |
425 | |
426 | $self->update_package_cache_flag; # still valid, since we just added the method to the map, and if it was invalid before that then get_method_map updated it |
427 | } |
428 | |
1db8ecc7 |
429 | sub find_method_by_name { (shift)->get_method(@_) } |
093b12c2 |
430 | |
fb1e11d5 |
431 | sub get_method_list { |
432 | my $self = shift; |
433 | grep { !/^meta$/ } keys %{$self->get_method_map}; |
434 | } |
435 | |
436 | sub alias_method { |
8b7cb9ab |
437 | Carp::cluck("The alias_method method is deprecated. Use add_method instead.\n"); |
89e6757a |
438 | |
87e63626 |
439 | my $self = shift; |
fb1e11d5 |
440 | |
87e63626 |
441 | $self->add_method(@_); |
fb1e11d5 |
442 | } |
443 | |
21716c07 |
444 | ## ------------------------------------------------------------------ |
fb1e11d5 |
445 | ## role construction |
21716c07 |
446 | ## ------------------------------------------------------------------ |
e185c027 |
447 | |
21716c07 |
448 | sub apply { |
bca01282 |
449 | my ($self, $other, @args) = @_; |
450 | |
451 | (blessed($other)) |
c245d69b |
452 | || Moose->throw_error("You must pass in an blessed instance"); |
d03bd989 |
453 | |
1c9db35c |
454 | if ($other->isa('Moose::Meta::Role')) { |
455 | require Moose::Meta::Role::Application::ToRole; |
709c321c |
456 | return Moose::Meta::Role::Application::ToRole->new(@args)->apply($self, $other); |
1c9db35c |
457 | } |
458 | elsif ($other->isa('Moose::Meta::Class')) { |
459 | require Moose::Meta::Role::Application::ToClass; |
709c321c |
460 | return Moose::Meta::Role::Application::ToClass->new(@args)->apply($self, $other); |
d03bd989 |
461 | } |
1c9db35c |
462 | else { |
463 | require Moose::Meta::Role::Application::ToInstance; |
d03bd989 |
464 | return Moose::Meta::Role::Application::ToInstance->new(@args)->apply($self, $other); |
465 | } |
0558683c |
466 | } |
467 | |
21716c07 |
468 | sub combine { |
a4e516f6 |
469 | my ($class, @role_specs) = @_; |
d03bd989 |
470 | |
fb1e11d5 |
471 | require Moose::Meta::Role::Application::RoleSummation; |
d03bd989 |
472 | require Moose::Meta::Role::Composite; |
473 | |
28412c0b |
474 | my (@roles, %role_params); |
475 | while (@role_specs) { |
31d96c3e |
476 | my ($role_name, $params) = @{ splice @role_specs, 0, 1 }; |
477 | my $requested_role = Class::MOP::class_of($role_name); |
478 | |
6bdef412 |
479 | my $actual_role = $requested_role->_role_for_combination($params); |
31d96c3e |
480 | push @roles => $actual_role; |
481 | |
28412c0b |
482 | next unless defined $params; |
31d96c3e |
483 | $role_params{$actual_role->name} = $params; |
28412c0b |
484 | } |
d03bd989 |
485 | |
fb1e11d5 |
486 | my $c = Moose::Meta::Role::Composite->new(roles => \@roles); |
a4e516f6 |
487 | Moose::Meta::Role::Application::RoleSummation->new( |
28412c0b |
488 | role_params => \%role_params |
a4e516f6 |
489 | )->apply($c); |
d03bd989 |
490 | |
fb1e11d5 |
491 | return $c; |
0558683c |
492 | } |
493 | |
6bdef412 |
494 | sub _role_for_combination { |
436d7a28 |
495 | my ($self, $params) = @_; |
496 | return $self; |
497 | } |
498 | |
b6a00b82 |
499 | sub create { |
2d5f3fab |
500 | my ( $role, $package_name, %options ) = @_; |
b6a00b82 |
501 | |
2d5f3fab |
502 | $options{package} = $package_name; |
b6a00b82 |
503 | |
504 | (ref $options{attributes} eq 'HASH') |
505 | || confess "You must pass a HASH ref of attributes" |
506 | if exists $options{attributes}; |
507 | |
508 | (ref $options{methods} eq 'HASH') |
509 | || confess "You must pass a HASH ref of methods" |
510 | if exists $options{methods}; |
511 | |
2d5f3fab |
512 | my (%initialize_options) = %options; |
b6a00b82 |
513 | delete @initialize_options{qw( |
514 | package |
515 | attributes |
516 | methods |
517 | version |
518 | authority |
519 | )}; |
520 | |
521 | my $meta = $role->initialize( $package_name => %initialize_options ); |
522 | |
6a4a7c31 |
523 | $meta->_instantiate_module( $options{version}, $options{authority} ); |
524 | |
b6a00b82 |
525 | # FIXME totally lame |
526 | $meta->add_method('meta' => sub { |
527 | $role->initialize(ref($_[0]) || $_[0]); |
528 | }); |
529 | |
530 | if (exists $options{attributes}) { |
531 | foreach my $attribute_name (keys %{$options{attributes}}) { |
532 | my $attr = $options{attributes}->{$attribute_name}; |
533 | $meta->add_attribute($attribute_name => $attr); |
534 | } |
535 | } |
536 | |
537 | if (exists $options{methods}) { |
538 | foreach my $method_name (keys %{$options{methods}}) { |
539 | $meta->add_method($method_name, $options{methods}->{$method_name}); |
540 | } |
541 | } |
542 | |
d1765290 |
543 | Class::MOP::weaken_metaclass($meta->name) |
544 | if $meta->is_anon_role; |
545 | |
b6a00b82 |
546 | return $meta; |
547 | } |
548 | |
c9ee520d |
549 | # anonymous roles. most of it is copied straight out of Class::MOP::Class. |
550 | # an intrepid hacker might find great riches if he unifies this code with that |
551 | # code in Class::MOP::Module or Class::MOP::Package |
552 | { |
553 | # NOTE: |
554 | # this should be sufficient, if you have a |
555 | # use case where it is not, write a test and |
556 | # I will change it. |
557 | my $ANON_ROLE_SERIAL = 0; |
558 | |
559 | # NOTE: |
560 | # we need a sufficiently annoying prefix |
561 | # this should suffice for now, this is |
562 | # used in a couple of places below, so |
563 | # need to put it up here for now. |
564 | my $ANON_ROLE_PREFIX = 'Moose::Meta::Role::__ANON__::SERIAL::'; |
565 | |
566 | sub is_anon_role { |
567 | my $self = shift; |
568 | no warnings 'uninitialized'; |
569 | $self->name =~ /^$ANON_ROLE_PREFIX/; |
570 | } |
571 | |
572 | sub create_anon_role { |
573 | my ($role, %options) = @_; |
574 | my $package_name = $ANON_ROLE_PREFIX . ++$ANON_ROLE_SERIAL; |
575 | return $role->create($package_name, %options); |
576 | } |
577 | |
578 | # NOTE: |
579 | # this will only get called for |
580 | # anon-roles, all other calls |
581 | # are assumed to occur during |
582 | # global destruction and so don't |
583 | # really need to be handled explicitly |
584 | sub DESTROY { |
585 | my $self = shift; |
586 | |
963c24e1 |
587 | return if in_global_destruction(); # it'll happen soon anyway and this just makes things more complicated |
c9ee520d |
588 | |
589 | no warnings 'uninitialized'; |
590 | return unless $self->name =~ /^$ANON_ROLE_PREFIX/; |
591 | |
592 | # XXX: is this necessary for us? I don't understand what it's doing |
593 | # -sartak |
594 | |
595 | # Moose does a weird thing where it replaces the metaclass for |
596 | # class when fixing metaclass incompatibility. In that case, |
597 | # we don't want to clean out the namespace now. We can detect |
598 | # that because Moose will explicitly update the singleton |
599 | # cache in Class::MOP. |
600 | #my $current_meta = Class::MOP::get_metaclass_by_name($self->name); |
601 | #return if $current_meta ne $self; |
602 | |
603 | my ($serial_id) = ($self->name =~ /^$ANON_ROLE_PREFIX(\d+)/); |
604 | no strict 'refs'; |
605 | foreach my $key (keys %{$ANON_ROLE_PREFIX . $serial_id}) { |
606 | delete ${$ANON_ROLE_PREFIX . $serial_id}{$key}; |
607 | } |
608 | delete ${'main::' . $ANON_ROLE_PREFIX}{$serial_id . '::'}; |
609 | } |
610 | } |
611 | |
fb1e11d5 |
612 | ##################################################################### |
613 | ## NOTE: |
d03bd989 |
614 | ## This is Moose::Meta::Role as defined by Moose (plus the use of |
615 | ## MooseX::AttributeHelpers module). It is here as a reference to |
fb1e11d5 |
616 | ## make it easier to see what is happening above with all the meta |
617 | ## programming. - SL |
618 | ##################################################################### |
619 | # |
620 | # has 'roles' => ( |
621 | # metaclass => 'Collection::Array', |
622 | # reader => 'get_roles', |
623 | # isa => 'ArrayRef[Moose::Meta::Roles]', |
624 | # default => sub { [] }, |
625 | # provides => { |
626 | # 'push' => 'add_role', |
627 | # } |
628 | # ); |
d03bd989 |
629 | # |
fb1e11d5 |
630 | # has 'excluded_roles_map' => ( |
631 | # metaclass => 'Collection::Hash', |
632 | # reader => 'get_excluded_roles_map', |
633 | # isa => 'HashRef[Str]', |
634 | # provides => { |
635 | # # Not exactly set, cause it sets multiple |
636 | # 'set' => 'add_excluded_roles', |
637 | # 'keys' => 'get_excluded_roles_list', |
638 | # 'exists' => 'excludes_role', |
639 | # } |
640 | # ); |
d03bd989 |
641 | # |
fb1e11d5 |
642 | # has 'attribute_map' => ( |
643 | # metaclass => 'Collection::Hash', |
644 | # reader => 'get_attribute_map', |
d03bd989 |
645 | # isa => 'HashRef[Str]', |
fb1e11d5 |
646 | # provides => { |
647 | # # 'set' => 'add_attribute' # has some special crap in it |
648 | # 'get' => 'get_attribute', |
649 | # 'keys' => 'get_attribute_list', |
650 | # 'exists' => 'has_attribute', |
651 | # # Not exactly delete, cause it sets multiple |
d03bd989 |
652 | # 'delete' => 'remove_attribute', |
fb1e11d5 |
653 | # } |
654 | # ); |
d03bd989 |
655 | # |
fb1e11d5 |
656 | # has 'required_methods' => ( |
657 | # metaclass => 'Collection::Hash', |
658 | # reader => 'get_required_methods_map', |
659 | # isa => 'HashRef[Str]', |
d03bd989 |
660 | # provides => { |
661 | # # not exactly set, or delete since it works for multiple |
fb1e11d5 |
662 | # 'set' => 'add_required_methods', |
663 | # 'delete' => 'remove_required_methods', |
664 | # 'keys' => 'get_required_method_list', |
d03bd989 |
665 | # 'exists' => 'requires_method', |
fb1e11d5 |
666 | # } |
667 | # ); |
d03bd989 |
668 | # |
669 | # # the before, around and after modifiers are |
670 | # # HASH keyed by method-name, with ARRAY of |
fb1e11d5 |
671 | # # CODE refs to apply in that order |
d03bd989 |
672 | # |
fb1e11d5 |
673 | # has 'before_method_modifiers' => ( |
d03bd989 |
674 | # metaclass => 'Collection::Hash', |
fb1e11d5 |
675 | # reader => 'get_before_method_modifiers_map', |
676 | # isa => 'HashRef[ArrayRef[CodeRef]]', |
677 | # provides => { |
678 | # 'keys' => 'get_before_method_modifiers', |
d03bd989 |
679 | # 'exists' => 'has_before_method_modifiers', |
680 | # # This actually makes sure there is an |
fb1e11d5 |
681 | # # ARRAY at the given key, and pushed onto |
682 | # # it. It also checks for duplicates as well |
d03bd989 |
683 | # # 'add' => 'add_before_method_modifier' |
684 | # } |
fb1e11d5 |
685 | # ); |
d03bd989 |
686 | # |
fb1e11d5 |
687 | # has 'after_method_modifiers' => ( |
d03bd989 |
688 | # metaclass => 'Collection::Hash', |
fb1e11d5 |
689 | # reader =>'get_after_method_modifiers_map', |
690 | # isa => 'HashRef[ArrayRef[CodeRef]]', |
691 | # provides => { |
692 | # 'keys' => 'get_after_method_modifiers', |
d03bd989 |
693 | # 'exists' => 'has_after_method_modifiers', |
694 | # # This actually makes sure there is an |
fb1e11d5 |
695 | # # ARRAY at the given key, and pushed onto |
d03bd989 |
696 | # # it. It also checks for duplicates as well |
697 | # # 'add' => 'add_after_method_modifier' |
698 | # } |
fb1e11d5 |
699 | # ); |
d03bd989 |
700 | # |
fb1e11d5 |
701 | # has 'around_method_modifiers' => ( |
d03bd989 |
702 | # metaclass => 'Collection::Hash', |
fb1e11d5 |
703 | # reader =>'get_around_method_modifiers_map', |
704 | # isa => 'HashRef[ArrayRef[CodeRef]]', |
705 | # provides => { |
706 | # 'keys' => 'get_around_method_modifiers', |
d03bd989 |
707 | # 'exists' => 'has_around_method_modifiers', |
708 | # # This actually makes sure there is an |
fb1e11d5 |
709 | # # ARRAY at the given key, and pushed onto |
d03bd989 |
710 | # # it. It also checks for duplicates as well |
711 | # # 'add' => 'add_around_method_modifier' |
712 | # } |
fb1e11d5 |
713 | # ); |
d03bd989 |
714 | # |
fb1e11d5 |
715 | # # override is similar to the other modifiers |
716 | # # except that it is not an ARRAY of code refs |
717 | # # but instead just a single name->code mapping |
d03bd989 |
718 | # |
fb1e11d5 |
719 | # has 'override_method_modifiers' => ( |
d03bd989 |
720 | # metaclass => 'Collection::Hash', |
fb1e11d5 |
721 | # reader =>'get_override_method_modifiers_map', |
d03bd989 |
722 | # isa => 'HashRef[CodeRef]', |
fb1e11d5 |
723 | # provides => { |
724 | # 'keys' => 'get_override_method_modifier', |
d03bd989 |
725 | # 'exists' => 'has_override_method_modifier', |
726 | # 'add' => 'add_override_method_modifier', # checks for local method .. |
fb1e11d5 |
727 | # } |
728 | # ); |
d03bd989 |
729 | # |
fb1e11d5 |
730 | ##################################################################### |
731 | |
732 | |
e185c027 |
733 | 1; |
734 | |
735 | __END__ |
736 | |
737 | =pod |
738 | |
739 | =head1 NAME |
740 | |
741 | Moose::Meta::Role - The Moose Role metaclass |
742 | |
743 | =head1 DESCRIPTION |
744 | |
705d4cfd |
745 | This class is a subclass of L<Class::MOP::Module> that provides |
746 | additional Moose-specific functionality. |
747 | |
748 | It's API looks a lot like L<Moose::Meta::Class>, but internally it |
749 | implements many things differently. This may change in the future. |
79592a54 |
750 | |
cf3bb5c5 |
751 | =head1 INHERITANCE |
752 | |
753 | C<Moose::Meta::Role> is a subclass of L<Class::MOP::Module>. |
754 | |
e185c027 |
755 | =head1 METHODS |
756 | |
705d4cfd |
757 | =head2 Construction |
758 | |
e185c027 |
759 | =over 4 |
760 | |
705d4cfd |
761 | =item B<< Moose::Meta::Role->initialize($role_name) >> |
e185c027 |
762 | |
705d4cfd |
763 | This method creates a new role object with the provided name. |
e185c027 |
764 | |
705d4cfd |
765 | =item B<< Moose::Meta::Role->combine( [ $role => { ... } ], [ $role ], ... ) >> |
78cd1d3b |
766 | |
705d4cfd |
767 | This method accepts a list of array references. Each array reference |
768 | should contain a role name as its first element. The second element is |
769 | an optional hash reference. The hash reference can contain C<exclude> |
770 | and C<alias> keys to control how methods are composed from the role. |
e606ae5f |
771 | |
705d4cfd |
772 | The return value is a new L<Moose::Meta::Role::Composite> that |
773 | represents the combined roles. |
db1ab48d |
774 | |
705d4cfd |
775 | =item B<< Moose::Meta::Role->create($name, %options) >> |
e185c027 |
776 | |
705d4cfd |
777 | This method is identical to the L<Moose::Meta::Class> C<create> |
778 | method. |
779 | |
780 | =item B<< Moose::Meta::Role->create_anon_role >> |
e185c027 |
781 | |
705d4cfd |
782 | This method is identical to the L<Moose::Meta::Class> |
783 | C<create_anon_class> method. |
e185c027 |
784 | |
705d4cfd |
785 | =item B<< $metarole->is_anon_role >> |
e185c027 |
786 | |
705d4cfd |
787 | Returns true if the role is an anonymous role. |
e185c027 |
788 | |
789 | =back |
790 | |
705d4cfd |
791 | =head2 Role application |
792 | |
e185c027 |
793 | =over 4 |
794 | |
705d4cfd |
795 | =item B<< $metarole->apply( $thing, @options ) >> |
80572233 |
796 | |
705d4cfd |
797 | This method applies a role to the given C<$thing>. That can be another |
798 | L<Moose::Meta::Role>, object, a L<Moose::Meta::Class> object, or a |
799 | (non-meta) object instance. |
80572233 |
800 | |
705d4cfd |
801 | The options are passed directly to the constructor for the appropriate |
802 | L<Moose::Meta::Role::Application> subclass. |
80572233 |
803 | |
d05235e1 |
804 | Note that this will apply the role even if the C<$thing> in question already |
805 | C<does> this role. L<Moose::Util/does_role> is a convenient wrapper for |
806 | finding out if role application is necessary. |
807 | |
80572233 |
808 | =back |
809 | |
705d4cfd |
810 | =head2 Roles and other roles |
811 | |
80572233 |
812 | =over 4 |
813 | |
705d4cfd |
814 | =item B<< $metarole->get_roles >> |
d79e62fd |
815 | |
705d4cfd |
816 | This returns an array reference of roles which this role does. This |
817 | list may include duplicates. |
d79e62fd |
818 | |
705d4cfd |
819 | =item B<< $metarole->calculate_all_roles >> |
d79e62fd |
820 | |
705d4cfd |
821 | This returns a I<unique> list of all roles that this role does, and |
822 | all the roles that its roles do. |
d79e62fd |
823 | |
705d4cfd |
824 | =item B<< $metarole->does_role($role_name) >> |
2b14ac61 |
825 | |
705d4cfd |
826 | Given a role I<name>, returns true if this role does the given |
827 | role. |
d79e62fd |
828 | |
705d4cfd |
829 | =item B<< $metarole->add_role($role) >> |
d79e62fd |
830 | |
705d4cfd |
831 | Given a L<Moose::Meta::Role> object, this adds the role to the list of |
832 | roles that the role does. |
68efb014 |
833 | |
705d4cfd |
834 | =item B<< $metarole->get_excluded_roles_list >> |
be4427d0 |
835 | |
705d4cfd |
836 | Returns a list of role names which this role excludes. |
e185c027 |
837 | |
705d4cfd |
838 | =item B<< $metarole->excludes_role($role_name) >> |
e185c027 |
839 | |
705d4cfd |
840 | Given a role I<name>, returns true if this role excludes the named |
841 | role. |
e606ae5f |
842 | |
705d4cfd |
843 | =item B<< $metarole->add_excluded_roles(@role_names) >> |
e606ae5f |
844 | |
705d4cfd |
845 | Given one or more role names, adds those roles to the list of excluded |
846 | roles. |
bdabd620 |
847 | |
705d4cfd |
848 | =back |
e185c027 |
849 | |
705d4cfd |
850 | =head2 Methods |
093b12c2 |
851 | |
705d4cfd |
852 | The methods for dealing with a role's methods are all identical in API |
853 | and behavior to the same methods in L<Class::MOP::Class>. |
638853cb |
854 | |
705d4cfd |
855 | =over 4 |
53dd42d8 |
856 | |
705d4cfd |
857 | =item B<< $metarole->method_metaclass >> |
e185c027 |
858 | |
705d4cfd |
859 | Returns the method metaclass name for the role. This defaults to |
860 | L<Moose::Meta::Role::Method>. |
e185c027 |
861 | |
705d4cfd |
862 | =item B<< $metarole->get_method($name) >> |
e185c027 |
863 | |
705d4cfd |
864 | =item B<< $metarole->has_method($name) >> |
e185c027 |
865 | |
705d4cfd |
866 | =item B<< $metarole->add_method( $name, $body ) >> |
e185c027 |
867 | |
705d4cfd |
868 | =item B<< $metarole->get_method_list >> |
e185c027 |
869 | |
705d4cfd |
870 | =item B<< $metarole->get_method_map >> |
e185c027 |
871 | |
705d4cfd |
872 | =item B<< $metarole->find_method_by_name($name) >> |
873 | |
874 | These methods are all identical to the methods of the same name in |
875 | L<Class::MOP::Class> |
e185c027 |
876 | |
877 | =back |
878 | |
705d4cfd |
879 | =head2 Attributes |
880 | |
881 | As with methods, the methods for dealing with a role's attribute are |
882 | all identical in API and behavior to the same methods in |
883 | L<Class::MOP::Class>. |
884 | |
885 | However, attributes stored in this class are I<not> stored as |
886 | objects. Rather, the attribute definition is stored as a hash |
887 | reference. When a role is composed into a class, this hash reference |
888 | is passed directly to the metaclass's C<add_attribute> method. |
889 | |
890 | This is quite likely to change in the future. |
891 | |
e185c027 |
892 | =over 4 |
893 | |
705d4cfd |
894 | =item B<< $metarole->get_attribute($attribute_name) >> |
895 | |
896 | =item B<< $metarole->has_attribute($attribute_name) >> |
1331430a |
897 | |
705d4cfd |
898 | =item B<< $metarole->get_attribute_map >> |
38f1204c |
899 | |
705d4cfd |
900 | =item B<< $metarole->get_attribute_list >> |
1331430a |
901 | |
705d4cfd |
902 | =item B<< $metarole->add_attribute($name, %options) >> |
1331430a |
903 | |
705d4cfd |
904 | =item B<< $metarole->remove_attribute($attribute_name) >> |
1331430a |
905 | |
906 | =back |
907 | |
705d4cfd |
908 | =head2 Required methods |
909 | |
0558683c |
910 | =over 4 |
911 | |
705d4cfd |
912 | =item B<< $metarole->get_required_method_list >> |
0558683c |
913 | |
705d4cfd |
914 | Returns the list of methods required by the role. |
0558683c |
915 | |
705d4cfd |
916 | =item B<< $metarole->requires_method($name) >> |
0558683c |
917 | |
705d4cfd |
918 | Returns true if the role requires the named method. |
0558683c |
919 | |
0ecff16b |
920 | =item B<< $metarole->add_required_methods(@names) >> |
0558683c |
921 | |
b3c1586a |
922 | Adds the named methods to the role's list of required methods. |
0558683c |
923 | |
705d4cfd |
924 | =item B<< $metarole->remove_required_methods(@names) >> |
0558683c |
925 | |
b3c1586a |
926 | Removes the named methods to the role's list of required methods. |
0558683c |
927 | |
705d4cfd |
928 | =back |
929 | |
930 | =head2 Method modifiers |
0558683c |
931 | |
705d4cfd |
932 | These methods act like their counterparts in L<Class::Mop::Class> and |
933 | L<Moose::Meta::Class>. |
934 | |
935 | However, method modifiers are simply stored internally, and are not |
936 | applied until the role itself is applied to a class. |
0558683c |
937 | |
938 | =over 4 |
939 | |
705d4cfd |
940 | =item B<< $metarole->add_after_method_modifier($method_name, $method) >> |
0558683c |
941 | |
705d4cfd |
942 | =item B<< $metarole->add_around_method_modifier($method_name, $method) >> |
0558683c |
943 | |
705d4cfd |
944 | =item B<< $metarole->add_before_method_modifier($method_name, $method) >> |
0558683c |
945 | |
705d4cfd |
946 | =item B<< $metarole->add_override_method_modifier($method_name, $method) >> |
0558683c |
947 | |
705d4cfd |
948 | These methods all add an appropriate modifier to the internal list of |
949 | modifiers. |
0558683c |
950 | |
705d4cfd |
951 | =item B<< $metarole->has_after_method_modifiers >> |
0558683c |
952 | |
705d4cfd |
953 | =item B<< $metarole->has_around_method_modifiers >> |
954 | |
955 | =item B<< $metarole->has_before_method_modifiers >> |
956 | |
957 | =item B<< $metarole->has_override_method_modifier >> |
0558683c |
958 | |
705d4cfd |
959 | Return true if the role has any modifiers of the given type. |
0558683c |
960 | |
705d4cfd |
961 | =item B<< $metarole->get_after_method_modifiers($method_name) >> |
0558683c |
962 | |
705d4cfd |
963 | =item B<< $metarole->get_around_method_modifiers($method_name) >> |
0558683c |
964 | |
705d4cfd |
965 | =item B<< $metarole->get_before_method_modifiers($method_name) >> |
0558683c |
966 | |
705d4cfd |
967 | Given a method name, returns a list of the appropriate modifiers for |
968 | that method. |
969 | |
970 | =item B<< $metarole->get_override_method_modifier($method_name) >> |
971 | |
972 | Given a method name, returns the override method modifier for that |
973 | method, if it has one. |
0558683c |
974 | |
975 | =back |
976 | |
705d4cfd |
977 | =head2 Introspection |
505065c4 |
978 | |
705d4cfd |
979 | =over 4 |
505065c4 |
980 | |
705d4cfd |
981 | =item B<< Moose::Meta::Role->meta >> |
505065c4 |
982 | |
705d4cfd |
983 | This will return a L<Class::MOP::Class> instance for this class. |
505065c4 |
984 | |
985 | =back |
986 | |
e185c027 |
987 | =head1 BUGS |
988 | |
fb1e11d5 |
989 | All complex software has bugs lurking in it, and this module is no |
e185c027 |
990 | exception. If you find a bug please either email me, or add the bug |
991 | to cpan-RT. |
992 | |
993 | =head1 AUTHOR |
994 | |
995 | Stevan Little E<lt>stevan@iinteractive.comE<gt> |
996 | |
997 | =head1 COPYRIGHT AND LICENSE |
998 | |
2840a3b2 |
999 | Copyright 2006-2009 by Infinity Interactive, Inc. |
e185c027 |
1000 | |
1001 | L<http://www.iinteractive.com> |
1002 | |
1003 | This library is free software; you can redistribute it and/or modify |
fb1e11d5 |
1004 | it under the same terms as Perl itself. |
e185c027 |
1005 | |
b8aeb4dc |
1006 | =cut |