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