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