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