0f96d22fed667690dff8ac55b9d568cc7472493f
[gitmo/Moose.git] / lib / Moose / Meta / Role.pm
1 package Moose::Meta::Role;
2
3 use strict;
4 use warnings;
5 use metaclass;
6
7 use Class::Load qw(load_class);
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 'stasis' 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         Class::MOP::_definition_context(),
87     ));
88
89     # create some helper methods
90     $META->add_method($methods->{add} => sub {
91         my ($self, @values) = @_;
92         $self->$attr_reader->{$_} = undef foreach @values;
93     }) if exists $methods->{add};
94
95     $META->add_method($methods->{get_keys} => sub {
96         my ($self) = @_;
97         keys %{$self->$attr_reader};
98     }) if exists $methods->{get_keys};
99
100     $META->add_method($methods->{get_values} => sub {
101         my ($self) = @_;
102         values %{$self->$attr_reader};
103     }) if exists $methods->{get_values};
104
105     $META->add_method($methods->{get} => sub {
106         my ($self, $name) = @_;
107         $self->$attr_reader->{$name}
108     }) if exists $methods->{get};
109
110     $META->add_method($methods->{existence} => sub {
111         my ($self, $name) = @_;
112         exists $self->$attr_reader->{$name} ? 1 : 0;
113     }) if exists $methods->{existence};
114
115     $META->add_method($methods->{remove} => sub {
116         my ($self, @values) = @_;
117         delete $self->$attr_reader->{$_} foreach @values;
118     }) if exists $methods->{remove};
119 }
120
121 $META->add_attribute(
122     'method_metaclass',
123     reader  => 'method_metaclass',
124     default => 'Moose::Meta::Role::Method',
125     Class::MOP::_definition_context(),
126 );
127
128 $META->add_attribute(
129     'required_method_metaclass',
130     reader  => 'required_method_metaclass',
131     default => 'Moose::Meta::Role::Method::Required',
132     Class::MOP::_definition_context(),
133 );
134
135 $META->add_attribute(
136     'conflicting_method_metaclass',
137     reader  => 'conflicting_method_metaclass',
138     default => 'Moose::Meta::Role::Method::Conflicting',
139     Class::MOP::_definition_context(),
140 );
141
142 $META->add_attribute(
143     'application_to_class_class',
144     reader  => 'application_to_class_class',
145     default => 'Moose::Meta::Role::Application::ToClass',
146     Class::MOP::_definition_context(),
147 );
148
149 $META->add_attribute(
150     'application_to_role_class',
151     reader  => 'application_to_role_class',
152     default => 'Moose::Meta::Role::Application::ToRole',
153     Class::MOP::_definition_context(),
154 );
155
156 $META->add_attribute(
157     'application_to_instance_class',
158     reader  => 'application_to_instance_class',
159     default => 'Moose::Meta::Role::Application::ToInstance',
160     Class::MOP::_definition_context(),
161 );
162
163 $META->add_attribute(
164     'applied_attribute_metaclass',
165     reader  => 'applied_attribute_metaclass',
166     default => 'Moose::Meta::Attribute',
167     Class::MOP::_definition_context(),
168 );
169
170 # More or less copied from Moose::Meta::Class
171 sub initialize {
172     my $class = shift;
173     my @args = @_;
174     unshift @args, 'package' if @args % 2;
175     my %opts = @args;
176     my $package = delete $opts{package};
177     return Class::MOP::get_metaclass_by_name($package)
178         || $class->SUPER::initialize($package,
179                 'attribute_metaclass' => 'Moose::Meta::Role::Attribute',
180                 %opts,
181             );
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         Class::MOP::_definition_context(),
304     ));
305
306     # and some helper methods ...
307     $META->add_method("get_${modifier_type}_method_modifiers" => sub {
308         my ($self, $method_name) = @_;
309         #return () unless exists $self->$attr_reader->{$method_name};
310         my $mm = $self->$attr_reader->{$method_name};
311         $mm ? @$mm : ();
312     });
313
314     $META->add_method("has_${modifier_type}_method_modifiers" => sub {
315         my ($self, $method_name) = @_;
316         # NOTE:
317         # for now we assume that if it exists,..
318         # it has at least one modifier in it
319         (exists $self->$attr_reader->{$method_name}) ? 1 : 0;
320     });
321
322     $META->add_method("add_${modifier_type}_method_modifier" => sub {
323         my ($self, $method_name, $method) = @_;
324
325         $self->$attr_reader->{$method_name} = []
326             unless exists $self->$attr_reader->{$method_name};
327
328         my $modifiers = $self->$attr_reader->{$method_name};
329
330         # NOTE:
331         # check to see that we aren't adding the
332         # same code twice. We err in favor of the
333         # first on here, this may not be as expected
334         foreach my $modifier (@{$modifiers}) {
335             return if $modifier == $method;
336         }
337
338         push @{$modifiers} => $method;
339     });
340
341 }
342
343 ## ------------------------------------------------------------------
344 ## override method mofidiers
345
346 $META->add_attribute('override_method_modifiers' => (
347     reader  => 'get_override_method_modifiers_map',
348     default => sub { {} },
349     Class::MOP::_definition_context(),
350 ));
351
352 # NOTE:
353 # these are a little different because there
354 # can only be one per name, whereas the other
355 # method modifiers can have multiples.
356 # - SL
357
358 sub add_override_method_modifier {
359     my ($self, $method_name, $method) = @_;
360     (!$self->has_method($method_name))
361         || Moose->throw_error("Cannot add an override of method '$method_name' " .
362                    "because there is a local version of '$method_name'");
363     $self->get_override_method_modifiers_map->{$method_name} = $method;
364 }
365
366 sub has_override_method_modifier {
367     my ($self, $method_name) = @_;
368     # NOTE:
369     # for now we assume that if it exists,..
370     # it has at least one modifier in it
371     (exists $self->get_override_method_modifiers_map->{$method_name}) ? 1 : 0;
372 }
373
374 sub get_override_method_modifier {
375     my ($self, $method_name) = @_;
376     $self->get_override_method_modifiers_map->{$method_name};
377 }
378
379 ## general list accessor ...
380
381 sub get_method_modifier_list {
382     my ($self, $modifier_type) = @_;
383     my $accessor = "get_${modifier_type}_method_modifiers_map";
384     keys %{$self->$accessor};
385 }
386
387 sub _meta_method_class { 'Moose::Meta::Method::Meta' }
388
389 ## ------------------------------------------------------------------
390 ## subroles
391
392 $META->add_attribute('roles' => (
393     reader  => 'get_roles',
394     default => sub { [] },
395     Class::MOP::_definition_context(),
396 ));
397
398 sub add_role {
399     my ($self, $role) = @_;
400     (blessed($role) && $role->isa('Moose::Meta::Role'))
401         || Moose->throw_error("Roles must be instances of Moose::Meta::Role");
402     push @{$self->get_roles} => $role;
403     $self->reset_package_cache_flag;
404 }
405
406 sub calculate_all_roles {
407     my $self = shift;
408     my %seen;
409     grep {
410         !$seen{$_->name}++
411     } ($self, map {
412                   $_->calculate_all_roles
413               } @{ $self->get_roles });
414 }
415
416 sub does_role {
417     my ($self, $role) = @_;
418     (defined $role)
419         || Moose->throw_error("You must supply a role name to look for");
420     my $role_name = blessed $role ? $role->name : $role;
421     # if we are it,.. then return true
422     return 1 if $role_name eq $self->name;
423     # otherwise.. check our children
424     foreach my $role (@{$self->get_roles}) {
425         return 1 if $role->does_role($role_name);
426     }
427     return 0;
428 }
429
430 sub find_method_by_name { (shift)->get_method(@_) }
431
432 ## ------------------------------------------------------------------
433 ## role construction
434 ## ------------------------------------------------------------------
435
436 sub apply {
437     my ($self, $other, %args) = @_;
438
439     (blessed($other))
440         || Moose->throw_error("You must pass in an blessed instance");
441
442     my $application_class;
443     if ($other->isa('Moose::Meta::Role')) {
444         $application_class = $self->application_to_role_class;
445     }
446     elsif ($other->isa('Moose::Meta::Class')) {
447         $application_class = $self->application_to_class_class;
448     }
449     else {
450         $application_class = $self->application_to_instance_class;
451     }
452
453     load_class($application_class);
454
455     if ( exists $args{'-excludes'} ) {
456         # I wish we had coercion here :)
457         $args{'-excludes'} = (
458             ref $args{'-excludes'} eq 'ARRAY'
459             ? $args{'-excludes'}
460             : [ $args{'-excludes'} ]
461         );
462     }
463
464     return $application_class->new(%args)->apply($self, $other, \%args);
465 }
466
467 sub composition_class_roles { }
468
469 sub combine {
470     my ($class, @role_specs) = @_;
471
472     require Moose::Meta::Role::Composite;
473
474     my (@roles, %role_params);
475     while (@role_specs) {
476         my ($role, $params) = @{ splice @role_specs, 0, 1 };
477         my $requested_role
478             = blessed $role
479             ? $role
480             : Class::MOP::class_of($role);
481
482         my $actual_role = $requested_role->_role_for_combination($params);
483         push @roles => $actual_role;
484
485         next unless defined $params;
486         $role_params{$actual_role->name} = $params;
487     }
488
489     my $c = Moose::Meta::Role::Composite->new(roles => \@roles);
490     return $c->apply_params(\%role_params);
491 }
492
493 sub _role_for_combination {
494     my ($self, $params) = @_;
495     return $self;
496 }
497
498 sub create {
499     my $class = shift;
500     my @args = @_;
501
502     unshift @args, 'package' if @args % 2 == 1;
503     my %options = @args;
504
505     (ref $options{attributes} eq 'HASH')
506         || confess "You must pass a HASH ref of attributes"
507             if exists $options{attributes};
508
509     (ref $options{methods} eq 'HASH')
510         || confess "You must pass a HASH ref of methods"
511             if exists $options{methods};
512
513     (ref $options{roles} eq 'ARRAY')
514         || confess "You must pass an ARRAY ref of roles"
515             if exists $options{roles};
516
517     my $package      = delete $options{package};
518     my $roles        = delete $options{roles};
519     my $attributes   = delete $options{attributes};
520     my $methods      = delete $options{methods};
521     my $meta_name    = exists $options{meta_name}
522                          ? delete $options{meta_name}
523                          : 'meta';
524
525     my $meta = $class->SUPER::create($package => %options);
526
527     $meta->_add_meta_method($meta_name)
528         if defined $meta_name;
529
530     if (defined $attributes) {
531         foreach my $attribute_name (keys %{$attributes}) {
532             my $attr = $attributes->{$attribute_name};
533             $meta->add_attribute(
534                 $attribute_name => blessed $attr ? $attr : %{$attr} );
535         }
536     }
537
538     if (defined $methods) {
539         foreach my $method_name (keys %{$methods}) {
540             $meta->add_method($method_name, $methods->{$method_name});
541         }
542     }
543
544     if ($roles) {
545         Moose::Util::apply_all_roles($meta, @$roles);
546     }
547
548     return $meta;
549 }
550
551 sub consumers {
552     my $self = shift;
553     my @consumers;
554     for my $meta (Class::MOP::get_all_metaclass_instances) {
555         next if $meta->name eq $self->name;
556         next unless $meta->isa('Moose::Meta::Class')
557                  || $meta->isa('Moose::Meta::Role');
558         push @consumers, $meta->name
559             if $meta->does_role($self->name);
560     }
561     return @consumers;
562 }
563
564 # XXX: something more intelligent here?
565 sub _anon_package_prefix { 'Moose::Meta::Role::__ANON__::SERIAL::' }
566
567 sub create_anon_role { shift->create_anon(@_) }
568 sub is_anon_role     { shift->is_anon(@_)     }
569
570 sub _anon_cache_key {
571     my $class = shift;
572     my %options = @_;
573
574     # XXX fix this duplication (see MMC::_anon_cache_key
575     my $roles = Data::OptList::mkopt(($options{roles} || []), {
576         moniker  => 'role',
577         val_test => sub { ref($_[0]) eq 'HASH' },
578     });
579
580     my @role_keys;
581     for my $role_spec (@$roles) {
582         my ($role, $params) = @$role_spec;
583         $params = { %$params };
584
585         my $key = blessed($role) ? $role->name : $role;
586
587         if ($params && %$params) {
588             my $alias    = delete $params->{'-alias'}
589                         || delete $params->{'alias'}
590                         || {};
591             my $excludes = delete $params->{'-excludes'}
592                         || delete $params->{'excludes'}
593                         || [];
594             $excludes = [$excludes] unless ref($excludes) eq 'ARRAY';
595
596             if (%$params) {
597                 warn "Roles with parameters cannot be cached. Consider "
598                    . "applying the parameters before calling "
599                    . "create_anon_class, or using 'weaken => 0' instead";
600                 return;
601             }
602
603             my $alias_key = join('%',
604                 map { $_ => $alias->{$_} } sort keys %$alias
605             );
606             my $excludes_key = join('%',
607                 sort @$excludes
608             );
609             $key .= '<' . join('+', 'a', $alias_key, 'e', $excludes_key) . '>';
610         }
611
612         push @role_keys, $key;
613     }
614
615     # Makes something like Role|Role::1
616     return join('|', sort @role_keys);
617 }
618
619 #####################################################################
620 ## NOTE:
621 ## This is Moose::Meta::Role as defined by Moose (plus the use of
622 ## MooseX::AttributeHelpers module). It is here as a reference to
623 ## make it easier to see what is happening above with all the meta
624 ## programming. - SL
625 #####################################################################
626 #
627 # has 'roles' => (
628 #     metaclass => 'Array',
629 #     reader    => 'get_roles',
630 #     isa       => 'ArrayRef[Moose::Meta::Role]',
631 #     default   => sub { [] },
632 #     provides  => {
633 #         'push' => 'add_role',
634 #     }
635 # );
636 #
637 # has 'excluded_roles_map' => (
638 #     metaclass => 'Hash',
639 #     reader    => 'get_excluded_roles_map',
640 #     isa       => 'HashRef[Str]',
641 #     provides  => {
642 #         # Not exactly set, cause it sets multiple
643 #         'set'    => 'add_excluded_roles',
644 #         'keys'   => 'get_excluded_roles_list',
645 #         'exists' => 'excludes_role',
646 #     }
647 # );
648 #
649 # has 'required_methods' => (
650 #     metaclass => 'Hash',
651 #     reader    => 'get_required_methods_map',
652 #     isa       => 'HashRef[Moose::Meta::Role::Method::Required]',
653 #     provides  => {
654 #         # not exactly set, or delete since it works for multiple
655 #         'set'    => 'add_required_methods',
656 #         'delete' => 'remove_required_methods',
657 #         'keys'   => 'get_required_method_list',
658 #         'exists' => 'requires_method',
659 #     }
660 # );
661 #
662 # # the before, around and after modifiers are
663 # # HASH keyed by method-name, with ARRAY of
664 # # CODE refs to apply in that order
665 #
666 # has 'before_method_modifiers' => (
667 #     metaclass => 'Hash',
668 #     reader    => 'get_before_method_modifiers_map',
669 #     isa       => 'HashRef[ArrayRef[CodeRef]]',
670 #     provides  => {
671 #         'keys'   => 'get_before_method_modifiers',
672 #         'exists' => 'has_before_method_modifiers',
673 #         # This actually makes sure there is an
674 #         # ARRAY at the given key, and pushed onto
675 #         # it. It also checks for duplicates as well
676 #         # 'add'  => 'add_before_method_modifier'
677 #     }
678 # );
679 #
680 # has 'after_method_modifiers' => (
681 #     metaclass => 'Hash',
682 #     reader    =>'get_after_method_modifiers_map',
683 #     isa       => 'HashRef[ArrayRef[CodeRef]]',
684 #     provides  => {
685 #         'keys'   => 'get_after_method_modifiers',
686 #         'exists' => 'has_after_method_modifiers',
687 #         # This actually makes sure there is an
688 #         # ARRAY at the given key, and pushed onto
689 #         # it. It also checks for duplicates as well
690 #         # 'add'  => 'add_after_method_modifier'
691 #     }
692 # );
693 #
694 # has 'around_method_modifiers' => (
695 #     metaclass => 'Hash',
696 #     reader    =>'get_around_method_modifiers_map',
697 #     isa       => 'HashRef[ArrayRef[CodeRef]]',
698 #     provides  => {
699 #         'keys'   => 'get_around_method_modifiers',
700 #         'exists' => 'has_around_method_modifiers',
701 #         # This actually makes sure there is an
702 #         # ARRAY at the given key, and pushed onto
703 #         # it. It also checks for duplicates as well
704 #         # 'add'  => 'add_around_method_modifier'
705 #     }
706 # );
707 #
708 # # override is similar to the other modifiers
709 # # except that it is not an ARRAY of code refs
710 # # but instead just a single name->code mapping
711 #
712 # has 'override_method_modifiers' => (
713 #     metaclass => 'Hash',
714 #     reader    =>'get_override_method_modifiers_map',
715 #     isa       => 'HashRef[CodeRef]',
716 #     provides  => {
717 #         'keys'   => 'get_override_method_modifier',
718 #         'exists' => 'has_override_method_modifier',
719 #         'add'    => 'add_override_method_modifier', # checks for local method ..
720 #     }
721 # );
722 #
723 #####################################################################
724
725
726 1;
727
728 # ABSTRACT: The Moose Role metaclass
729
730 __END__
731
732 =pod
733
734 =head1 DESCRIPTION
735
736 This class is a subclass of L<Class::MOP::Module> that provides
737 additional Moose-specific functionality.
738
739 Its API looks a lot like L<Moose::Meta::Class>, but internally it
740 implements many things differently. This may change in the future.
741
742 =head1 INHERITANCE
743
744 C<Moose::Meta::Role> is a subclass of L<Class::MOP::Module>.
745
746 =head1 METHODS
747
748 =head2 Construction
749
750 =over 4
751
752 =item B<< Moose::Meta::Role->initialize($role_name) >>
753
754 This method creates a new role object with the provided name.
755
756 =item B<< Moose::Meta::Role->combine( [ $role => { ... } ], [ $role ], ... ) >>
757
758 This method accepts a list of array references. Each array reference
759 should contain a role name or L<Moose::Meta::Role> object as its first element. The second element is
760 an optional hash reference. The hash reference can contain C<-excludes>
761 and C<-alias> keys to control how methods are composed from the role.
762
763 The return value is a new L<Moose::Meta::Role::Composite> that
764 represents the combined roles.
765
766 =item B<< $metarole->composition_class_roles >>
767
768 When combining multiple roles using C<combine>, this method is used to obtain a
769 list of role names to be applied to the L<Moose::Meta::Role::Composite>
770 instance returned by C<combine>. The default implementation returns an empty
771 list. Extensions that need to hook into role combination may wrap this method
772 to return additional role names.
773
774 =item B<< Moose::Meta::Role->create($name, %options) >>
775
776 This method is identical to the L<Moose::Meta::Class> C<create>
777 method.
778
779 =item B<< Moose::Meta::Role->create_anon_role >>
780
781 This method is identical to the L<Moose::Meta::Class>
782 C<create_anon_class> method.
783
784 =item B<< $metarole->is_anon_role >>
785
786 Returns true if the role is an anonymous role.
787
788 =item B<< $metarole->consumers >>
789
790 Returns a list of names of classes and roles which consume this role.
791
792 =back
793
794 =head2 Role application
795
796 =over 4
797
798 =item B<< $metarole->apply( $thing, @options ) >>
799
800 This method applies a role to the given C<$thing>. That can be another
801 L<Moose::Meta::Role>, object, a L<Moose::Meta::Class> object, or a
802 (non-meta) object instance.
803
804 The options are passed directly to the constructor for the appropriate
805 L<Moose::Meta::Role::Application> subclass.
806
807 Note that this will apply the role even if the C<$thing> in question already
808 C<does> this role.  L<Moose::Util/does_role> is a convenient wrapper for
809 finding out if role application is necessary.
810
811 =back
812
813 =head2 Roles and other roles
814
815 =over 4
816
817 =item B<< $metarole->get_roles >>
818
819 This returns an array reference of roles which this role does. This
820 list may include duplicates.
821
822 =item B<< $metarole->calculate_all_roles >>
823
824 This returns a I<unique> list of all roles that this role does, and
825 all the roles that its roles do.
826
827 =item B<< $metarole->does_role($role) >>
828
829 Given a role I<name> or L<Moose::Meta::Role> object, returns true if this role
830 does the given role.
831
832 =item B<< $metarole->add_role($role) >>
833
834 Given a L<Moose::Meta::Role> object, this adds the role to the list of
835 roles that the role does.
836
837 =item B<< $metarole->get_excluded_roles_list >>
838
839 Returns a list of role names which this role excludes.
840
841 =item B<< $metarole->excludes_role($role_name) >>
842
843 Given a role I<name>, returns true if this role excludes the named
844 role.
845
846 =item B<< $metarole->add_excluded_roles(@role_names) >>
847
848 Given one or more role names, adds those roles to the list of excluded
849 roles.
850
851 =back
852
853 =head2 Methods
854
855 The methods for dealing with a role's methods are all identical in API
856 and behavior to the same methods in L<Class::MOP::Class>.
857
858 =over 4
859
860 =item B<< $metarole->method_metaclass >>
861
862 Returns the method metaclass name for the role. This defaults to
863 L<Moose::Meta::Role::Method>.
864
865 =item B<< $metarole->get_method($name) >>
866
867 =item B<< $metarole->has_method($name) >>
868
869 =item B<< $metarole->add_method( $name, $body ) >>
870
871 =item B<< $metarole->get_method_list >>
872
873 =item B<< $metarole->find_method_by_name($name) >>
874
875 These methods are all identical to the methods of the same name in
876 L<Class::MOP::Package>
877
878 =back
879
880 =head2 Attributes
881
882 As with methods, the methods for dealing with a role's attribute are
883 all identical in API and behavior to the same methods in
884 L<Class::MOP::Class>.
885
886 However, attributes stored in this class are I<not> stored as
887 objects. Rather, the attribute definition is stored as a hash
888 reference. When a role is composed into a class, this hash reference
889 is passed directly to the metaclass's C<add_attribute> method.
890
891 This is quite likely to change in the future.
892
893 =over 4
894
895 =item B<< $metarole->get_attribute($attribute_name) >>
896
897 =item B<< $metarole->has_attribute($attribute_name) >>
898
899 =item B<< $metarole->get_attribute_list >>
900
901 =item B<< $metarole->add_attribute($name, %options) >>
902
903 =item B<< $metarole->remove_attribute($attribute_name) >>
904
905 =back
906
907 =head2 Required methods
908
909 =over 4
910
911 =item B<< $metarole->get_required_method_list >>
912
913 Returns the list of methods required by the role.
914
915 =item B<< $metarole->requires_method($name) >>
916
917 Returns true if the role requires the named method.
918
919 =item B<< $metarole->add_required_methods(@names) >>
920
921 Adds the named methods to the role's list of required methods.
922
923 =item B<< $metarole->remove_required_methods(@names) >>
924
925 Removes the named methods from the role's list of required methods.
926
927 =item B<< $metarole->add_conflicting_method(%params) >>
928
929 Instantiate the parameters as a L<Moose::Meta::Role::Method::Conflicting>
930 object, then add it to the required method list.
931
932 =back
933
934 =head2 Method modifiers
935
936 These methods act like their counterparts in L<Class::MOP::Class> and
937 L<Moose::Meta::Class>.
938
939 However, method modifiers are simply stored internally, and are not
940 applied until the role itself is applied to a class.
941
942 =over 4
943
944 =item B<< $metarole->add_after_method_modifier($method_name, $method) >>
945
946 =item B<< $metarole->add_around_method_modifier($method_name, $method) >>
947
948 =item B<< $metarole->add_before_method_modifier($method_name, $method) >>
949
950 =item B<< $metarole->add_override_method_modifier($method_name, $method) >>
951
952 These methods all add an appropriate modifier to the internal list of
953 modifiers.
954
955 =item B<< $metarole->has_after_method_modifiers >>
956
957 =item B<< $metarole->has_around_method_modifiers >>
958
959 =item B<< $metarole->has_before_method_modifiers >>
960
961 =item B<< $metarole->has_override_method_modifier >>
962
963 Return true if the role has any modifiers of the given type.
964
965 =item B<< $metarole->get_after_method_modifiers($method_name) >>
966
967 =item B<< $metarole->get_around_method_modifiers($method_name) >>
968
969 =item B<< $metarole->get_before_method_modifiers($method_name) >>
970
971 Given a method name, returns a list of the appropriate modifiers for
972 that method.
973
974 =item B<< $metarole->get_override_method_modifier($method_name) >>
975
976 Given a method name, returns the override method modifier for that
977 method, if it has one.
978
979 =back
980
981 =head2 Introspection
982
983 =over 4
984
985 =item B<< Moose::Meta::Role->meta >>
986
987 This will return a L<Class::MOP::Class> instance for this class.
988
989 =back
990
991 =head1 BUGS
992
993 See L<Moose/BUGS> for details on reporting bugs.
994
995 =cut