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