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