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