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