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