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