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