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