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