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