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