2 package Moose::Meta::Role;
9 use Scalar::Util 'blessed';
11 our $VERSION = '0.10';
12 our $AUTHORITY = 'cpan:STEVAN';
14 use Moose::Meta::Class;
15 use Moose::Meta::Role::Method;
16 use Moose::Meta::Role::Method::Required;
18 use base 'Class::MOP::Module';
22 # I normally don't do this, but I am doing
23 # a whole bunch of meta-programmin in this
24 # module, so it just makes sense.
27 my $META = __PACKAGE__->meta;
29 ## ------------------------------------------------------------------
33 # since roles are lazy, we hold all the attributes
34 # of the individual role in 'statis' until which
35 # time when it is applied to a class. This means
36 # keeping a lot of things in hash maps, so we are
37 # using a little of that meta-programmin' magic
38 # here an saving lots of extra typin.
41 $META->add_attribute($_->{name} => (
42 reader => $_->{reader},
45 { name => 'excluded_roles_map', reader => 'get_excluded_roles_map' },
46 { name => 'attribute_map', reader => 'get_attribute_map' },
47 { name => 'required_methods', reader => 'get_required_methods_map' },
51 # many of these attributes above require similar
52 # functionality to support them, so we again use
53 # the wonders of meta-programmin' to deliver a
54 # very compact solution to this normally verbose
60 attr_reader => 'get_excluded_roles_map' ,
62 add => 'add_excluded_roles',
63 get_list => 'get_excluded_roles_list',
64 existence => 'excludes_role',
68 attr_reader => 'get_required_methods_map',
70 add => 'add_required_methods',
71 remove => 'remove_required_methods',
72 get_list => 'get_required_method_list',
73 existence => 'requires_method',
77 attr_reader => 'get_attribute_map',
79 get => 'get_attribute',
80 get_list => 'get_attribute_list',
81 existence => 'has_attribute',
82 remove => 'remove_attribute',
87 my $attr_reader = $action->{attr_reader};
88 my $methods = $action->{methods};
90 $META->add_method($methods->{add} => sub {
91 my ($self, @values) = @_;
92 $self->$attr_reader->{$_} = undef foreach @values;
93 }) if exists $methods->{add};
95 $META->add_method($methods->{get_list} => sub {
97 keys %{$self->$attr_reader};
98 }) if exists $methods->{get_list};
100 $META->add_method($methods->{get} => sub {
101 my ($self, $name) = @_;
102 $self->$attr_reader->{$name}
103 }) if exists $methods->{get};
105 $META->add_method($methods->{existence} => sub {
106 my ($self, $name) = @_;
107 exists $self->$attr_reader->{$name} ? 1 : 0;
108 }) if exists $methods->{existence};
110 $META->add_method($methods->{remove} => sub {
111 my ($self, @values) = @_;
112 delete $self->$attr_reader->{$_} foreach @values;
113 }) if exists $methods->{remove};
116 ## some things don't always fit, so they go here ...
122 if (scalar @_ == 1 && ref($_[0]) eq 'HASH') {
128 $self->get_attribute_map->{$name} = $attr_desc;
131 sub _clean_up_required_methods {
133 foreach my $method ($self->get_required_method_list) {
134 $self->remove_required_methods($method)
135 if $self->has_method($method);
139 ## ------------------------------------------------------------------
142 $META->add_attribute($_->{name} => (
143 reader => $_->{reader},
144 default => sub { {} }
146 { name => 'before_method_modifiers', reader => 'get_before_method_modifiers_map' },
147 { name => 'after_method_modifiers', reader => 'get_after_method_modifiers_map' },
148 { name => 'around_method_modifiers', reader => 'get_around_method_modifiers_map' },
149 { name => 'override_method_modifiers', reader => 'get_override_method_modifiers_map' },
153 # the before/around/after method modifiers are
154 # stored by name, but there can be many methods
155 # then associated with that name. So again we have
156 # lots of similar functionality, so we can do some
157 # meta-programmin' and save some time.
160 foreach my $modifier_type (qw[ before around after ]) {
162 my $attr_reader = "get_${modifier_type}_method_modifiers_map";
164 $META->add_method("get_${modifier_type}_method_modifiers" => sub {
165 my ($self, $method_name) = @_;
166 @{$self->$attr_reader->{$method_name}};
169 $META->add_method("has_${modifier_type}_method_modifiers" => sub {
170 my ($self, $method_name) = @_;
172 # for now we assume that if it exists,..
173 # it has at least one modifier in it
174 (exists $self->$attr_reader->{$method_name}) ? 1 : 0;
177 $META->add_method("add_${modifier_type}_method_modifier" => sub {
178 my ($self, $method_name, $method) = @_;
180 $self->$attr_reader->{$method_name} = []
181 unless exists $self->$attr_reader->{$method_name};
183 my $modifiers = $self->$attr_reader->{$method_name};
186 # check to see that we aren't adding the
187 # same code twice. We err in favor of the
188 # first on here, this may not be as expected
189 foreach my $modifier (@{$modifiers}) {
190 return if $modifier == $method;
193 push @{$modifiers} => $method;
198 ## ------------------------------------------------------------------
199 ## override method mofidiers
202 # these are a little different because there
203 # can only be one per name, whereas the other
204 # method modifiers can have multiples.
207 sub add_override_method_modifier {
208 my ($self, $method_name, $method) = @_;
209 (!$self->has_method($method_name))
210 || confess "Cannot add an override of method '$method_name' " .
211 "because there is a local version of '$method_name'";
212 $self->get_override_method_modifiers_map->{$method_name} = $method;
215 sub has_override_method_modifier {
216 my ($self, $method_name) = @_;
218 # for now we assume that if it exists,..
219 # it has at least one modifier in it
220 (exists $self->get_override_method_modifiers_map->{$method_name}) ? 1 : 0;
223 sub get_override_method_modifier {
224 my ($self, $method_name) = @_;
225 $self->get_override_method_modifiers_map->{$method_name};
228 ## general list accessor ...
230 sub get_method_modifier_list {
231 my ($self, $modifier_type) = @_;
232 my $accessor = "get_${modifier_type}_method_modifiers_map";
233 keys %{$self->$accessor};
236 ## ------------------------------------------------------------------
239 __PACKAGE__->meta->add_attribute('roles' => (
240 reader => 'get_roles',
241 default => sub { [] }
245 my ($self, $role) = @_;
246 (blessed($role) && $role->isa('Moose::Meta::Role'))
247 || confess "Roles must be instances of Moose::Meta::Role";
248 push @{$self->get_roles} => $role;
251 sub calculate_all_roles {
258 $_->calculate_all_roles
259 } @{ $self->get_roles });
263 my ($self, $role_name) = @_;
265 || confess "You must supply a role name to look for";
266 # if we are it,.. then return true
267 return 1 if $role_name eq $self->name;
268 # otherwise.. check our children
269 foreach my $role (@{$self->get_roles}) {
270 return 1 if $role->does_role($role_name);
275 ## ------------------------------------------------------------------
278 sub method_metaclass { 'Moose::Meta::Role::Method' }
281 # this is an UGLY hack
284 $self->{'%!methods'} ||= {};
285 $self->{'$!_package_cache_flag'} = undef;
286 $self->Moose::Meta::Class::get_method_map()
288 sub reset_package_cache_flag { () }
291 # Yes, this is a really really UGLY hack
292 # but it works, and until I can figure
293 # out a better way, this is gonna be it.
295 sub get_method { (shift)->Moose::Meta::Class::get_method(@_) }
296 sub has_method { (shift)->Moose::Meta::Class::has_method(@_) }
297 sub alias_method { (shift)->Moose::Meta::Class::alias_method(@_) }
298 sub get_method_list {
301 } (shift)->Moose::Meta::Class::get_method_list(@_)
304 sub find_method_by_name { (shift)->get_method(@_) }
306 ## ------------------------------------------------------------------
308 ## ------------------------------------------------------------------
310 my $anon_counter = 0;
313 my ($self, $other) = @_;
315 unless ($other->isa('Moose::Meta::Class') || $other->isa('Moose::Meta::Role')) {
317 # Runtime Role mixins
320 # We really should do this better, and
321 # cache the results of our efforts so
322 # that we don't need to repeat them.
324 my $pkg_name = __PACKAGE__ . "::__RUNTIME_ROLE_ANON_CLASS__::" . $anon_counter++;
325 eval "package " . $pkg_name . "; our \$VERSION = '0.00';";
330 $other = Moose::Meta::Class->initialize($pkg_name);
331 $other->superclasses(blessed($object));
333 bless $object => $pkg_name;
336 $self->_check_excluded_roles($other);
337 $self->_check_required_methods($other);
339 $self->_apply_attributes($other);
340 $self->_apply_methods($other);
343 # we need a clear cache flag too ...
344 $other->{'$!_package_cache_flag'} = undef;
346 $self->_apply_override_method_modifiers($other);
347 $self->_apply_before_method_modifiers($other);
348 $self->_apply_around_method_modifiers($other);
349 $self->_apply_after_method_modifiers($other);
351 $other->add_role($self);
355 my ($class, @roles) = @_;
357 my $pkg_name = __PACKAGE__ . "::__COMPOSITE_ROLE_SANDBOX__::" . $anon_counter++;
358 eval "package " . $pkg_name . "; our \$VERSION = '0.00';";
361 my $combined = $class->initialize($pkg_name);
363 foreach my $role (@roles) {
364 $role->apply($combined);
367 $combined->_clean_up_required_methods;
372 ## ------------------------------------------------------------------
374 ## applying a role to a class ...
376 sub _check_excluded_roles {
377 my ($self, $other) = @_;
378 if ($other->excludes_role($self->name)) {
379 confess "Conflict detected: " . $other->name . " excludes role '" . $self->name . "'";
381 foreach my $excluded_role_name ($self->get_excluded_roles_list) {
382 if ($other->does_role($excluded_role_name)) {
383 confess "The class " . $other->name . " does the excluded role '$excluded_role_name'";
386 if ($other->isa('Moose::Meta::Role')) {
387 $other->add_excluded_roles($excluded_role_name);
389 # else -> ignore it :)
394 sub _check_required_methods {
395 my ($self, $other) = @_;
397 # we might need to move this down below the
398 # the attributes so that we can require any
399 # attribute accessors. However I am thinking
400 # that maybe those are somehow exempt from
401 # the require methods stuff.
402 foreach my $required_method_name ($self->get_required_method_list) {
404 unless ($other->find_method_by_name($required_method_name)) {
405 if ($other->isa('Moose::Meta::Role')) {
406 $other->add_required_methods($required_method_name);
409 confess "'" . $self->name . "' requires the method '$required_method_name' " .
410 "to be implemented by '" . $other->name . "'";
415 # we need to make sure that the method is
416 # not a method modifier, because those do
417 # not satisfy the requirements ...
418 my $method = $other->find_method_by_name($required_method_name);
420 # check if it is a generated accessor ...
421 (!$method->isa('Class::MOP::Method::Accessor'))
422 || confess "'" . $self->name . "' requires the method '$required_method_name' " .
423 "to be implemented by '" . $other->name . "', the method is only an attribute accessor";
426 # All other tests here have been removed, they were tests
427 # for overriden methods and before/after/around modifiers.
428 # But we realized that for classes any overriden or modified
429 # methods would be backed by a real method of that name
430 # (and therefore meet the requirement). And for roles, the
431 # overriden and modified methods are "in statis" and so would
432 # not show up in this test anyway (and as a side-effect they
433 # would not fufill the requirement, which is exactly what we
434 # want them to do anyway).
440 sub _apply_attributes {
441 my ($self, $other) = @_;
442 foreach my $attribute_name ($self->get_attribute_list) {
443 # it if it has one already
444 if ($other->has_attribute($attribute_name) &&
445 # make sure we haven't seen this one already too
446 $other->get_attribute($attribute_name) != $self->get_attribute($attribute_name)) {
447 # see if we are being composed
449 if ($other->isa('Moose::Meta::Role')) {
450 # all attribute conflicts between roles
451 # result in an immediate fatal error
452 confess "Role '" . $self->name . "' has encountered an attribute conflict " .
453 "during composition. This is fatal error and cannot be disambiguated.";
456 # but if this is a class, we
457 # can safely skip adding the
458 # attribute to the class
464 # this is kinda ugly ...
465 if ($other->isa('Moose::Meta::Class')) {
466 $other->_process_attribute(
468 %{$self->get_attribute($attribute_name)}
472 $other->add_attribute(
474 $self->get_attribute($attribute_name)
482 my ($self, $other) = @_;
483 foreach my $method_name ($self->get_method_list) {
484 # it if it has one already
485 if ($other->has_method($method_name) &&
486 # and if they are not the same thing ...
487 $other->get_method($method_name)->body != $self->get_method($method_name)->body) {
488 # see if we are composing into a role
489 if ($other->isa('Moose::Meta::Role')) {
490 # method conflicts between roles result
491 # in the method becoming a requirement
492 $other->add_required_methods($method_name);
494 # we have to remove the method from our
495 # role, if this is being called from combine()
496 # which means the meta is an anon class
497 # this *may* cause problems later, but it
498 # is probably fairly safe to assume that
499 # anon classes will only be used internally
500 # or by people who know what they are doing
501 $other->Moose::Meta::Class::remove_method($method_name)
502 if $other->name =~ /__COMPOSITE_ROLE_SANDBOX__/;
509 # add it, although it could be overriden
510 $other->alias_method(
512 $self->get_method($method_name)
518 sub _apply_override_method_modifiers {
519 my ($self, $other) = @_;
520 foreach my $method_name ($self->get_method_modifier_list('override')) {
521 # it if it has one already then ...
522 if ($other->has_method($method_name)) {
523 # if it is being composed into another role
524 # we have a conflict here, because you cannot
525 # combine an overriden method with a locally
527 if ($other->isa('Moose::Meta::Role')) {
528 confess "Role '" . $self->name . "' has encountered an 'override' method conflict " .
529 "during composition (A local method of the same name as been found). This " .
533 # if it is a class, then we
534 # just ignore this here ...
539 # if no local method is found, then we
540 # must check if we are a role or class
541 if ($other->isa('Moose::Meta::Role')) {
542 # if we are a role, we need to make sure
543 # we dont have a conflict with the role
544 # we are composing into
545 if ($other->has_override_method_modifier($method_name) &&
546 $other->get_override_method_modifier($method_name) != $self->get_override_method_modifier($method_name)) {
547 confess "Role '" . $self->name . "' has encountered an 'override' method conflict " .
548 "during composition (Two 'override' methods of the same name encountered). " .
549 "This is fatal error.";
552 # if there is no conflict,
553 # just add it to the role
554 $other->add_override_method_modifier(
556 $self->get_override_method_modifier($method_name)
561 # if this is not a role, then we need to
562 # find the original package of the method
563 # so that we can tell the class were to
564 # find the right super() method
565 my $method = $self->get_override_method_modifier($method_name);
566 my ($package) = Class::MOP::get_code_info($method);
567 # if it is a class, we just add it
568 $other->add_override_method_modifier($method_name, $method, $package);
574 sub _apply_method_modifiers {
575 my ($self, $modifier_type, $other) = @_;
576 my $add = "add_${modifier_type}_method_modifier";
577 my $get = "get_${modifier_type}_method_modifiers";
578 foreach my $method_name ($self->get_method_modifier_list($modifier_type)) {
582 ) foreach $self->$get($method_name);
586 sub _apply_before_method_modifiers { (shift)->_apply_method_modifiers('before' => @_) }
587 sub _apply_around_method_modifiers { (shift)->_apply_method_modifiers('around' => @_) }
588 sub _apply_after_method_modifiers { (shift)->_apply_method_modifiers('after' => @_) }
598 Moose::Meta::Role - The Moose Role metaclass
602 Please see L<Moose::Role> for more information about roles.
603 For the most part, this has no user-serviceable parts inside
604 this module. It's API is still subject to some change (although
605 probably not that much really).
643 =item B<add_excluded_roles>
645 =item B<excludes_role>
647 =item B<get_excluded_roles_list>
649 =item B<get_excluded_roles_map>
651 =item B<calculate_all_roles>
657 =item B<method_metaclass>
659 =item B<find_method_by_name>
665 =item B<alias_method>
667 =item B<get_method_list>
669 =item B<get_method_map>
671 =item B<reset_package_cache_flag>
677 =item B<add_attribute>
679 =item B<has_attribute>
681 =item B<get_attribute>
683 =item B<get_attribute_list>
685 =item B<get_attribute_map>
687 =item B<remove_attribute>
693 =item B<add_required_methods>
695 =item B<remove_required_methods>
697 =item B<get_required_method_list>
699 =item B<get_required_methods_map>
701 =item B<requires_method>
707 =item B<add_after_method_modifier>
709 =item B<add_around_method_modifier>
711 =item B<add_before_method_modifier>
713 =item B<add_override_method_modifier>
719 =item B<has_after_method_modifiers>
721 =item B<has_around_method_modifiers>
723 =item B<has_before_method_modifiers>
725 =item B<has_override_method_modifier>
731 =item B<get_after_method_modifiers>
733 =item B<get_around_method_modifiers>
735 =item B<get_before_method_modifiers>
737 =item B<get_method_modifier_list>
743 =item B<get_override_method_modifier>
745 =item B<get_after_method_modifiers_map>
747 =item B<get_around_method_modifiers_map>
749 =item B<get_before_method_modifiers_map>
751 =item B<get_override_method_modifiers_map>
757 All complex software has bugs lurking in it, and this module is no
758 exception. If you find a bug please either email me, or add the bug
763 Stevan Little E<lt>stevan@iinteractive.comE<gt>
765 =head1 COPYRIGHT AND LICENSE
767 Copyright 2006, 2007 by Infinity Interactive, Inc.
769 L<http://www.iinteractive.com>
771 This library is free software; you can redistribute it and/or modify
772 it under the same terms as Perl itself.