2 package Moose::Meta::Role;
9 use Scalar::Util 'blessed';
11 our $VERSION = '0.11';
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->reset_package_cache_flag;
286 $self->Moose::Meta::Class::get_method_map()
288 sub update_package_cache_flag { () }
289 sub reset_package_cache_flag { (shift)->{'$!_package_cache_flag'} = undef; }
292 # Yes, this is a really really UGLY hack
293 # but it works, and until I can figure
294 # out a better way, this is gonna be it.
296 sub get_method { (shift)->Moose::Meta::Class::get_method(@_) }
297 sub has_method { (shift)->Moose::Meta::Class::has_method(@_) }
298 sub alias_method { (shift)->Moose::Meta::Class::alias_method(@_) }
299 sub get_method_list {
302 } (shift)->Moose::Meta::Class::get_method_list(@_)
305 sub find_method_by_name { (shift)->get_method(@_) }
307 ## ------------------------------------------------------------------
309 ## ------------------------------------------------------------------
311 my $anon_counter = 0;
314 my ($self, $other) = @_;
316 unless ($other->isa('Moose::Meta::Class') || $other->isa('Moose::Meta::Role')) {
318 # Runtime Role mixins
321 # We really should do this better, and
322 # cache the results of our efforts so
323 # that we don't need to repeat them.
325 my $pkg_name = __PACKAGE__ . "::__RUNTIME_ROLE_ANON_CLASS__::" . $anon_counter++;
326 eval "package " . $pkg_name . "; our \$VERSION = '0.00';";
331 $other = Moose::Meta::Class->initialize($pkg_name);
332 $other->superclasses(blessed($object));
334 bless $object => $pkg_name;
337 $self->_check_excluded_roles($other);
338 $self->_check_required_methods($other);
340 $self->_apply_attributes($other);
341 $self->_apply_methods($other);
344 # we need a clear cache flag too ...
345 $other->reset_package_cache_flag;
347 $self->_apply_override_method_modifiers($other);
348 $self->_apply_before_method_modifiers($other);
349 $self->_apply_around_method_modifiers($other);
350 $self->_apply_after_method_modifiers($other);
352 $other->add_role($self);
356 my ($class, @roles) = @_;
358 my $pkg_name = __PACKAGE__ . "::__COMPOSITE_ROLE_SANDBOX__::" . $anon_counter++;
359 eval "package " . $pkg_name . "; our \$VERSION = '0.00';";
362 my $combined = $class->initialize($pkg_name);
364 foreach my $role (@roles) {
365 $role->apply($combined);
368 $combined->_clean_up_required_methods;
373 ## ------------------------------------------------------------------
375 ## applying a role to a class ...
377 sub _check_excluded_roles {
378 my ($self, $other) = @_;
379 if ($other->excludes_role($self->name)) {
380 confess "Conflict detected: " . $other->name . " excludes role '" . $self->name . "'";
382 foreach my $excluded_role_name ($self->get_excluded_roles_list) {
383 if ($other->does_role($excluded_role_name)) {
384 confess "The class " . $other->name . " does the excluded role '$excluded_role_name'";
387 if ($other->isa('Moose::Meta::Role')) {
388 $other->add_excluded_roles($excluded_role_name);
390 # else -> ignore it :)
395 sub _check_required_methods {
396 my ($self, $other) = @_;
398 # we might need to move this down below the
399 # the attributes so that we can require any
400 # attribute accessors. However I am thinking
401 # that maybe those are somehow exempt from
402 # the require methods stuff.
403 foreach my $required_method_name ($self->get_required_method_list) {
405 unless ($other->find_method_by_name($required_method_name)) {
406 if ($other->isa('Moose::Meta::Role')) {
407 $other->add_required_methods($required_method_name);
410 confess "'" . $self->name . "' requires the method '$required_method_name' " .
411 "to be implemented by '" . $other->name . "'";
416 # we need to make sure that the method is
417 # not a method modifier, because those do
418 # not satisfy the requirements ...
419 my $method = $other->find_method_by_name($required_method_name);
421 # check if it is a generated accessor ...
422 (!$method->isa('Class::MOP::Method::Accessor'))
423 || confess "'" . $self->name . "' requires the method '$required_method_name' " .
424 "to be implemented by '" . $other->name . "', the method is only an attribute accessor";
427 # All other tests here have been removed, they were tests
428 # for overriden methods and before/after/around modifiers.
429 # But we realized that for classes any overriden or modified
430 # methods would be backed by a real method of that name
431 # (and therefore meet the requirement). And for roles, the
432 # overriden and modified methods are "in statis" and so would
433 # not show up in this test anyway (and as a side-effect they
434 # would not fufill the requirement, which is exactly what we
435 # want them to do anyway).
441 sub _apply_attributes {
442 my ($self, $other) = @_;
443 foreach my $attribute_name ($self->get_attribute_list) {
444 # it if it has one already
445 if ($other->has_attribute($attribute_name) &&
446 # make sure we haven't seen this one already too
447 $other->get_attribute($attribute_name) != $self->get_attribute($attribute_name)) {
448 # see if we are being composed
450 if ($other->isa('Moose::Meta::Role')) {
451 # all attribute conflicts between roles
452 # result in an immediate fatal error
453 confess "Role '" . $self->name . "' has encountered an attribute conflict " .
454 "during composition. This is fatal error and cannot be disambiguated.";
457 # but if this is a class, we
458 # can safely skip adding the
459 # attribute to the class
465 # this is kinda ugly ...
466 if ($other->isa('Moose::Meta::Class')) {
467 $other->_process_attribute(
469 %{$self->get_attribute($attribute_name)}
473 $other->add_attribute(
475 $self->get_attribute($attribute_name)
483 my ($self, $other) = @_;
484 foreach my $method_name ($self->get_method_list) {
485 # it if it has one already
486 if ($other->has_method($method_name) &&
487 # and if they are not the same thing ...
488 $other->get_method($method_name)->body != $self->get_method($method_name)->body) {
489 # see if we are composing into a role
490 if ($other->isa('Moose::Meta::Role')) {
491 # method conflicts between roles result
492 # in the method becoming a requirement
493 $other->add_required_methods($method_name);
495 # we have to remove the method from our
496 # role, if this is being called from combine()
497 # which means the meta is an anon class
498 # this *may* cause problems later, but it
499 # is probably fairly safe to assume that
500 # anon classes will only be used internally
501 # or by people who know what they are doing
502 $other->Moose::Meta::Class::remove_method($method_name)
503 if $other->name =~ /__COMPOSITE_ROLE_SANDBOX__/;
510 # add it, although it could be overriden
511 $other->alias_method(
513 $self->get_method($method_name)
519 sub _apply_override_method_modifiers {
520 my ($self, $other) = @_;
521 foreach my $method_name ($self->get_method_modifier_list('override')) {
522 # it if it has one already then ...
523 if ($other->has_method($method_name)) {
524 # if it is being composed into another role
525 # we have a conflict here, because you cannot
526 # combine an overriden method with a locally
528 if ($other->isa('Moose::Meta::Role')) {
529 confess "Role '" . $self->name . "' has encountered an 'override' method conflict " .
530 "during composition (A local method of the same name as been found). This " .
534 # if it is a class, then we
535 # just ignore this here ...
540 # if no local method is found, then we
541 # must check if we are a role or class
542 if ($other->isa('Moose::Meta::Role')) {
543 # if we are a role, we need to make sure
544 # we dont have a conflict with the role
545 # we are composing into
546 if ($other->has_override_method_modifier($method_name) &&
547 $other->get_override_method_modifier($method_name) != $self->get_override_method_modifier($method_name)) {
548 confess "Role '" . $self->name . "' has encountered an 'override' method conflict " .
549 "during composition (Two 'override' methods of the same name encountered). " .
550 "This is fatal error.";
553 # if there is no conflict,
554 # just add it to the role
555 $other->add_override_method_modifier(
557 $self->get_override_method_modifier($method_name)
562 # if this is not a role, then we need to
563 # find the original package of the method
564 # so that we can tell the class were to
565 # find the right super() method
566 my $method = $self->get_override_method_modifier($method_name);
567 my ($package) = Class::MOP::get_code_info($method);
568 # if it is a class, we just add it
569 $other->add_override_method_modifier($method_name, $method, $package);
575 sub _apply_method_modifiers {
576 my ($self, $modifier_type, $other) = @_;
577 my $add = "add_${modifier_type}_method_modifier";
578 my $get = "get_${modifier_type}_method_modifiers";
579 foreach my $method_name ($self->get_method_modifier_list($modifier_type)) {
583 ) foreach $self->$get($method_name);
587 sub _apply_before_method_modifiers { (shift)->_apply_method_modifiers('before' => @_) }
588 sub _apply_around_method_modifiers { (shift)->_apply_method_modifiers('around' => @_) }
589 sub _apply_after_method_modifiers { (shift)->_apply_method_modifiers('after' => @_) }
599 Moose::Meta::Role - The Moose Role metaclass
603 Please see L<Moose::Role> for more information about roles.
604 For the most part, this has no user-serviceable parts inside
605 this module. It's API is still subject to some change (although
606 probably not that much really).
644 =item B<add_excluded_roles>
646 =item B<excludes_role>
648 =item B<get_excluded_roles_list>
650 =item B<get_excluded_roles_map>
652 =item B<calculate_all_roles>
658 =item B<method_metaclass>
660 =item B<find_method_by_name>
666 =item B<alias_method>
668 =item B<get_method_list>
670 =item B<get_method_map>
672 =item B<update_package_cache_flag>
674 =item B<reset_package_cache_flag>
680 =item B<add_attribute>
682 =item B<has_attribute>
684 =item B<get_attribute>
686 =item B<get_attribute_list>
688 =item B<get_attribute_map>
690 =item B<remove_attribute>
696 =item B<add_required_methods>
698 =item B<remove_required_methods>
700 =item B<get_required_method_list>
702 =item B<get_required_methods_map>
704 =item B<requires_method>
710 =item B<add_after_method_modifier>
712 =item B<add_around_method_modifier>
714 =item B<add_before_method_modifier>
716 =item B<add_override_method_modifier>
722 =item B<has_after_method_modifiers>
724 =item B<has_around_method_modifiers>
726 =item B<has_before_method_modifiers>
728 =item B<has_override_method_modifier>
734 =item B<get_after_method_modifiers>
736 =item B<get_around_method_modifiers>
738 =item B<get_before_method_modifiers>
740 =item B<get_method_modifier_list>
746 =item B<get_override_method_modifier>
748 =item B<get_after_method_modifiers_map>
750 =item B<get_around_method_modifiers_map>
752 =item B<get_before_method_modifiers_map>
754 =item B<get_override_method_modifiers_map>
760 All complex software has bugs lurking in it, and this module is no
761 exception. If you find a bug please either email me, or add the bug
766 Stevan Little E<lt>stevan@iinteractive.comE<gt>
768 =head1 COPYRIGHT AND LICENSE
770 Copyright 2006, 2007 by Infinity Interactive, Inc.
772 L<http://www.iinteractive.com>
774 This library is free software; you can redistribute it and/or modify
775 it under the same terms as Perl itself.