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