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