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