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