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