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