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