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