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