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