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