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