bump version to 1.14
[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
b6cca0d5 12our $VERSION = '1.14';
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
21716c07 394## ------------------------------------------------------------------
fb1e11d5 395## role construction
21716c07 396## ------------------------------------------------------------------
e185c027 397
21716c07 398sub apply {
f315aab3 399 my ($self, $other, %args) = @_;
bca01282 400
401 (blessed($other))
c245d69b 402 || Moose->throw_error("You must pass in an blessed instance");
d03bd989 403
07de7559 404 my $application_class;
1c9db35c 405 if ($other->isa('Moose::Meta::Role')) {
07de7559 406 $application_class = $self->application_to_role_class;
1c9db35c 407 }
408 elsif ($other->isa('Moose::Meta::Class')) {
07de7559 409 $application_class = $self->application_to_class_class;
d03bd989 410 }
1c9db35c 411 else {
07de7559 412 $application_class = $self->application_to_instance_class;
d03bd989 413 }
07de7559 414
415 Class::MOP::load_class($application_class);
f315aab3 416 return $application_class->new(%args)->apply($self, $other, \%args);
0558683c 417}
418
4701ceff 419sub composition_class_roles { }
420
21716c07 421sub combine {
a4e516f6 422 my ($class, @role_specs) = @_;
d03bd989 423
d03bd989 424 require Moose::Meta::Role::Composite;
425
28412c0b 426 my (@roles, %role_params);
427 while (@role_specs) {
560c498d 428 my ($role, $params) = @{ splice @role_specs, 0, 1 };
429 my $requested_role
430 = blessed $role
431 ? $role
432 : Class::MOP::class_of($role);
31d96c3e 433
6bdef412 434 my $actual_role = $requested_role->_role_for_combination($params);
31d96c3e 435 push @roles => $actual_role;
436
28412c0b 437 next unless defined $params;
31d96c3e 438 $role_params{$actual_role->name} = $params;
28412c0b 439 }
d03bd989 440
fb1e11d5 441 my $c = Moose::Meta::Role::Composite->new(roles => \@roles);
7071e2cb 442 return $c->apply_params(\%role_params);
0558683c 443}
444
6bdef412 445sub _role_for_combination {
436d7a28 446 my ($self, $params) = @_;
447 return $self;
448}
449
b6a00b82 450sub create {
2d5f3fab 451 my ( $role, $package_name, %options ) = @_;
b6a00b82 452
2d5f3fab 453 $options{package} = $package_name;
b6a00b82 454
455 (ref $options{attributes} eq 'HASH')
456 || confess "You must pass a HASH ref of attributes"
457 if exists $options{attributes};
458
459 (ref $options{methods} eq 'HASH')
460 || confess "You must pass a HASH ref of methods"
461 if exists $options{methods};
462
2d5f3fab 463 my (%initialize_options) = %options;
b6a00b82 464 delete @initialize_options{qw(
465 package
466 attributes
467 methods
468 version
469 authority
470 )};
471
472 my $meta = $role->initialize( $package_name => %initialize_options );
473
6a4a7c31 474 $meta->_instantiate_module( $options{version}, $options{authority} );
475
b6a00b82 476 # FIXME totally lame
477 $meta->add_method('meta' => sub {
478 $role->initialize(ref($_[0]) || $_[0]);
479 });
480
481 if (exists $options{attributes}) {
482 foreach my $attribute_name (keys %{$options{attributes}}) {
483 my $attr = $options{attributes}->{$attribute_name};
f785aad8 484 $meta->add_attribute(
485 $attribute_name => blessed $attr ? $attr : %{$attr} );
b6a00b82 486 }
487 }
488
489 if (exists $options{methods}) {
490 foreach my $method_name (keys %{$options{methods}}) {
491 $meta->add_method($method_name, $options{methods}->{$method_name});
492 }
493 }
494
d1765290 495 Class::MOP::weaken_metaclass($meta->name)
496 if $meta->is_anon_role;
497
b6a00b82 498 return $meta;
499}
500
c23885b1 501sub consumers {
502 my $self = shift;
503 my @consumers;
504 for my $meta (Class::MOP::get_all_metaclass_instances) {
505 next if $meta->name eq $self->name;
506 next unless $meta->isa('Moose::Meta::Class')
507 || $meta->isa('Moose::Meta::Role');
508 push @consumers, $meta->name
509 if $meta->does_role($self->name);
510 }
511 return @consumers;
512}
513
c9ee520d 514# anonymous roles. most of it is copied straight out of Class::MOP::Class.
515# an intrepid hacker might find great riches if he unifies this code with that
516# code in Class::MOP::Module or Class::MOP::Package
517{
518 # NOTE:
519 # this should be sufficient, if you have a
520 # use case where it is not, write a test and
521 # I will change it.
522 my $ANON_ROLE_SERIAL = 0;
523
524 # NOTE:
525 # we need a sufficiently annoying prefix
526 # this should suffice for now, this is
527 # used in a couple of places below, so
528 # need to put it up here for now.
529 my $ANON_ROLE_PREFIX = 'Moose::Meta::Role::__ANON__::SERIAL::';
530
531 sub is_anon_role {
532 my $self = shift;
533 no warnings 'uninitialized';
534 $self->name =~ /^$ANON_ROLE_PREFIX/;
535 }
536
537 sub create_anon_role {
538 my ($role, %options) = @_;
539 my $package_name = $ANON_ROLE_PREFIX . ++$ANON_ROLE_SERIAL;
540 return $role->create($package_name, %options);
541 }
542
543 # NOTE:
544 # this will only get called for
545 # anon-roles, all other calls
546 # are assumed to occur during
547 # global destruction and so don't
548 # really need to be handled explicitly
549 sub DESTROY {
550 my $self = shift;
551
963c24e1 552 return if in_global_destruction(); # it'll happen soon anyway and this just makes things more complicated
c9ee520d 553
554 no warnings 'uninitialized';
555 return unless $self->name =~ /^$ANON_ROLE_PREFIX/;
556
557 # XXX: is this necessary for us? I don't understand what it's doing
558 # -sartak
559
560 # Moose does a weird thing where it replaces the metaclass for
561 # class when fixing metaclass incompatibility. In that case,
562 # we don't want to clean out the namespace now. We can detect
563 # that because Moose will explicitly update the singleton
564 # cache in Class::MOP.
565 #my $current_meta = Class::MOP::get_metaclass_by_name($self->name);
566 #return if $current_meta ne $self;
567
568 my ($serial_id) = ($self->name =~ /^$ANON_ROLE_PREFIX(\d+)/);
569 no strict 'refs';
570 foreach my $key (keys %{$ANON_ROLE_PREFIX . $serial_id}) {
571 delete ${$ANON_ROLE_PREFIX . $serial_id}{$key};
572 }
573 delete ${'main::' . $ANON_ROLE_PREFIX}{$serial_id . '::'};
574 }
575}
576
fb1e11d5 577#####################################################################
578## NOTE:
d03bd989 579## This is Moose::Meta::Role as defined by Moose (plus the use of
580## MooseX::AttributeHelpers module). It is here as a reference to
fb1e11d5 581## make it easier to see what is happening above with all the meta
582## programming. - SL
583#####################################################################
584#
585# has 'roles' => (
a40b446a 586# metaclass => 'Array',
fb1e11d5 587# reader => 'get_roles',
764a7831 588# isa => 'ArrayRef[Moose::Meta::Role]',
fb1e11d5 589# default => sub { [] },
590# provides => {
591# 'push' => 'add_role',
592# }
593# );
d03bd989 594#
fb1e11d5 595# has 'excluded_roles_map' => (
a40b446a 596# metaclass => 'Hash',
fb1e11d5 597# reader => 'get_excluded_roles_map',
598# isa => 'HashRef[Str]',
599# provides => {
600# # Not exactly set, cause it sets multiple
601# 'set' => 'add_excluded_roles',
602# 'keys' => 'get_excluded_roles_list',
603# 'exists' => 'excludes_role',
604# }
605# );
d03bd989 606#
fb1e11d5 607# has 'required_methods' => (
a40b446a 608# metaclass => 'Hash',
fb1e11d5 609# reader => 'get_required_methods_map',
72264f10 610# isa => 'HashRef[Moose::Meta::Role::Method::Required]',
d03bd989 611# provides => {
612# # not exactly set, or delete since it works for multiple
fb1e11d5 613# 'set' => 'add_required_methods',
614# 'delete' => 'remove_required_methods',
615# 'keys' => 'get_required_method_list',
d03bd989 616# 'exists' => 'requires_method',
fb1e11d5 617# }
618# );
d03bd989 619#
620# # the before, around and after modifiers are
621# # HASH keyed by method-name, with ARRAY of
fb1e11d5 622# # CODE refs to apply in that order
d03bd989 623#
fb1e11d5 624# has 'before_method_modifiers' => (
a40b446a 625# metaclass => 'Hash',
fb1e11d5 626# reader => 'get_before_method_modifiers_map',
627# isa => 'HashRef[ArrayRef[CodeRef]]',
628# provides => {
629# 'keys' => 'get_before_method_modifiers',
d03bd989 630# 'exists' => 'has_before_method_modifiers',
631# # This actually makes sure there is an
fb1e11d5 632# # ARRAY at the given key, and pushed onto
633# # it. It also checks for duplicates as well
d03bd989 634# # 'add' => 'add_before_method_modifier'
635# }
fb1e11d5 636# );
d03bd989 637#
fb1e11d5 638# has 'after_method_modifiers' => (
a40b446a 639# metaclass => 'Hash',
fb1e11d5 640# reader =>'get_after_method_modifiers_map',
641# isa => 'HashRef[ArrayRef[CodeRef]]',
642# provides => {
643# 'keys' => 'get_after_method_modifiers',
d03bd989 644# 'exists' => 'has_after_method_modifiers',
645# # This actually makes sure there is an
fb1e11d5 646# # ARRAY at the given key, and pushed onto
d03bd989 647# # it. It also checks for duplicates as well
648# # 'add' => 'add_after_method_modifier'
649# }
fb1e11d5 650# );
d03bd989 651#
fb1e11d5 652# has 'around_method_modifiers' => (
a40b446a 653# metaclass => 'Hash',
fb1e11d5 654# reader =>'get_around_method_modifiers_map',
655# isa => 'HashRef[ArrayRef[CodeRef]]',
656# provides => {
657# 'keys' => 'get_around_method_modifiers',
d03bd989 658# 'exists' => 'has_around_method_modifiers',
659# # This actually makes sure there is an
fb1e11d5 660# # ARRAY at the given key, and pushed onto
d03bd989 661# # it. It also checks for duplicates as well
662# # 'add' => 'add_around_method_modifier'
663# }
fb1e11d5 664# );
d03bd989 665#
fb1e11d5 666# # override is similar to the other modifiers
667# # except that it is not an ARRAY of code refs
668# # but instead just a single name->code mapping
d03bd989 669#
fb1e11d5 670# has 'override_method_modifiers' => (
a40b446a 671# metaclass => 'Hash',
fb1e11d5 672# reader =>'get_override_method_modifiers_map',
d03bd989 673# isa => 'HashRef[CodeRef]',
fb1e11d5 674# provides => {
675# 'keys' => 'get_override_method_modifier',
d03bd989 676# 'exists' => 'has_override_method_modifier',
677# 'add' => 'add_override_method_modifier', # checks for local method ..
fb1e11d5 678# }
679# );
d03bd989 680#
fb1e11d5 681#####################################################################
682
683
e185c027 6841;
685
686__END__
687
688=pod
689
690=head1 NAME
691
692Moose::Meta::Role - The Moose Role metaclass
693
694=head1 DESCRIPTION
695
705d4cfd 696This class is a subclass of L<Class::MOP::Module> that provides
697additional Moose-specific functionality.
698
699It's API looks a lot like L<Moose::Meta::Class>, but internally it
700implements many things differently. This may change in the future.
79592a54 701
cf3bb5c5 702=head1 INHERITANCE
703
704C<Moose::Meta::Role> is a subclass of L<Class::MOP::Module>.
705
e185c027 706=head1 METHODS
707
705d4cfd 708=head2 Construction
709
e185c027 710=over 4
711
705d4cfd 712=item B<< Moose::Meta::Role->initialize($role_name) >>
e185c027 713
705d4cfd 714This method creates a new role object with the provided name.
e185c027 715
705d4cfd 716=item B<< Moose::Meta::Role->combine( [ $role => { ... } ], [ $role ], ... ) >>
78cd1d3b 717
705d4cfd 718This method accepts a list of array references. Each array reference
560c498d 719should contain a role name or L<Moose::Meta::Role> object as its first element. The second element is
c8b8d92f 720an optional hash reference. The hash reference can contain C<-excludes>
721and C<-alias> keys to control how methods are composed from the role.
e606ae5f 722
705d4cfd 723The return value is a new L<Moose::Meta::Role::Composite> that
724represents the combined roles.
db1ab48d 725
4701ceff 726=item B<< $metarole->composition_class_roles >>
727
728When combining multiple roles using C<combine>, this method is used to obtain a
729list of role names to be applied to the L<Moose::Meta::Role::Composite>
730instance returned by C<combine>. The default implementation returns an empty
731list. Extensions that need to hook into role combination may wrap this method
732to return additional role names.
733
705d4cfd 734=item B<< Moose::Meta::Role->create($name, %options) >>
e185c027 735
705d4cfd 736This method is identical to the L<Moose::Meta::Class> C<create>
737method.
738
739=item B<< Moose::Meta::Role->create_anon_role >>
e185c027 740
705d4cfd 741This method is identical to the L<Moose::Meta::Class>
742C<create_anon_class> method.
e185c027 743
705d4cfd 744=item B<< $metarole->is_anon_role >>
e185c027 745
705d4cfd 746Returns true if the role is an anonymous role.
e185c027 747
c23885b1 748=item B<< $metarole->consumers >>
749
750Returns a list of names of classes and roles which consume this role.
751
e185c027 752=back
753
705d4cfd 754=head2 Role application
755
e185c027 756=over 4
757
705d4cfd 758=item B<< $metarole->apply( $thing, @options ) >>
80572233 759
705d4cfd 760This method applies a role to the given C<$thing>. That can be another
761L<Moose::Meta::Role>, object, a L<Moose::Meta::Class> object, or a
762(non-meta) object instance.
80572233 763
705d4cfd 764The options are passed directly to the constructor for the appropriate
765L<Moose::Meta::Role::Application> subclass.
80572233 766
d05235e1 767Note that this will apply the role even if the C<$thing> in question already
768C<does> this role. L<Moose::Util/does_role> is a convenient wrapper for
769finding out if role application is necessary.
770
80572233 771=back
772
705d4cfd 773=head2 Roles and other roles
774
80572233 775=over 4
776
705d4cfd 777=item B<< $metarole->get_roles >>
d79e62fd 778
705d4cfd 779This returns an array reference of roles which this role does. This
780list may include duplicates.
d79e62fd 781
705d4cfd 782=item B<< $metarole->calculate_all_roles >>
d79e62fd 783
705d4cfd 784This returns a I<unique> list of all roles that this role does, and
785all the roles that its roles do.
d79e62fd 786
560c498d 787=item B<< $metarole->does_role($role) >>
2b14ac61 788
560c498d 789Given a role I<name> or L<Moose::Meta::Role> object, returns true if this role
790does the given role.
d79e62fd 791
705d4cfd 792=item B<< $metarole->add_role($role) >>
d79e62fd 793
705d4cfd 794Given a L<Moose::Meta::Role> object, this adds the role to the list of
795roles that the role does.
68efb014 796
705d4cfd 797=item B<< $metarole->get_excluded_roles_list >>
be4427d0 798
705d4cfd 799Returns a list of role names which this role excludes.
e185c027 800
705d4cfd 801=item B<< $metarole->excludes_role($role_name) >>
e185c027 802
705d4cfd 803Given a role I<name>, returns true if this role excludes the named
804role.
e606ae5f 805
705d4cfd 806=item B<< $metarole->add_excluded_roles(@role_names) >>
e606ae5f 807
705d4cfd 808Given one or more role names, adds those roles to the list of excluded
809roles.
bdabd620 810
705d4cfd 811=back
e185c027 812
705d4cfd 813=head2 Methods
093b12c2 814
705d4cfd 815The methods for dealing with a role's methods are all identical in API
816and behavior to the same methods in L<Class::MOP::Class>.
638853cb 817
705d4cfd 818=over 4
53dd42d8 819
705d4cfd 820=item B<< $metarole->method_metaclass >>
e185c027 821
705d4cfd 822Returns the method metaclass name for the role. This defaults to
823L<Moose::Meta::Role::Method>.
e185c027 824
705d4cfd 825=item B<< $metarole->get_method($name) >>
e185c027 826
705d4cfd 827=item B<< $metarole->has_method($name) >>
e185c027 828
705d4cfd 829=item B<< $metarole->add_method( $name, $body ) >>
e185c027 830
705d4cfd 831=item B<< $metarole->get_method_list >>
e185c027 832
705d4cfd 833=item B<< $metarole->find_method_by_name($name) >>
834
835These methods are all identical to the methods of the same name in
8f6cbfcd 836L<Class::MOP::Package>
e185c027 837
838=back
839
705d4cfd 840=head2 Attributes
841
842As with methods, the methods for dealing with a role's attribute are
843all identical in API and behavior to the same methods in
844L<Class::MOP::Class>.
845
846However, attributes stored in this class are I<not> stored as
847objects. Rather, the attribute definition is stored as a hash
848reference. When a role is composed into a class, this hash reference
849is passed directly to the metaclass's C<add_attribute> method.
850
851This is quite likely to change in the future.
852
e185c027 853=over 4
854
705d4cfd 855=item B<< $metarole->get_attribute($attribute_name) >>
856
857=item B<< $metarole->has_attribute($attribute_name) >>
1331430a 858
705d4cfd 859=item B<< $metarole->get_attribute_list >>
1331430a 860
705d4cfd 861=item B<< $metarole->add_attribute($name, %options) >>
1331430a 862
705d4cfd 863=item B<< $metarole->remove_attribute($attribute_name) >>
1331430a 864
865=back
866
705d4cfd 867=head2 Required methods
868
0558683c 869=over 4
870
705d4cfd 871=item B<< $metarole->get_required_method_list >>
0558683c 872
705d4cfd 873Returns the list of methods required by the role.
0558683c 874
705d4cfd 875=item B<< $metarole->requires_method($name) >>
0558683c 876
705d4cfd 877Returns true if the role requires the named method.
0558683c 878
0ecff16b 879=item B<< $metarole->add_required_methods(@names) >>
0558683c 880
b3c1586a 881Adds the named methods to the role's list of required methods.
0558683c 882
705d4cfd 883=item B<< $metarole->remove_required_methods(@names) >>
0558683c 884
2dda1d28 885Removes the named methods from the role's list of required methods.
0558683c 886
bb153262 887=item B<< $metarole->add_conflicting_method(%params) >>
09eeab06 888
bb153262 889Instantiate the parameters as a L<Moose::Meta::Role::Method::Conflicting>
09eeab06 890object, then add it to the required method list.
891
705d4cfd 892=back
893
894=head2 Method modifiers
0558683c 895
fa907f2e 896These methods act like their counterparts in L<Class::MOP::Class> and
705d4cfd 897L<Moose::Meta::Class>.
898
899However, method modifiers are simply stored internally, and are not
900applied until the role itself is applied to a class.
0558683c 901
902=over 4
903
705d4cfd 904=item B<< $metarole->add_after_method_modifier($method_name, $method) >>
0558683c 905
705d4cfd 906=item B<< $metarole->add_around_method_modifier($method_name, $method) >>
0558683c 907
705d4cfd 908=item B<< $metarole->add_before_method_modifier($method_name, $method) >>
0558683c 909
705d4cfd 910=item B<< $metarole->add_override_method_modifier($method_name, $method) >>
0558683c 911
705d4cfd 912These methods all add an appropriate modifier to the internal list of
913modifiers.
0558683c 914
705d4cfd 915=item B<< $metarole->has_after_method_modifiers >>
0558683c 916
705d4cfd 917=item B<< $metarole->has_around_method_modifiers >>
918
919=item B<< $metarole->has_before_method_modifiers >>
920
921=item B<< $metarole->has_override_method_modifier >>
0558683c 922
705d4cfd 923Return true if the role has any modifiers of the given type.
0558683c 924
705d4cfd 925=item B<< $metarole->get_after_method_modifiers($method_name) >>
0558683c 926
705d4cfd 927=item B<< $metarole->get_around_method_modifiers($method_name) >>
0558683c 928
705d4cfd 929=item B<< $metarole->get_before_method_modifiers($method_name) >>
0558683c 930
705d4cfd 931Given a method name, returns a list of the appropriate modifiers for
932that method.
933
934=item B<< $metarole->get_override_method_modifier($method_name) >>
935
936Given a method name, returns the override method modifier for that
937method, if it has one.
0558683c 938
939=back
940
705d4cfd 941=head2 Introspection
505065c4 942
705d4cfd 943=over 4
505065c4 944
705d4cfd 945=item B<< Moose::Meta::Role->meta >>
505065c4 946
705d4cfd 947This will return a L<Class::MOP::Class> instance for this class.
505065c4 948
949=back
950
e185c027 951=head1 BUGS
952
d4048ef3 953See L<Moose/BUGS> for details on reporting bugs.
e185c027 954
955=head1 AUTHOR
956
957Stevan Little E<lt>stevan@iinteractive.comE<gt>
958
959=head1 COPYRIGHT AND LICENSE
960
7e0492d3 961Copyright 2006-2010 by Infinity Interactive, Inc.
e185c027 962
963L<http://www.iinteractive.com>
964
965This library is free software; you can redistribute it and/or modify
fb1e11d5 966it under the same terms as Perl itself.
e185c027 967
b8aeb4dc 968=cut