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