roles don't have throw_error
[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 use Moose::Meta::Class;
13 use Moose::Meta::Role::Attribute;
14 use Moose::Meta::Role::Method;
15 use Moose::Meta::Role::Method::Required;
16 use Moose::Meta::Role::Method::Conflicting;
17 use Moose::Meta::Method::Meta;
18 use Moose::Util qw( ensure_all_roles );
19 use Class::MOP::MiniTrait;
20
21 use base 'Class::MOP::Module',
22          'Class::MOP::Mixin::HasAttributes',
23          'Class::MOP::Mixin::HasMethods';
24
25 Class::MOP::MiniTrait::apply(__PACKAGE__, 'Moose::Meta::Object::Trait');
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 $META->add_attribute(
157     'applied_attribute_metaclass',
158     reader  => 'applied_attribute_metaclass',
159     default => 'Moose::Meta::Attribute',
160 );
161
162 # More or less copied from Moose::Meta::Class
163 sub initialize {
164     my $class = shift;
165     my @args = @_;
166     unshift @args, 'package' if @args % 2;
167     my %opts = @args;
168     my $package = delete $opts{package};
169     return Class::MOP::get_metaclass_by_name($package)
170         || $class->SUPER::initialize($package,
171                 'attribute_metaclass' => 'Moose::Meta::Role::Attribute',
172                 %opts,
173             );
174 }
175
176 sub reinitialize {
177     my $self = shift;
178     my $pkg  = shift;
179
180     my $meta = blessed $pkg ? $pkg : Class::MOP::class_of($pkg);
181
182     my %existing_classes;
183     if ($meta) {
184         %existing_classes = map { $_ => $meta->$_() } qw(
185             attribute_metaclass
186             method_metaclass
187             wrapped_method_metaclass
188             required_method_metaclass
189             conflicting_method_metaclass
190             application_to_class_class
191             application_to_role_class
192             application_to_instance_class
193             applied_attribute_metaclass
194         );
195     }
196
197     my %options = @_;
198     $options{weaken} = Class::MOP::metaclass_is_weak($meta->name)
199         if !exists $options{weaken}
200         && blessed($meta)
201         && $meta->isa('Moose::Meta::Role');
202
203     # don't need to remove generated metaobjects here yet, since we don't
204     # yet generate anything in roles. this may change in the future though...
205     # keep an eye on that
206     my $new_meta = $self->SUPER::reinitialize(
207         $pkg,
208         %existing_classes,
209         %options,
210     );
211     $new_meta->_restore_metaobjects_from($meta)
212         if $meta && $meta->isa('Moose::Meta::Role');
213     return $new_meta;
214 }
215
216 sub _restore_metaobjects_from {
217     my $self = shift;
218     my ($old_meta) = @_;
219
220     $self->_restore_metamethods_from($old_meta);
221     $self->_restore_metaattributes_from($old_meta);
222
223     for my $role ( @{ $old_meta->get_roles } ) {
224         $self->add_role($role);
225     }
226 }
227
228 sub add_attribute {
229     my $self = shift;
230
231     if (blessed $_[0] && ! $_[0]->isa('Moose::Meta::Role::Attribute') ) {
232         my $class = ref $_[0];
233         Moose->throw_error( "Cannot add a $class as an attribute to a role" );
234     }
235     elsif (!blessed($_[0]) && defined($_[0]) && $_[0] =~ /^\+(.*)/) {
236         Moose->throw_error( "has '+attr' is not supported in roles" );
237     }
238
239     return $self->SUPER::add_attribute(@_);
240 }
241
242 sub _attach_attribute {
243     my ( $self, $attribute ) = @_;
244
245     $attribute->attach_to_role($self);
246 }
247
248 sub add_required_methods {
249     my $self = shift;
250
251     for (@_) {
252         my $method = $_;
253         if (!blessed($method)) {
254             $method = $self->required_method_metaclass->new(
255                 name => $method,
256             );
257         }
258         $self->get_required_methods_map->{$method->name} = $method;
259     }
260 }
261
262 sub add_conflicting_method {
263     my $self = shift;
264
265     my $method;
266     if (@_ == 1 && blessed($_[0])) {
267         $method = shift;
268     }
269     else {
270         $method = $self->conflicting_method_metaclass->new(@_);
271     }
272
273     $self->add_required_methods($method);
274 }
275
276 ## ------------------------------------------------------------------
277 ## method modifiers
278
279 # NOTE:
280 # the before/around/after method modifiers are
281 # stored by name, but there can be many methods
282 # then associated with that name. So again we have
283 # lots of similar functionality, so we can do some
284 # meta-programmin' and save some time.
285 # - SL
286
287 foreach my $modifier_type (qw[ before around after ]) {
288
289     my $attr_reader = "get_${modifier_type}_method_modifiers_map";
290
291     # create the attribute ...
292     $META->add_attribute("${modifier_type}_method_modifiers" => (
293         reader  => $attr_reader,
294         default => sub { {} }
295     ));
296
297     # and some helper methods ...
298     $META->add_method("get_${modifier_type}_method_modifiers" => sub {
299         my ($self, $method_name) = @_;
300         #return () unless exists $self->$attr_reader->{$method_name};
301         my $mm = $self->$attr_reader->{$method_name};
302         $mm ? @$mm : ();
303     });
304
305     $META->add_method("has_${modifier_type}_method_modifiers" => sub {
306         my ($self, $method_name) = @_;
307         # NOTE:
308         # for now we assume that if it exists,..
309         # it has at least one modifier in it
310         (exists $self->$attr_reader->{$method_name}) ? 1 : 0;
311     });
312
313     $META->add_method("add_${modifier_type}_method_modifier" => sub {
314         my ($self, $method_name, $method) = @_;
315
316         $self->$attr_reader->{$method_name} = []
317             unless exists $self->$attr_reader->{$method_name};
318
319         my $modifiers = $self->$attr_reader->{$method_name};
320
321         # NOTE:
322         # check to see that we aren't adding the
323         # same code twice. We err in favor of the
324         # first on here, this may not be as expected
325         foreach my $modifier (@{$modifiers}) {
326             return if $modifier == $method;
327         }
328
329         push @{$modifiers} => $method;
330     });
331
332 }
333
334 ## ------------------------------------------------------------------
335 ## override method mofidiers
336
337 $META->add_attribute('override_method_modifiers' => (
338     reader  => 'get_override_method_modifiers_map',
339     default => sub { {} }
340 ));
341
342 # NOTE:
343 # these are a little different because there
344 # can only be one per name, whereas the other
345 # method modifiers can have multiples.
346 # - SL
347
348 sub add_override_method_modifier {
349     my ($self, $method_name, $method) = @_;
350     (!$self->has_method($method_name))
351         || Moose->throw_error("Cannot add an override of method '$method_name' " .
352                    "because there is a local version of '$method_name'");
353     $self->get_override_method_modifiers_map->{$method_name} = $method;
354 }
355
356 sub has_override_method_modifier {
357     my ($self, $method_name) = @_;
358     # NOTE:
359     # for now we assume that if it exists,..
360     # it has at least one modifier in it
361     (exists $self->get_override_method_modifiers_map->{$method_name}) ? 1 : 0;
362 }
363
364 sub get_override_method_modifier {
365     my ($self, $method_name) = @_;
366     $self->get_override_method_modifiers_map->{$method_name};
367 }
368
369 ## general list accessor ...
370
371 sub get_method_modifier_list {
372     my ($self, $modifier_type) = @_;
373     my $accessor = "get_${modifier_type}_method_modifiers_map";
374     keys %{$self->$accessor};
375 }
376
377 sub _meta_method_class { 'Moose::Meta::Method::Meta' }
378
379 ## ------------------------------------------------------------------
380 ## subroles
381
382 $META->add_attribute('roles' => (
383     reader  => 'get_roles',
384     default => sub { [] }
385 ));
386
387 sub add_role {
388     my ($self, $role) = @_;
389     (blessed($role) && $role->isa('Moose::Meta::Role'))
390         || Moose->throw_error("Roles must be instances of Moose::Meta::Role");
391     push @{$self->get_roles} => $role;
392     $self->reset_package_cache_flag;
393 }
394
395 sub calculate_all_roles {
396     my $self = shift;
397     my %seen;
398     grep {
399         !$seen{$_->name}++
400     } ($self, map {
401                   $_->calculate_all_roles
402               } @{ $self->get_roles });
403 }
404
405 sub does_role {
406     my ($self, $role) = @_;
407     (defined $role)
408         || Moose->throw_error("You must supply a role name to look for");
409     my $role_name = blessed $role ? $role->name : $role;
410     # if we are it,.. then return true
411     return 1 if $role_name eq $self->name;
412     # otherwise.. check our children
413     foreach my $role (@{$self->get_roles}) {
414         return 1 if $role->does_role($role_name);
415     }
416     return 0;
417 }
418
419 sub find_method_by_name { (shift)->get_method(@_) }
420
421 ## ------------------------------------------------------------------
422 ## role construction
423 ## ------------------------------------------------------------------
424
425 sub apply {
426     my ($self, $other, %args) = @_;
427
428     (blessed($other))
429         || Moose->throw_error("You must pass in an blessed instance");
430
431     my $application_class;
432     if ($other->isa('Moose::Meta::Role')) {
433         $application_class = $self->application_to_role_class;
434     }
435     elsif ($other->isa('Moose::Meta::Class')) {
436         $application_class = $self->application_to_class_class;
437     }
438     else {
439         $application_class = $self->application_to_instance_class;
440     }
441
442     Class::MOP::load_class($application_class);
443
444     my $deprecation_check = 0;
445
446     if ( exists $args{excludes} && !exists $args{'-excludes'} ) {
447         $args{'-excludes'} = delete $args{excludes};
448         $deprecation_check = 1;
449     }
450     if ( exists $args{alias} && !exists $args{'-alias'} ) {
451         $args{'-alias'} = delete $args{alias};
452         $deprecation_check = 1;
453     }
454
455     if ( $deprecation_check ) {
456         Moose::Deprecated::deprecated(
457             feature => 'alias or excludes',
458             message =>
459                 'The alias and excludes options for role application'.
460                 ' have been renamed -alias and -excludes'.
461                 " (${\$other->name} is consuming ${\$self->name}".
462                 " - do you need to upgrade ${\$other->name}?).".
463                 ' This will be an error in Moose 2.0200.'
464         );
465     }
466
467     if ( exists $args{'-excludes'} ) {
468         # I wish we had coercion here :)
469         $args{'-excludes'} = (
470             ref $args{'-excludes'} eq 'ARRAY'
471             ? $args{'-excludes'}
472             : [ $args{'-excludes'} ]
473         );
474     }
475
476     return $application_class->new(%args)->apply($self, $other, \%args);
477 }
478
479 sub composition_class_roles { }
480
481 sub combine {
482     my ($class, @role_specs) = @_;
483
484     require Moose::Meta::Role::Composite;
485
486     my (@roles, %role_params);
487     while (@role_specs) {
488         my ($role, $params) = @{ splice @role_specs, 0, 1 };
489         my $requested_role
490             = blessed $role
491             ? $role
492             : Class::MOP::class_of($role);
493
494         my $actual_role = $requested_role->_role_for_combination($params);
495         push @roles => $actual_role;
496
497         next unless defined $params;
498         $role_params{$actual_role->name} = $params;
499     }
500
501     my $c = Moose::Meta::Role::Composite->new(roles => \@roles);
502     return $c->apply_params(\%role_params);
503 }
504
505 sub _role_for_combination {
506     my ($self, $params) = @_;
507     return $self;
508 }
509
510 sub create {
511     my $class = shift;
512     my @args = @_;
513
514     unshift @args, 'package' if @args % 2 == 1;
515     my %options = @args;
516
517     (ref $options{attributes} eq 'HASH')
518         || confess "You must pass a HASH ref of attributes"
519             if exists $options{attributes};
520
521     (ref $options{methods} eq 'HASH')
522         || confess "You must pass a HASH ref of methods"
523             if exists $options{methods};
524
525     (ref $options{roles} eq 'ARRAY')
526         || confess "You must pass an ARRAY ref of roles"
527             if exists $options{roles};
528
529     my $package      = delete $options{package};
530     my $roles        = delete $options{roles};
531     my $attributes   = delete $options{attributes};
532     my $methods      = delete $options{methods};
533     my $meta_name    = exists $options{meta_name}
534                          ? delete $options{meta_name}
535                          : 'meta';
536
537     my $meta = $class->SUPER::create($package => %options);
538
539     $meta->_add_meta_method($meta_name)
540         if defined $meta_name;
541
542     if (defined $attributes) {
543         foreach my $attribute_name (keys %{$attributes}) {
544             my $attr = $attributes->{$attribute_name};
545             $meta->add_attribute(
546                 $attribute_name => blessed $attr ? $attr : %{$attr} );
547         }
548     }
549
550     if (defined $methods) {
551         foreach my $method_name (keys %{$methods}) {
552             $meta->add_method($method_name, $methods->{$method_name});
553         }
554     }
555
556     if ($roles) {
557         Moose::Util::apply_all_roles($meta, @$roles);
558     }
559
560     return $meta;
561 }
562
563 sub consumers {
564     my $self = shift;
565     my @consumers;
566     for my $meta (Class::MOP::get_all_metaclass_instances) {
567         next if $meta->name eq $self->name;
568         next unless $meta->isa('Moose::Meta::Class')
569                  || $meta->isa('Moose::Meta::Role');
570         push @consumers, $meta->name
571             if $meta->does_role($self->name);
572     }
573     return @consumers;
574 }
575
576 # XXX: something more intelligent here?
577 sub _anon_package_prefix { 'Moose::Meta::Role::__ANON__::SERIAL::' }
578
579 sub create_anon_role { shift->create_anon(@_) }
580 sub is_anon_role     { shift->is_anon(@_)     }
581
582 sub _anon_cache_key {
583     my $class = shift;
584     my %options = @_;
585     # Makes something like Role|Role::1
586     return join '=' => (
587         join( '|', sort @{ $options{roles} || [] } ),
588     );
589 }
590
591 #####################################################################
592 ## NOTE:
593 ## This is Moose::Meta::Role as defined by Moose (plus the use of
594 ## MooseX::AttributeHelpers module). It is here as a reference to
595 ## make it easier to see what is happening above with all the meta
596 ## programming. - SL
597 #####################################################################
598 #
599 # has 'roles' => (
600 #     metaclass => 'Array',
601 #     reader    => 'get_roles',
602 #     isa       => 'ArrayRef[Moose::Meta::Role]',
603 #     default   => sub { [] },
604 #     provides  => {
605 #         'push' => 'add_role',
606 #     }
607 # );
608 #
609 # has 'excluded_roles_map' => (
610 #     metaclass => 'Hash',
611 #     reader    => 'get_excluded_roles_map',
612 #     isa       => 'HashRef[Str]',
613 #     provides  => {
614 #         # Not exactly set, cause it sets multiple
615 #         'set'    => 'add_excluded_roles',
616 #         'keys'   => 'get_excluded_roles_list',
617 #         'exists' => 'excludes_role',
618 #     }
619 # );
620 #
621 # has 'required_methods' => (
622 #     metaclass => 'Hash',
623 #     reader    => 'get_required_methods_map',
624 #     isa       => 'HashRef[Moose::Meta::Role::Method::Required]',
625 #     provides  => {
626 #         # not exactly set, or delete since it works for multiple
627 #         'set'    => 'add_required_methods',
628 #         'delete' => 'remove_required_methods',
629 #         'keys'   => 'get_required_method_list',
630 #         'exists' => 'requires_method',
631 #     }
632 # );
633 #
634 # # the before, around and after modifiers are
635 # # HASH keyed by method-name, with ARRAY of
636 # # CODE refs to apply in that order
637 #
638 # has 'before_method_modifiers' => (
639 #     metaclass => 'Hash',
640 #     reader    => 'get_before_method_modifiers_map',
641 #     isa       => 'HashRef[ArrayRef[CodeRef]]',
642 #     provides  => {
643 #         'keys'   => 'get_before_method_modifiers',
644 #         'exists' => 'has_before_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_before_method_modifier'
649 #     }
650 # );
651 #
652 # has 'after_method_modifiers' => (
653 #     metaclass => 'Hash',
654 #     reader    =>'get_after_method_modifiers_map',
655 #     isa       => 'HashRef[ArrayRef[CodeRef]]',
656 #     provides  => {
657 #         'keys'   => 'get_after_method_modifiers',
658 #         'exists' => 'has_after_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_after_method_modifier'
663 #     }
664 # );
665 #
666 # has 'around_method_modifiers' => (
667 #     metaclass => 'Hash',
668 #     reader    =>'get_around_method_modifiers_map',
669 #     isa       => 'HashRef[ArrayRef[CodeRef]]',
670 #     provides  => {
671 #         'keys'   => 'get_around_method_modifiers',
672 #         'exists' => 'has_around_method_modifiers',
673 #         # This actually makes sure there is an
674 #         # ARRAY at the given key, and pushed onto
675 #         # it. It also checks for duplicates as well
676 #         # 'add'  => 'add_around_method_modifier'
677 #     }
678 # );
679 #
680 # # override is similar to the other modifiers
681 # # except that it is not an ARRAY of code refs
682 # # but instead just a single name->code mapping
683 #
684 # has 'override_method_modifiers' => (
685 #     metaclass => 'Hash',
686 #     reader    =>'get_override_method_modifiers_map',
687 #     isa       => 'HashRef[CodeRef]',
688 #     provides  => {
689 #         'keys'   => 'get_override_method_modifier',
690 #         'exists' => 'has_override_method_modifier',
691 #         'add'    => 'add_override_method_modifier', # checks for local method ..
692 #     }
693 # );
694 #
695 #####################################################################
696
697
698 1;
699
700 # ABSTRACT: The Moose Role metaclass
701
702 __END__
703
704 =pod
705
706 =head1 DESCRIPTION
707
708 This class is a subclass of L<Class::MOP::Module> that provides
709 additional Moose-specific functionality.
710
711 It's API looks a lot like L<Moose::Meta::Class>, but internally it
712 implements many things differently. This may change in the future.
713
714 =head1 INHERITANCE
715
716 C<Moose::Meta::Role> is a subclass of L<Class::MOP::Module>.
717
718 =head1 METHODS
719
720 =head2 Construction
721
722 =over 4
723
724 =item B<< Moose::Meta::Role->initialize($role_name) >>
725
726 This method creates a new role object with the provided name.
727
728 =item B<< Moose::Meta::Role->combine( [ $role => { ... } ], [ $role ], ... ) >>
729
730 This method accepts a list of array references. Each array reference
731 should contain a role name or L<Moose::Meta::Role> object as its first element. The second element is
732 an optional hash reference. The hash reference can contain C<-excludes>
733 and C<-alias> keys to control how methods are composed from the role.
734
735 The return value is a new L<Moose::Meta::Role::Composite> that
736 represents the combined roles.
737
738 =item B<< $metarole->composition_class_roles >>
739
740 When combining multiple roles using C<combine>, this method is used to obtain a
741 list of role names to be applied to the L<Moose::Meta::Role::Composite>
742 instance returned by C<combine>. The default implementation returns an empty
743 list. Extensions that need to hook into role combination may wrap this method
744 to return additional role names.
745
746 =item B<< Moose::Meta::Role->create($name, %options) >>
747
748 This method is identical to the L<Moose::Meta::Class> C<create>
749 method.
750
751 =item B<< Moose::Meta::Role->create_anon_role >>
752
753 This method is identical to the L<Moose::Meta::Class>
754 C<create_anon_class> method.
755
756 =item B<< $metarole->is_anon_role >>
757
758 Returns true if the role is an anonymous role.
759
760 =item B<< $metarole->consumers >>
761
762 Returns a list of names of classes and roles which consume this role.
763
764 =back
765
766 =head2 Role application
767
768 =over 4
769
770 =item B<< $metarole->apply( $thing, @options ) >>
771
772 This method applies a role to the given C<$thing>. That can be another
773 L<Moose::Meta::Role>, object, a L<Moose::Meta::Class> object, or a
774 (non-meta) object instance.
775
776 The options are passed directly to the constructor for the appropriate
777 L<Moose::Meta::Role::Application> subclass.
778
779 Note that this will apply the role even if the C<$thing> in question already
780 C<does> this role.  L<Moose::Util/does_role> is a convenient wrapper for
781 finding out if role application is necessary.
782
783 =back
784
785 =head2 Roles and other roles
786
787 =over 4
788
789 =item B<< $metarole->get_roles >>
790
791 This returns an array reference of roles which this role does. This
792 list may include duplicates.
793
794 =item B<< $metarole->calculate_all_roles >>
795
796 This returns a I<unique> list of all roles that this role does, and
797 all the roles that its roles do.
798
799 =item B<< $metarole->does_role($role) >>
800
801 Given a role I<name> or L<Moose::Meta::Role> object, returns true if this role
802 does the given role.
803
804 =item B<< $metarole->add_role($role) >>
805
806 Given a L<Moose::Meta::Role> object, this adds the role to the list of
807 roles that the role does.
808
809 =item B<< $metarole->get_excluded_roles_list >>
810
811 Returns a list of role names which this role excludes.
812
813 =item B<< $metarole->excludes_role($role_name) >>
814
815 Given a role I<name>, returns true if this role excludes the named
816 role.
817
818 =item B<< $metarole->add_excluded_roles(@role_names) >>
819
820 Given one or more role names, adds those roles to the list of excluded
821 roles.
822
823 =back
824
825 =head2 Methods
826
827 The methods for dealing with a role's methods are all identical in API
828 and behavior to the same methods in L<Class::MOP::Class>.
829
830 =over 4
831
832 =item B<< $metarole->method_metaclass >>
833
834 Returns the method metaclass name for the role. This defaults to
835 L<Moose::Meta::Role::Method>.
836
837 =item B<< $metarole->get_method($name) >>
838
839 =item B<< $metarole->has_method($name) >>
840
841 =item B<< $metarole->add_method( $name, $body ) >>
842
843 =item B<< $metarole->get_method_list >>
844
845 =item B<< $metarole->find_method_by_name($name) >>
846
847 These methods are all identical to the methods of the same name in
848 L<Class::MOP::Package>
849
850 =back
851
852 =head2 Attributes
853
854 As with methods, the methods for dealing with a role's attribute are
855 all identical in API and behavior to the same methods in
856 L<Class::MOP::Class>.
857
858 However, attributes stored in this class are I<not> stored as
859 objects. Rather, the attribute definition is stored as a hash
860 reference. When a role is composed into a class, this hash reference
861 is passed directly to the metaclass's C<add_attribute> method.
862
863 This is quite likely to change in the future.
864
865 =over 4
866
867 =item B<< $metarole->get_attribute($attribute_name) >>
868
869 =item B<< $metarole->has_attribute($attribute_name) >>
870
871 =item B<< $metarole->get_attribute_list >>
872
873 =item B<< $metarole->add_attribute($name, %options) >>
874
875 =item B<< $metarole->remove_attribute($attribute_name) >>
876
877 =back
878
879 =head2 Required methods
880
881 =over 4
882
883 =item B<< $metarole->get_required_method_list >>
884
885 Returns the list of methods required by the role.
886
887 =item B<< $metarole->requires_method($name) >>
888
889 Returns true if the role requires the named method.
890
891 =item B<< $metarole->add_required_methods(@names) >>
892
893 Adds the named methods to the role's list of required methods.
894
895 =item B<< $metarole->remove_required_methods(@names) >>
896
897 Removes the named methods from the role's list of required methods.
898
899 =item B<< $metarole->add_conflicting_method(%params) >>
900
901 Instantiate the parameters as a L<Moose::Meta::Role::Method::Conflicting>
902 object, then add it to the required method list.
903
904 =back
905
906 =head2 Method modifiers
907
908 These methods act like their counterparts in L<Class::MOP::Class> and
909 L<Moose::Meta::Class>.
910
911 However, method modifiers are simply stored internally, and are not
912 applied until the role itself is applied to a class.
913
914 =over 4
915
916 =item B<< $metarole->add_after_method_modifier($method_name, $method) >>
917
918 =item B<< $metarole->add_around_method_modifier($method_name, $method) >>
919
920 =item B<< $metarole->add_before_method_modifier($method_name, $method) >>
921
922 =item B<< $metarole->add_override_method_modifier($method_name, $method) >>
923
924 These methods all add an appropriate modifier to the internal list of
925 modifiers.
926
927 =item B<< $metarole->has_after_method_modifiers >>
928
929 =item B<< $metarole->has_around_method_modifiers >>
930
931 =item B<< $metarole->has_before_method_modifiers >>
932
933 =item B<< $metarole->has_override_method_modifier >>
934
935 Return true if the role has any modifiers of the given type.
936
937 =item B<< $metarole->get_after_method_modifiers($method_name) >>
938
939 =item B<< $metarole->get_around_method_modifiers($method_name) >>
940
941 =item B<< $metarole->get_before_method_modifiers($method_name) >>
942
943 Given a method name, returns a list of the appropriate modifiers for
944 that method.
945
946 =item B<< $metarole->get_override_method_modifier($method_name) >>
947
948 Given a method name, returns the override method modifier for that
949 method, if it has one.
950
951 =back
952
953 =head2 Introspection
954
955 =over 4
956
957 =item B<< Moose::Meta::Role->meta >>
958
959 This will return a L<Class::MOP::Class> instance for this class.
960
961 =back
962
963 =head1 BUGS
964
965 See L<Moose/BUGS> for details on reporting bugs.
966
967 =cut