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