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