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