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