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