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