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