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