2 package Moose::Meta::Role;
9 use Scalar::Util 'blessed';
10 use B 'svref_2object';
12 our $VERSION = '0.09';
13 our $AUTHORITY = 'cpan:STEVAN';
15 use Moose::Meta::Class;
16 use Moose::Meta::Role::Method;
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->Moose::Meta::Class::get_method_map()
289 # Yes, this is a really really UGLY hack
290 # but it works, and until I can figure
291 # out a better way, this is gonna be it.
293 sub get_method { (shift)->Moose::Meta::Class::get_method(@_) }
294 sub has_method { (shift)->Moose::Meta::Class::has_method(@_) }
295 sub alias_method { (shift)->Moose::Meta::Class::alias_method(@_) }
296 sub get_method_list {
299 } (shift)->Moose::Meta::Class::get_method_list(@_)
302 sub find_method_by_name { (shift)->get_method(@_) }
304 ## ------------------------------------------------------------------
306 ## ------------------------------------------------------------------
308 my $anon_counter = 0;
311 my ($self, $other) = @_;
313 unless ($other->isa('Moose::Meta::Class') || $other->isa('Moose::Meta::Role')) {
315 # Runtime Role mixins
318 # We really should do this better, and
319 # cache the results of our efforts so
320 # that we don't need to repeat them.
322 my $pkg_name = __PACKAGE__ . "::__RUNTIME_ROLE_ANON_CLASS__::" . $anon_counter++;
323 eval "package " . $pkg_name . "; our \$VERSION = '0.00';";
328 $other = Moose::Meta::Class->initialize($pkg_name);
329 $other->superclasses(blessed($object));
331 bless $object => $pkg_name;
334 $self->_check_excluded_roles($other);
335 $self->_check_required_methods($other);
337 $self->_apply_attributes($other);
338 $self->_apply_methods($other);
340 $self->_apply_override_method_modifiers($other);
341 $self->_apply_before_method_modifiers($other);
342 $self->_apply_around_method_modifiers($other);
343 $self->_apply_after_method_modifiers($other);
345 $other->add_role($self);
349 my ($class, @roles) = @_;
351 my $pkg_name = __PACKAGE__ . "::__COMPOSITE_ROLE_SANDBOX__::" . $anon_counter++;
352 eval "package " . $pkg_name . "; our \$VERSION = '0.00';";
355 my $combined = $class->initialize($pkg_name);
357 foreach my $role (@roles) {
358 $role->apply($combined);
361 $combined->_clean_up_required_methods;
366 ## ------------------------------------------------------------------
368 ## applying a role to a class ...
370 sub _check_excluded_roles {
371 my ($self, $other) = @_;
372 if ($other->excludes_role($self->name)) {
373 confess "Conflict detected: " . $other->name . " excludes role '" . $self->name . "'";
375 foreach my $excluded_role_name ($self->get_excluded_roles_list) {
376 if ($other->does_role($excluded_role_name)) {
377 confess "The class " . $other->name . " does the excluded role '$excluded_role_name'";
380 if ($other->isa('Moose::Meta::Role')) {
381 $other->add_excluded_roles($excluded_role_name);
383 # else -> ignore it :)
388 sub _check_required_methods {
389 my ($self, $other) = @_;
391 # we might need to move this down below the
392 # the attributes so that we can require any
393 # attribute accessors. However I am thinking
394 # that maybe those are somehow exempt from
395 # the require methods stuff.
396 foreach my $required_method_name ($self->get_required_method_list) {
398 unless ($other->find_method_by_name($required_method_name)) {
399 if ($other->isa('Moose::Meta::Role')) {
400 $other->add_required_methods($required_method_name);
403 confess "'" . $self->name . "' requires the method '$required_method_name' " .
404 "to be implemented by '" . $other->name . "'";
409 # we need to make sure that the method is
410 # not a method modifier, because those do
411 # not satisfy the requirements ...
412 my $method = $other->find_method_by_name($required_method_name);
413 # check if it is an override or a generated accessor ..
414 (!$method->isa('Moose::Meta::Method::Overriden') &&
415 !$method->isa('Class::MOP::Method::Accessor'))
416 || confess "'" . $self->name . "' requires the method '$required_method_name' " .
417 "to be implemented by '" . $other->name . "', the method is only a method modifier";
418 # before/after/around methods are a little trickier
419 # since we wrap the original local method (if applicable)
420 # so we need to check if the original wrapped method is
421 # from the same package, and not a wrap of the super method
422 if ($method->isa('Class::MOP::Method::Wrapped')) {
423 ($method->get_original_method->package_name eq $other->name)
424 || confess "'" . $self->name . "' requires the method '$required_method_name' " .
425 "to be implemented by '" . $other->name . "', the method is only a method modifier";
431 sub _apply_attributes {
432 my ($self, $other) = @_;
433 foreach my $attribute_name ($self->get_attribute_list) {
434 # it if it has one already
435 if ($other->has_attribute($attribute_name) &&
436 # make sure we haven't seen this one already too
437 $other->get_attribute($attribute_name) != $self->get_attribute($attribute_name)) {
438 # see if we are being composed
440 if ($other->isa('Moose::Meta::Role')) {
441 # all attribute conflicts between roles
442 # result in an immediate fatal error
443 confess "Role '" . $self->name . "' has encountered an attribute conflict " .
444 "during composition. This is fatal error and cannot be disambiguated.";
447 # but if this is a class, we
448 # can safely skip adding the
449 # attribute to the class
455 # this is kinda ugly ...
456 if ($other->isa('Moose::Meta::Class')) {
457 $other->_process_attribute(
459 %{$self->get_attribute($attribute_name)}
463 $other->add_attribute(
465 $self->get_attribute($attribute_name)
473 my ($self, $other) = @_;
474 foreach my $method_name ($self->get_method_list) {
475 # it if it has one already
476 if ($other->has_method($method_name) &&
477 # and if they are not the same thing ...
478 $other->get_method($method_name)->body != $self->get_method($method_name)->body) {
479 # see if we are composing into a role
480 if ($other->isa('Moose::Meta::Role')) {
481 # method conflicts between roles result
482 # in the method becoming a requirement
483 $other->add_required_methods($method_name);
485 # we have to remove the method from our
486 # role, if this is being called from combine()
487 # which means the meta is an anon class
488 # this *may* cause problems later, but it
489 # is probably fairly safe to assume that
490 # anon classes will only be used internally
491 # or by people who know what they are doing
492 $other->Moose::Meta::Class::remove_method($method_name)
493 if $other->name =~ /__COMPOSITE_ROLE_SANDBOX__/;
500 # add it, although it could be overriden
501 $other->alias_method(
503 $self->get_method($method_name)
509 sub _apply_override_method_modifiers {
510 my ($self, $other) = @_;
511 foreach my $method_name ($self->get_method_modifier_list('override')) {
512 # it if it has one already then ...
513 if ($other->has_method($method_name)) {
514 # if it is being composed into another role
515 # we have a conflict here, because you cannot
516 # combine an overriden method with a locally
518 if ($other->isa('Moose::Meta::Role')) {
519 confess "Role '" . $self->name . "' has encountered an 'override' method conflict " .
520 "during composition (A local method of the same name as been found). This " .
524 # if it is a class, then we
525 # just ignore this here ...
530 # if no local method is found, then we
531 # must check if we are a role or class
532 if ($other->isa('Moose::Meta::Role')) {
533 # if we are a role, we need to make sure
534 # we dont have a conflict with the role
535 # we are composing into
536 if ($other->has_override_method_modifier($method_name) &&
537 $other->get_override_method_modifier($method_name) != $self->get_override_method_modifier($method_name)) {
538 confess "Role '" . $self->name . "' has encountered an 'override' method conflict " .
539 "during composition (Two 'override' methods of the same name encountered). " .
540 "This is fatal error.";
543 # if there is no conflict,
544 # just add it to the role
545 $other->add_override_method_modifier(
547 $self->get_override_method_modifier($method_name)
552 # if this is not a role, then we need to
553 # find the original package of the method
554 # so that we can tell the class were to
555 # find the right super() method
556 my $method = $self->get_override_method_modifier($method_name);
557 my $package = svref_2object($method)->GV->STASH->NAME;
558 # if it is a class, we just add it
559 $other->add_override_method_modifier($method_name, $method, $package);
565 sub _apply_method_modifiers {
566 my ($self, $modifier_type, $other) = @_;
567 my $add = "add_${modifier_type}_method_modifier";
568 my $get = "get_${modifier_type}_method_modifiers";
569 foreach my $method_name ($self->get_method_modifier_list($modifier_type)) {
573 ) foreach $self->$get($method_name);
577 sub _apply_before_method_modifiers { (shift)->_apply_method_modifiers('before' => @_) }
578 sub _apply_around_method_modifiers { (shift)->_apply_method_modifiers('around' => @_) }
579 sub _apply_after_method_modifiers { (shift)->_apply_method_modifiers('after' => @_) }
589 Moose::Meta::Role - The Moose Role metaclass
593 Please see L<Moose::Role> for more information about roles.
594 For the most part, this has no user-serviceable parts inside
595 this module. It's API is still subject to some change (although
596 probably not that much really).
634 =item B<add_excluded_roles>
636 =item B<excludes_role>
638 =item B<get_excluded_roles_list>
640 =item B<get_excluded_roles_map>
642 =item B<calculate_all_roles>
648 =item B<method_metaclass>
650 =item B<find_method_by_name>
656 =item B<alias_method>
658 =item B<get_method_list>
660 =item B<get_method_map>
666 =item B<add_attribute>
668 =item B<has_attribute>
670 =item B<get_attribute>
672 =item B<get_attribute_list>
674 =item B<get_attribute_map>
676 =item B<remove_attribute>
682 =item B<add_required_methods>
684 =item B<remove_required_methods>
686 =item B<get_required_method_list>
688 =item B<get_required_methods_map>
690 =item B<requires_method>
696 =item B<add_after_method_modifier>
698 =item B<add_around_method_modifier>
700 =item B<add_before_method_modifier>
702 =item B<add_override_method_modifier>
708 =item B<has_after_method_modifiers>
710 =item B<has_around_method_modifiers>
712 =item B<has_before_method_modifiers>
714 =item B<has_override_method_modifier>
720 =item B<get_after_method_modifiers>
722 =item B<get_around_method_modifiers>
724 =item B<get_before_method_modifiers>
726 =item B<get_method_modifier_list>
732 =item B<get_override_method_modifier>
734 =item B<get_after_method_modifiers_map>
736 =item B<get_around_method_modifiers_map>
738 =item B<get_before_method_modifiers_map>
740 =item B<get_override_method_modifiers_map>
746 All complex software has bugs lurking in it, and this module is no
747 exception. If you find a bug please either email me, or add the bug
752 Stevan Little E<lt>stevan@iinteractive.comE<gt>
754 =head1 COPYRIGHT AND LICENSE
756 Copyright 2006, 2007 by Infinity Interactive, Inc.
758 L<http://www.iinteractive.com>
760 This library is free software; you can redistribute it and/or modify
761 it under the same terms as Perl itself.