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