2 package Moose::Meta::Role;
8 use Sub::Name 'subname';
10 use Scalar::Util 'blessed', 'reftype';
12 our $VERSION = '0.12';
13 our $AUTHORITY = 'cpan:STEVAN';
15 use Moose::Meta::Class;
16 use Moose::Meta::Role::Method;
17 use Moose::Meta::Role::Method::Required;
19 use base 'Class::MOP::Module';
21 ## ------------------------------------------------------------------
23 ## I normally don't do this, but I am doing
24 ## a whole bunch of meta-programmin in this
25 ## module, so it just makes sense. For a clearer
26 ## picture of what is going on in the next
27 ## several lines of code, look at the really
28 ## big comment at the end of this file (right
31 ## ------------------------------------------------------------------
33 my $META = __PACKAGE__->meta;
35 ## ------------------------------------------------------------------
39 # since roles are lazy, we hold all the attributes
40 # of the individual role in 'statis' until which
41 # time when it is applied to a class. This means
42 # keeping a lot of things in hash maps, so we are
43 # using a little of that meta-programmin' magic
44 # here an saving lots of extra typin. And since
45 # many of these attributes above require similar
46 # functionality to support them, so we again use
47 # the wonders of meta-programmin' to deliver a
48 # very compact solution to this normally verbose
54 name => 'excluded_roles_map',
55 attr_reader => 'get_excluded_roles_map' ,
57 add => 'add_excluded_roles',
58 get_list => 'get_excluded_roles_list',
59 existence => 'excludes_role',
63 name => 'required_methods',
64 attr_reader => 'get_required_methods_map',
66 add => 'add_required_methods',
67 remove => 'remove_required_methods',
68 get_list => 'get_required_method_list',
69 existence => 'requires_method',
73 name => 'attribute_map',
74 attr_reader => 'get_attribute_map',
76 get => 'get_attribute',
77 get_list => 'get_attribute_list',
78 existence => 'has_attribute',
79 remove => 'remove_attribute',
84 my $attr_reader = $action->{attr_reader};
85 my $methods = $action->{methods};
87 # create the attribute
88 $META->add_attribute($action->{name} => (
89 reader => $attr_reader,
93 # create some helper methods
94 $META->add_method($methods->{add} => sub {
95 my ($self, @values) = @_;
96 $self->$attr_reader->{$_} = undef foreach @values;
97 }) if exists $methods->{add};
99 $META->add_method($methods->{get_list} => sub {
101 keys %{$self->$attr_reader};
102 }) if exists $methods->{get_list};
104 $META->add_method($methods->{get} => sub {
105 my ($self, $name) = @_;
106 $self->$attr_reader->{$name}
107 }) if exists $methods->{get};
109 $META->add_method($methods->{existence} => sub {
110 my ($self, $name) = @_;
111 exists $self->$attr_reader->{$name} ? 1 : 0;
112 }) if exists $methods->{existence};
114 $META->add_method($methods->{remove} => sub {
115 my ($self, @values) = @_;
116 delete $self->$attr_reader->{$_} foreach @values;
117 }) if exists $methods->{remove};
120 ## some things don't always fit, so they go here ...
126 if (scalar @_ == 1 && ref($_[0]) eq 'HASH') {
132 $self->get_attribute_map->{$name} = $attr_desc;
135 sub _clean_up_required_methods {
137 foreach my $method ($self->get_required_method_list) {
138 $self->remove_required_methods($method)
139 if $self->has_method($method);
143 ## ------------------------------------------------------------------
147 # the before/around/after method modifiers are
148 # stored by name, but there can be many methods
149 # then associated with that name. So again we have
150 # lots of similar functionality, so we can do some
151 # meta-programmin' and save some time.
154 foreach my $modifier_type (qw[ before around after ]) {
156 my $attr_reader = "get_${modifier_type}_method_modifiers_map";
158 # create the attribute ...
159 $META->add_attribute("${modifier_type}_method_modifiers" => (
160 reader => $attr_reader,
161 default => sub { {} }
164 # and some helper methods ...
165 $META->add_method("get_${modifier_type}_method_modifiers" => sub {
166 my ($self, $method_name) = @_;
167 #return () unless exists $self->$attr_reader->{$method_name};
168 @{$self->$attr_reader->{$method_name}};
171 $META->add_method("has_${modifier_type}_method_modifiers" => sub {
172 my ($self, $method_name) = @_;
174 # for now we assume that if it exists,..
175 # it has at least one modifier in it
176 (exists $self->$attr_reader->{$method_name}) ? 1 : 0;
179 $META->add_method("add_${modifier_type}_method_modifier" => sub {
180 my ($self, $method_name, $method) = @_;
182 $self->$attr_reader->{$method_name} = []
183 unless exists $self->$attr_reader->{$method_name};
185 my $modifiers = $self->$attr_reader->{$method_name};
188 # check to see that we aren't adding the
189 # same code twice. We err in favor of the
190 # first on here, this may not be as expected
191 foreach my $modifier (@{$modifiers}) {
192 return if $modifier == $method;
195 push @{$modifiers} => $method;
200 ## ------------------------------------------------------------------
201 ## override method mofidiers
203 $META->add_attribute('override_method_modifiers' => (
204 reader => 'get_override_method_modifiers_map',
205 default => sub { {} }
209 # these are a little different because there
210 # can only be one per name, whereas the other
211 # method modifiers can have multiples.
214 sub add_override_method_modifier {
215 my ($self, $method_name, $method) = @_;
216 (!$self->has_method($method_name))
217 || confess "Cannot add an override of method '$method_name' " .
218 "because there is a local version of '$method_name'";
219 $self->get_override_method_modifiers_map->{$method_name} = $method;
222 sub has_override_method_modifier {
223 my ($self, $method_name) = @_;
225 # for now we assume that if it exists,..
226 # it has at least one modifier in it
227 (exists $self->get_override_method_modifiers_map->{$method_name}) ? 1 : 0;
230 sub get_override_method_modifier {
231 my ($self, $method_name) = @_;
232 $self->get_override_method_modifiers_map->{$method_name};
235 ## general list accessor ...
237 sub get_method_modifier_list {
238 my ($self, $modifier_type) = @_;
239 my $accessor = "get_${modifier_type}_method_modifiers_map";
240 keys %{$self->$accessor};
243 ## ------------------------------------------------------------------
246 __PACKAGE__->meta->add_attribute('roles' => (
247 reader => 'get_roles',
248 default => sub { [] }
252 my ($self, $role) = @_;
253 (blessed($role) && $role->isa('Moose::Meta::Role'))
254 || confess "Roles must be instances of Moose::Meta::Role";
255 push @{$self->get_roles} => $role;
258 sub calculate_all_roles {
264 $_->calculate_all_roles
265 } @{ $self->get_roles });
269 my ($self, $role_name) = @_;
271 || confess "You must supply a role name to look for";
272 # if we are it,.. then return true
273 return 1 if $role_name eq $self->name;
274 # otherwise.. check our children
275 foreach my $role (@{$self->get_roles}) {
276 return 1 if $role->does_role($role_name);
281 ## ------------------------------------------------------------------
284 sub method_metaclass { 'Moose::Meta::Role::Method' }
290 my $role_name = $self->name;
291 my $method_metaclass = $self->method_metaclass;
293 foreach my $symbol ($self->list_all_package_symbols('CODE')) {
295 my $code = $self->get_package_symbol('&' . $symbol);
297 my ($pkg, $name) = Class::MOP::get_code_info($code);
299 if ($pkg->can('meta')
301 # we don't know what ->meta we are calling
302 # here, so we need to be careful cause it
303 # just might blow up at us, or just complain
304 # loudly (in the case of Curses.pm) so we
305 # just be a little overly cautious here.
307 && eval { no warnings; blessed($pkg->meta) }
308 && $pkg->meta->isa('Moose::Meta::Role')) {
309 my $role = $pkg->meta->name;
310 next unless $self->does_role($role);
313 next if ($pkg || '') ne $role_name &&
314 ($name || '') ne '__ANON__';
317 $map->{$symbol} = $method_metaclass->wrap($code);
324 my ($self, $name) = @_;
325 $self->get_method_map->{$name}
329 my ($self, $name) = @_;
330 exists $self->get_method_map->{$name} ? 1 : 0
333 sub find_method_by_name { (shift)->get_method(@_) }
335 sub get_method_list {
337 grep { !/^meta$/ } keys %{$self->get_method_map};
341 my ($self, $method_name, $method) = @_;
342 (defined $method_name && $method_name)
343 || confess "You must define a method name";
345 my $body = (blessed($method) ? $method->body : $method);
346 ('CODE' eq (reftype($body) || ''))
347 || confess "Your code block must be a CODE reference";
349 $self->add_package_symbol("&${method_name}" => $body);
352 sub reset_package_cache_flag { () }
353 sub update_package_cache_flag { () }
355 ## ------------------------------------------------------------------
357 ## ------------------------------------------------------------------
359 my $anon_counter = 0;
362 my ($self, $other) = @_;
364 unless ($other->isa('Moose::Meta::Class') || $other->isa('Moose::Meta::Role')) {
366 # Runtime Role mixins
369 # We really should do this better, and
370 # cache the results of our efforts so
371 # that we don't need to repeat them.
373 my $pkg_name = __PACKAGE__ . "::__RUNTIME_ROLE_ANON_CLASS__::" . $anon_counter++;
374 eval "package " . $pkg_name . "; our \$VERSION = '0.00';";
379 $other = Moose::Meta::Class->initialize($pkg_name);
380 $other->superclasses(blessed($object));
382 bless $object => $pkg_name;
385 $self->_check_excluded_roles($other);
386 $self->_check_required_methods($other);
388 $self->_apply_attributes($other);
389 $self->_apply_methods($other);
392 # we need a clear cache flag too ...
393 $other->reset_package_cache_flag;
395 $self->_apply_override_method_modifiers($other);
397 $self->_apply_before_method_modifiers($other);
398 $self->_apply_around_method_modifiers($other);
399 $self->_apply_after_method_modifiers($other);
401 $other->add_role($self);
405 my ($class, @roles) = @_;
407 require Moose::Meta::Role::Application::RoleSummation;
408 require Moose::Meta::Role::Composite;
410 my $c = Moose::Meta::Role::Composite->new(roles => \@roles);
411 Moose::Meta::Role::Application::RoleSummation->new->apply($c);
415 ## ------------------------------------------------------------------
417 ## applying a role to a class ...
419 sub _check_excluded_roles {
420 my ($self, $other) = @_;
421 if ($other->excludes_role($self->name)) {
422 confess "Conflict detected: " . $other->name . " excludes role '" . $self->name . "'";
424 foreach my $excluded_role_name ($self->get_excluded_roles_list) {
425 if ($other->does_role($excluded_role_name)) {
426 confess "The class " . $other->name . " does the excluded role '$excluded_role_name'";
429 if ($other->isa('Moose::Meta::Role')) {
430 $other->add_excluded_roles($excluded_role_name);
432 # else -> ignore it :)
437 sub _check_required_methods {
438 my ($self, $other) = @_;
440 # we might need to move this down below the
441 # the attributes so that we can require any
442 # attribute accessors. However I am thinking
443 # that maybe those are somehow exempt from
444 # the require methods stuff.
445 foreach my $required_method_name ($self->get_required_method_list) {
447 unless ($other->find_method_by_name($required_method_name)) {
448 if ($other->isa('Moose::Meta::Role')) {
449 $other->add_required_methods($required_method_name);
452 confess "'" . $self->name . "' requires the method '$required_method_name' " .
453 "to be implemented by '" . $other->name . "'";
458 # we need to make sure that the method is
459 # not a method modifier, because those do
460 # not satisfy the requirements ...
461 my $method = $other->find_method_by_name($required_method_name);
463 # check if it is a generated accessor ...
464 (!$method->isa('Class::MOP::Method::Accessor'))
465 || confess "'" . $self->name . "' requires the method '$required_method_name' " .
466 "to be implemented by '" . $other->name . "', the method is only an attribute accessor";
469 # All other tests here have been removed, they were tests
470 # for overriden methods and before/after/around modifiers.
471 # But we realized that for classes any overriden or modified
472 # methods would be backed by a real method of that name
473 # (and therefore meet the requirement). And for roles, the
474 # overriden and modified methods are "in statis" and so would
475 # not show up in this test anyway (and as a side-effect they
476 # would not fufill the requirement, which is exactly what we
477 # want them to do anyway).
483 sub _apply_attributes {
484 my ($self, $other) = @_;
485 foreach my $attribute_name ($self->get_attribute_list) {
486 # it if it has one already
487 if ($other->has_attribute($attribute_name) &&
488 # make sure we haven't seen this one already too
489 $other->get_attribute($attribute_name) != $self->get_attribute($attribute_name)) {
490 # see if we are being composed
492 if ($other->isa('Moose::Meta::Role')) {
493 # all attribute conflicts between roles
494 # result in an immediate fatal error
495 confess "Role '" . $self->name . "' has encountered an attribute conflict " .
496 "during composition. This is fatal error and cannot be disambiguated.";
499 # but if this is a class, we
500 # can safely skip adding the
501 # attribute to the class
507 # this is kinda ugly ...
508 if ($other->isa('Moose::Meta::Class')) {
509 $other->_process_attribute(
511 %{$self->get_attribute($attribute_name)}
515 $other->add_attribute(
517 $self->get_attribute($attribute_name)
525 my ($self, $other) = @_;
526 foreach my $method_name ($self->get_method_list) {
527 # it if it has one already
528 if ($other->has_method($method_name) &&
529 # and if they are not the same thing ...
530 $other->get_method($method_name)->body != $self->get_method($method_name)->body) {
531 # see if we are composing into a role
532 if ($other->isa('Moose::Meta::Role')) {
533 # method conflicts between roles result
534 # in the method becoming a requirement
535 $other->add_required_methods($method_name);
537 # we have to remove the method from our
538 # role, if this is being called from combine()
539 # which means the meta is an anon class
540 # this *may* cause problems later, but it
541 # is probably fairly safe to assume that
542 # anon classes will only be used internally
543 # or by people who know what they are doing
544 $other->Moose::Meta::Class::remove_method($method_name)
545 if $other->name =~ /__COMPOSITE_ROLE_SANDBOX__/;
552 # add it, although it could be overriden
553 $other->alias_method(
555 $self->get_method($method_name)
561 sub _apply_override_method_modifiers {
562 my ($self, $other) = @_;
563 foreach my $method_name ($self->get_method_modifier_list('override')) {
564 # it if it has one already then ...
565 if ($other->has_method($method_name)) {
566 # if it is being composed into another role
567 # we have a conflict here, because you cannot
568 # combine an overriden method with a locally
570 if ($other->isa('Moose::Meta::Role')) {
571 confess "Role '" . $self->name . "' has encountered an 'override' method conflict " .
572 "during composition (A local method of the same name as been found). This " .
576 # if it is a class, then we
577 # just ignore this here ...
582 # if no local method is found, then we
583 # must check if we are a role or class
584 if ($other->isa('Moose::Meta::Role')) {
585 # if we are a role, we need to make sure
586 # we dont have a conflict with the role
587 # we are composing into
588 if ($other->has_override_method_modifier($method_name) &&
589 $other->get_override_method_modifier($method_name) != $self->get_override_method_modifier($method_name)) {
590 confess "Role '" . $self->name . "' has encountered an 'override' method conflict " .
591 "during composition (Two 'override' methods of the same name encountered). " .
592 "This is fatal error.";
595 # if there is no conflict,
596 # just add it to the role
597 $other->add_override_method_modifier(
599 $self->get_override_method_modifier($method_name)
604 # if this is not a role, then we need to
605 # find the original package of the method
606 # so that we can tell the class were to
607 # find the right super() method
608 my $method = $self->get_override_method_modifier($method_name);
609 my ($package) = Class::MOP::get_code_info($method);
610 # if it is a class, we just add it
611 $other->add_override_method_modifier($method_name, $method, $package);
617 sub _apply_method_modifiers {
618 my ($self, $modifier_type, $other) = @_;
619 my $add = "add_${modifier_type}_method_modifier";
620 my $get = "get_${modifier_type}_method_modifiers";
621 foreach my $method_name ($self->get_method_modifier_list($modifier_type)) {
625 ) foreach $self->$get($method_name);
629 sub _apply_before_method_modifiers { (shift)->_apply_method_modifiers('before' => @_) }
630 sub _apply_around_method_modifiers { (shift)->_apply_method_modifiers('around' => @_) }
631 sub _apply_after_method_modifiers { (shift)->_apply_method_modifiers('after' => @_) }
633 #####################################################################
635 ## This is Moose::Meta::Role as defined by Moose (plus the use of
636 ## MooseX::AttributeHelpers module). It is here as a reference to
637 ## make it easier to see what is happening above with all the meta
639 #####################################################################
642 # metaclass => 'Collection::Array',
643 # reader => 'get_roles',
644 # isa => 'ArrayRef[Moose::Meta::Roles]',
645 # default => sub { [] },
647 # 'push' => 'add_role',
651 # has 'excluded_roles_map' => (
652 # metaclass => 'Collection::Hash',
653 # reader => 'get_excluded_roles_map',
654 # isa => 'HashRef[Str]',
656 # # Not exactly set, cause it sets multiple
657 # 'set' => 'add_excluded_roles',
658 # 'keys' => 'get_excluded_roles_list',
659 # 'exists' => 'excludes_role',
663 # has 'attribute_map' => (
664 # metaclass => 'Collection::Hash',
665 # reader => 'get_attribute_map',
666 # isa => 'HashRef[Str]',
668 # # 'set' => 'add_attribute' # has some special crap in it
669 # 'get' => 'get_attribute',
670 # 'keys' => 'get_attribute_list',
671 # 'exists' => 'has_attribute',
672 # # Not exactly delete, cause it sets multiple
673 # 'delete' => 'remove_attribute',
677 # has 'required_methods' => (
678 # metaclass => 'Collection::Hash',
679 # reader => 'get_required_methods_map',
680 # isa => 'HashRef[Str]',
682 # # not exactly set, or delete since it works for multiple
683 # 'set' => 'add_required_methods',
684 # 'delete' => 'remove_required_methods',
685 # 'keys' => 'get_required_method_list',
686 # 'exists' => 'requires_method',
690 # # the before, around and after modifiers are
691 # # HASH keyed by method-name, with ARRAY of
692 # # CODE refs to apply in that order
694 # has 'before_method_modifiers' => (
695 # metaclass => 'Collection::Hash',
696 # reader => 'get_before_method_modifiers_map',
697 # isa => 'HashRef[ArrayRef[CodeRef]]',
699 # 'keys' => 'get_before_method_modifiers',
700 # 'exists' => 'has_before_method_modifiers',
701 # # This actually makes sure there is an
702 # # ARRAY at the given key, and pushed onto
703 # # it. It also checks for duplicates as well
704 # # 'add' => 'add_before_method_modifier'
708 # has 'after_method_modifiers' => (
709 # metaclass => 'Collection::Hash',
710 # reader =>'get_after_method_modifiers_map',
711 # isa => 'HashRef[ArrayRef[CodeRef]]',
713 # 'keys' => 'get_after_method_modifiers',
714 # 'exists' => 'has_after_method_modifiers',
715 # # This actually makes sure there is an
716 # # ARRAY at the given key, and pushed onto
717 # # it. It also checks for duplicates as well
718 # # 'add' => 'add_after_method_modifier'
722 # has 'around_method_modifiers' => (
723 # metaclass => 'Collection::Hash',
724 # reader =>'get_around_method_modifiers_map',
725 # isa => 'HashRef[ArrayRef[CodeRef]]',
727 # 'keys' => 'get_around_method_modifiers',
728 # 'exists' => 'has_around_method_modifiers',
729 # # This actually makes sure there is an
730 # # ARRAY at the given key, and pushed onto
731 # # it. It also checks for duplicates as well
732 # # 'add' => 'add_around_method_modifier'
736 # # override is similar to the other modifiers
737 # # except that it is not an ARRAY of code refs
738 # # but instead just a single name->code mapping
740 # has 'override_method_modifiers' => (
741 # metaclass => 'Collection::Hash',
742 # reader =>'get_override_method_modifiers_map',
743 # isa => 'HashRef[CodeRef]',
745 # 'keys' => 'get_override_method_modifier',
746 # 'exists' => 'has_override_method_modifier',
747 # 'add' => 'add_override_method_modifier', # checks for local method ..
751 #####################################################################
762 Moose::Meta::Role - The Moose Role metaclass
766 Please see L<Moose::Role> for more information about roles.
767 For the most part, this has no user-serviceable parts inside
768 this module. It's API is still subject to some change (although
769 probably not that much really).
807 =item B<add_excluded_roles>
809 =item B<excludes_role>
811 =item B<get_excluded_roles_list>
813 =item B<get_excluded_roles_map>
815 =item B<calculate_all_roles>
821 =item B<method_metaclass>
823 =item B<find_method_by_name>
829 =item B<alias_method>
831 =item B<get_method_list>
833 =item B<get_method_map>
835 =item B<update_package_cache_flag>
837 =item B<reset_package_cache_flag>
843 =item B<add_attribute>
845 =item B<has_attribute>
847 =item B<get_attribute>
849 =item B<get_attribute_list>
851 =item B<get_attribute_map>
853 =item B<remove_attribute>
859 =item B<add_required_methods>
861 =item B<remove_required_methods>
863 =item B<get_required_method_list>
865 =item B<get_required_methods_map>
867 =item B<requires_method>
873 =item B<add_after_method_modifier>
875 =item B<add_around_method_modifier>
877 =item B<add_before_method_modifier>
879 =item B<add_override_method_modifier>
885 =item B<has_after_method_modifiers>
887 =item B<has_around_method_modifiers>
889 =item B<has_before_method_modifiers>
891 =item B<has_override_method_modifier>
897 =item B<get_after_method_modifiers>
899 =item B<get_around_method_modifiers>
901 =item B<get_before_method_modifiers>
903 =item B<get_method_modifier_list>
909 =item B<get_override_method_modifier>
911 =item B<get_after_method_modifiers_map>
913 =item B<get_around_method_modifiers_map>
915 =item B<get_before_method_modifiers_map>
917 =item B<get_override_method_modifiers_map>
923 All complex software has bugs lurking in it, and this module is no
924 exception. If you find a bug please either email me, or add the bug
929 Stevan Little E<lt>stevan@iinteractive.comE<gt>
931 =head1 COPYRIGHT AND LICENSE
933 Copyright 2006, 2007 by Infinity Interactive, Inc.
935 L<http://www.iinteractive.com>
937 This library is free software; you can redistribute it and/or modify
938 it under the same terms as Perl itself.