bump version
[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
3543f4d4 12our $VERSION = '1.23';
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);
7c7ab486 227
228 for my $role ( @{ $old_meta->get_roles } ) {
229 $self->add_role($role);
230 }
f785aad8 231}
232
233sub add_attribute {
234 my $self = shift;
235
236 if (blessed $_[0] && ! $_[0]->isa('Moose::Meta::Role::Attribute') ) {
237 my $class = ref $_[0];
238 Moose->throw_error( "Cannot add a $class as an attribute to a role" );
21716c07 239 }
8c063f8e 240 elsif (!blessed($_[0]) && defined($_[0]) && $_[0] =~ /^\+(.*)/) {
576cd474 241 Moose->throw_error( "has '+attr' is not supported in roles" );
242 }
f785aad8 243
244 return $self->SUPER::add_attribute(@_);
245}
246
247sub _attach_attribute {
248 my ( $self, $attribute ) = @_;
249
250 $attribute->attach_to_role($self);
21716c07 251}
e185c027 252
16d721b3 253sub add_required_methods {
254 my $self = shift;
255
256 for (@_) {
257 my $method = $_;
c9d7e396 258 if (!blessed($method)) {
16d721b3 259 $method = $self->required_method_metaclass->new(
260 name => $method,
261 );
262 }
263 $self->get_required_methods_map->{$method->name} = $method;
264 }
265}
266
bb153262 267sub add_conflicting_method {
09eeab06 268 my $self = shift;
269
270 my $method;
271 if (@_ == 1 && blessed($_[0])) {
272 $method = shift;
273 }
274 else {
bb153262 275 $method = $self->conflicting_method_metaclass->new(@_);
09eeab06 276 }
277
278 $self->add_required_methods($method);
279}
280
21716c07 281## ------------------------------------------------------------------
282## method modifiers
283
21716c07 284# NOTE:
fb1e11d5 285# the before/around/after method modifiers are
21716c07 286# stored by name, but there can be many methods
287# then associated with that name. So again we have
288# lots of similar functionality, so we can do some
289# meta-programmin' and save some time.
290# - SL
291
292foreach my $modifier_type (qw[ before around after ]) {
fb1e11d5 293
294 my $attr_reader = "get_${modifier_type}_method_modifiers_map";
d03bd989 295
fb1e11d5 296 # create the attribute ...
297 $META->add_attribute("${modifier_type}_method_modifiers" => (
298 reader => $attr_reader,
299 default => sub { {} }
d03bd989 300 ));
fb1e11d5 301
302 # and some helper methods ...
21716c07 303 $META->add_method("get_${modifier_type}_method_modifiers" => sub {
304 my ($self, $method_name) = @_;
fb1e11d5 305 #return () unless exists $self->$attr_reader->{$method_name};
d9847263 306 my $mm = $self->$attr_reader->{$method_name};
307 $mm ? @$mm : ();
21716c07 308 });
fb1e11d5 309
21716c07 310 $META->add_method("has_${modifier_type}_method_modifiers" => sub {
311 my ($self, $method_name) = @_;
312 # NOTE:
fb1e11d5 313 # for now we assume that if it exists,..
21716c07 314 # it has at least one modifier in it
315 (exists $self->$attr_reader->{$method_name}) ? 1 : 0;
fb1e11d5 316 });
317
21716c07 318 $META->add_method("add_${modifier_type}_method_modifier" => sub {
319 my ($self, $method_name, $method) = @_;
fb1e11d5 320
321 $self->$attr_reader->{$method_name} = []
21716c07 322 unless exists $self->$attr_reader->{$method_name};
fb1e11d5 323
21716c07 324 my $modifiers = $self->$attr_reader->{$method_name};
fb1e11d5 325
21716c07 326 # NOTE:
fb1e11d5 327 # check to see that we aren't adding the
328 # same code twice. We err in favor of the
21716c07 329 # first on here, this may not be as expected
330 foreach my $modifier (@{$modifiers}) {
331 return if $modifier == $method;
332 }
fb1e11d5 333
21716c07 334 push @{$modifiers} => $method;
335 });
fb1e11d5 336
21716c07 337}
1331430a 338
21716c07 339## ------------------------------------------------------------------
340## override method mofidiers
0558683c 341
fb1e11d5 342$META->add_attribute('override_method_modifiers' => (
343 reader => 'get_override_method_modifiers_map',
344 default => sub { {} }
345));
346
21716c07 347# NOTE:
fb1e11d5 348# these are a little different because there
21716c07 349# can only be one per name, whereas the other
350# method modifiers can have multiples.
351# - SL
0558683c 352
21716c07 353sub add_override_method_modifier {
354 my ($self, $method_name, $method) = @_;
355 (!$self->has_method($method_name))
c245d69b 356 || Moose->throw_error("Cannot add an override of method '$method_name' " .
4c0b3599 357 "because there is a local version of '$method_name'");
fb1e11d5 358 $self->get_override_method_modifiers_map->{$method_name} = $method;
21716c07 359}
0558683c 360
21716c07 361sub has_override_method_modifier {
362 my ($self, $method_name) = @_;
363 # NOTE:
fb1e11d5 364 # for now we assume that if it exists,..
21716c07 365 # it has at least one modifier in it
fb1e11d5 366 (exists $self->get_override_method_modifiers_map->{$method_name}) ? 1 : 0;
21716c07 367}
0558683c 368
21716c07 369sub get_override_method_modifier {
370 my ($self, $method_name) = @_;
fb1e11d5 371 $self->get_override_method_modifiers_map->{$method_name};
21716c07 372}
0558683c 373
21716c07 374## general list accessor ...
80572233 375
21716c07 376sub get_method_modifier_list {
377 my ($self, $modifier_type) = @_;
fb1e11d5 378 my $accessor = "get_${modifier_type}_method_modifiers_map";
21716c07 379 keys %{$self->$accessor};
380}
e185c027 381
e606ae5f 382sub reset_package_cache_flag { (shift)->{'_package_cache_flag'} = undef }
383sub update_package_cache_flag {
384 my $self = shift;
385 $self->{'_package_cache_flag'} = Class::MOP::check_package_cache_flag($self->name);
386}
387
388
699a2e32 389sub _meta_method_class { 'Moose::Meta::Method::Meta' }
e606ae5f 390
21716c07 391## ------------------------------------------------------------------
80572233 392## subroles
393
278ca49c 394$META->add_attribute('roles' => (
21716c07 395 reader => 'get_roles',
396 default => sub { [] }
397));
398
80572233 399sub add_role {
400 my ($self, $role) = @_;
401 (blessed($role) && $role->isa('Moose::Meta::Role'))
c245d69b 402 || Moose->throw_error("Roles must be instances of Moose::Meta::Role");
80572233 403 push @{$self->get_roles} => $role;
e606ae5f 404 $self->reset_package_cache_flag;
80572233 405}
406
b8aeb4dc 407sub calculate_all_roles {
408 my $self = shift;
409 my %seen;
fb1e11d5 410 grep {
411 !$seen{$_->name}++
412 } ($self, map {
413 $_->calculate_all_roles
414 } @{ $self->get_roles });
b8aeb4dc 415}
416
80572233 417sub does_role {
560c498d 418 my ($self, $role) = @_;
419 (defined $role)
c245d69b 420 || Moose->throw_error("You must supply a role name to look for");
560c498d 421 my $role_name = blessed $role ? $role->name : $role;
bdabd620 422 # if we are it,.. then return true
423 return 1 if $role_name eq $self->name;
424 # otherwise.. check our children
80572233 425 foreach my $role (@{$self->get_roles}) {
bdabd620 426 return 1 if $role->does_role($role_name);
80572233 427 }
428 return 0;
429}
430
1db8ecc7 431sub find_method_by_name { (shift)->get_method(@_) }
093b12c2 432
21716c07 433## ------------------------------------------------------------------
fb1e11d5 434## role construction
21716c07 435## ------------------------------------------------------------------
e185c027 436
21716c07 437sub apply {
f315aab3 438 my ($self, $other, %args) = @_;
bca01282 439
440 (blessed($other))
c245d69b 441 || Moose->throw_error("You must pass in an blessed instance");
d03bd989 442
07de7559 443 my $application_class;
1c9db35c 444 if ($other->isa('Moose::Meta::Role')) {
07de7559 445 $application_class = $self->application_to_role_class;
1c9db35c 446 }
447 elsif ($other->isa('Moose::Meta::Class')) {
07de7559 448 $application_class = $self->application_to_class_class;
d03bd989 449 }
1c9db35c 450 else {
07de7559 451 $application_class = $self->application_to_instance_class;
d03bd989 452 }
07de7559 453
454 Class::MOP::load_class($application_class);
4c52e860 455
456 my $deprecation_check = 0;
457
458 if ( exists $args{excludes} && !exists $args{'-excludes'} ) {
459 $args{'-excludes'} = delete $args{excludes};
460 $deprecation_check = 1;
461 }
462 if ( exists $args{alias} && !exists $args{'-alias'} ) {
463 $args{'-alias'} = delete $args{alias};
464 $deprecation_check = 1;
465 }
466
467 if ( $deprecation_check ) {
4c52e860 468 Moose::Deprecated::deprecated(
469 feature => 'alias or excludes',
470 message =>
471 'The alias and excludes options for role application'.
472 ' have been renamed -alias and -excludes'.
473 " (${\$other->name} is consuming ${\$self->name}".
474 " - do you need to upgrade ${\$other->name}?)"
475 );
476 }
477
478 if ( exists $args{'-excludes'} ) {
4c52e860 479 # I wish we had coercion here :)
480 $args{'-excludes'} = (
481 ref $args{'-excludes'} eq 'ARRAY'
482 ? $args{'-excludes'}
483 : [ $args{'-excludes'} ]
484 );
485 }
486
f315aab3 487 return $application_class->new(%args)->apply($self, $other, \%args);
0558683c 488}
489
4701ceff 490sub composition_class_roles { }
491
21716c07 492sub combine {
a4e516f6 493 my ($class, @role_specs) = @_;
d03bd989 494
d03bd989 495 require Moose::Meta::Role::Composite;
496
28412c0b 497 my (@roles, %role_params);
498 while (@role_specs) {
560c498d 499 my ($role, $params) = @{ splice @role_specs, 0, 1 };
500 my $requested_role
501 = blessed $role
502 ? $role
503 : Class::MOP::class_of($role);
31d96c3e 504
6bdef412 505 my $actual_role = $requested_role->_role_for_combination($params);
31d96c3e 506 push @roles => $actual_role;
507
28412c0b 508 next unless defined $params;
31d96c3e 509 $role_params{$actual_role->name} = $params;
28412c0b 510 }
d03bd989 511
fb1e11d5 512 my $c = Moose::Meta::Role::Composite->new(roles => \@roles);
7071e2cb 513 return $c->apply_params(\%role_params);
0558683c 514}
515
6bdef412 516sub _role_for_combination {
436d7a28 517 my ($self, $params) = @_;
518 return $self;
519}
520
b6a00b82 521sub create {
2d5f3fab 522 my ( $role, $package_name, %options ) = @_;
b6a00b82 523
2d5f3fab 524 $options{package} = $package_name;
b6a00b82 525
526 (ref $options{attributes} eq 'HASH')
527 || confess "You must pass a HASH ref of attributes"
528 if exists $options{attributes};
529
530 (ref $options{methods} eq 'HASH')
531 || confess "You must pass a HASH ref of methods"
532 if exists $options{methods};
533
2937ed18 534 $options{meta_name} = 'meta'
535 unless exists $options{meta_name};
536
2d5f3fab 537 my (%initialize_options) = %options;
b6a00b82 538 delete @initialize_options{qw(
539 package
540 attributes
541 methods
2937ed18 542 meta_name
b6a00b82 543 version
544 authority
545 )};
546
547 my $meta = $role->initialize( $package_name => %initialize_options );
548
6a4a7c31 549 $meta->_instantiate_module( $options{version}, $options{authority} );
550
2937ed18 551 $meta->_add_meta_method($options{meta_name})
552 if defined $options{meta_name};
b6a00b82 553
554 if (exists $options{attributes}) {
555 foreach my $attribute_name (keys %{$options{attributes}}) {
556 my $attr = $options{attributes}->{$attribute_name};
f785aad8 557 $meta->add_attribute(
558 $attribute_name => blessed $attr ? $attr : %{$attr} );
b6a00b82 559 }
560 }
561
562 if (exists $options{methods}) {
563 foreach my $method_name (keys %{$options{methods}}) {
564 $meta->add_method($method_name, $options{methods}->{$method_name});
565 }
566 }
567
568 return $meta;
569}
570
c23885b1 571sub consumers {
572 my $self = shift;
573 my @consumers;
574 for my $meta (Class::MOP::get_all_metaclass_instances) {
575 next if $meta->name eq $self->name;
576 next unless $meta->isa('Moose::Meta::Class')
577 || $meta->isa('Moose::Meta::Role');
578 push @consumers, $meta->name
579 if $meta->does_role($self->name);
580 }
581 return @consumers;
582}
583
c9ee520d 584# anonymous roles. most of it is copied straight out of Class::MOP::Class.
585# an intrepid hacker might find great riches if he unifies this code with that
586# code in Class::MOP::Module or Class::MOP::Package
587{
588 # NOTE:
589 # this should be sufficient, if you have a
590 # use case where it is not, write a test and
591 # I will change it.
592 my $ANON_ROLE_SERIAL = 0;
593
594 # NOTE:
595 # we need a sufficiently annoying prefix
596 # this should suffice for now, this is
597 # used in a couple of places below, so
598 # need to put it up here for now.
599 my $ANON_ROLE_PREFIX = 'Moose::Meta::Role::__ANON__::SERIAL::';
600
601 sub is_anon_role {
602 my $self = shift;
603 no warnings 'uninitialized';
604 $self->name =~ /^$ANON_ROLE_PREFIX/;
605 }
606
607 sub create_anon_role {
608 my ($role, %options) = @_;
6f73c555 609 $options{weaken} = 1 unless exists $options{weaken};
c9ee520d 610 my $package_name = $ANON_ROLE_PREFIX . ++$ANON_ROLE_SERIAL;
611 return $role->create($package_name, %options);
612 }
613
614 # NOTE:
615 # this will only get called for
616 # anon-roles, all other calls
617 # are assumed to occur during
618 # global destruction and so don't
619 # really need to be handled explicitly
620 sub DESTROY {
621 my $self = shift;
622
963c24e1 623 return if in_global_destruction(); # it'll happen soon anyway and this just makes things more complicated
c9ee520d 624
625 no warnings 'uninitialized';
626 return unless $self->name =~ /^$ANON_ROLE_PREFIX/;
627
628 # XXX: is this necessary for us? I don't understand what it's doing
629 # -sartak
630
631 # Moose does a weird thing where it replaces the metaclass for
632 # class when fixing metaclass incompatibility. In that case,
633 # we don't want to clean out the namespace now. We can detect
634 # that because Moose will explicitly update the singleton
635 # cache in Class::MOP.
636 #my $current_meta = Class::MOP::get_metaclass_by_name($self->name);
637 #return if $current_meta ne $self;
638
639 my ($serial_id) = ($self->name =~ /^$ANON_ROLE_PREFIX(\d+)/);
640 no strict 'refs';
641 foreach my $key (keys %{$ANON_ROLE_PREFIX . $serial_id}) {
642 delete ${$ANON_ROLE_PREFIX . $serial_id}{$key};
643 }
644 delete ${'main::' . $ANON_ROLE_PREFIX}{$serial_id . '::'};
645 }
646}
647
fb1e11d5 648#####################################################################
649## NOTE:
d03bd989 650## This is Moose::Meta::Role as defined by Moose (plus the use of
651## MooseX::AttributeHelpers module). It is here as a reference to
fb1e11d5 652## make it easier to see what is happening above with all the meta
653## programming. - SL
654#####################################################################
655#
656# has 'roles' => (
a40b446a 657# metaclass => 'Array',
fb1e11d5 658# reader => 'get_roles',
764a7831 659# isa => 'ArrayRef[Moose::Meta::Role]',
fb1e11d5 660# default => sub { [] },
661# provides => {
662# 'push' => 'add_role',
663# }
664# );
d03bd989 665#
fb1e11d5 666# has 'excluded_roles_map' => (
a40b446a 667# metaclass => 'Hash',
fb1e11d5 668# reader => 'get_excluded_roles_map',
669# isa => 'HashRef[Str]',
670# provides => {
671# # Not exactly set, cause it sets multiple
672# 'set' => 'add_excluded_roles',
673# 'keys' => 'get_excluded_roles_list',
674# 'exists' => 'excludes_role',
675# }
676# );
d03bd989 677#
fb1e11d5 678# has 'required_methods' => (
a40b446a 679# metaclass => 'Hash',
fb1e11d5 680# reader => 'get_required_methods_map',
72264f10 681# isa => 'HashRef[Moose::Meta::Role::Method::Required]',
d03bd989 682# provides => {
683# # not exactly set, or delete since it works for multiple
fb1e11d5 684# 'set' => 'add_required_methods',
685# 'delete' => 'remove_required_methods',
686# 'keys' => 'get_required_method_list',
d03bd989 687# 'exists' => 'requires_method',
fb1e11d5 688# }
689# );
d03bd989 690#
691# # the before, around and after modifiers are
692# # HASH keyed by method-name, with ARRAY of
fb1e11d5 693# # CODE refs to apply in that order
d03bd989 694#
fb1e11d5 695# has 'before_method_modifiers' => (
a40b446a 696# metaclass => 'Hash',
fb1e11d5 697# reader => 'get_before_method_modifiers_map',
698# isa => 'HashRef[ArrayRef[CodeRef]]',
699# provides => {
700# 'keys' => 'get_before_method_modifiers',
d03bd989 701# 'exists' => 'has_before_method_modifiers',
702# # This actually makes sure there is an
fb1e11d5 703# # ARRAY at the given key, and pushed onto
704# # it. It also checks for duplicates as well
d03bd989 705# # 'add' => 'add_before_method_modifier'
706# }
fb1e11d5 707# );
d03bd989 708#
fb1e11d5 709# has 'after_method_modifiers' => (
a40b446a 710# metaclass => 'Hash',
fb1e11d5 711# reader =>'get_after_method_modifiers_map',
712# isa => 'HashRef[ArrayRef[CodeRef]]',
713# provides => {
714# 'keys' => 'get_after_method_modifiers',
d03bd989 715# 'exists' => 'has_after_method_modifiers',
716# # This actually makes sure there is an
fb1e11d5 717# # ARRAY at the given key, and pushed onto
d03bd989 718# # it. It also checks for duplicates as well
719# # 'add' => 'add_after_method_modifier'
720# }
fb1e11d5 721# );
d03bd989 722#
fb1e11d5 723# has 'around_method_modifiers' => (
a40b446a 724# metaclass => 'Hash',
fb1e11d5 725# reader =>'get_around_method_modifiers_map',
726# isa => 'HashRef[ArrayRef[CodeRef]]',
727# provides => {
728# 'keys' => 'get_around_method_modifiers',
d03bd989 729# 'exists' => 'has_around_method_modifiers',
730# # This actually makes sure there is an
fb1e11d5 731# # ARRAY at the given key, and pushed onto
d03bd989 732# # it. It also checks for duplicates as well
733# # 'add' => 'add_around_method_modifier'
734# }
fb1e11d5 735# );
d03bd989 736#
fb1e11d5 737# # override is similar to the other modifiers
738# # except that it is not an ARRAY of code refs
739# # but instead just a single name->code mapping
d03bd989 740#
fb1e11d5 741# has 'override_method_modifiers' => (
a40b446a 742# metaclass => 'Hash',
fb1e11d5 743# reader =>'get_override_method_modifiers_map',
d03bd989 744# isa => 'HashRef[CodeRef]',
fb1e11d5 745# provides => {
746# 'keys' => 'get_override_method_modifier',
d03bd989 747# 'exists' => 'has_override_method_modifier',
748# 'add' => 'add_override_method_modifier', # checks for local method ..
fb1e11d5 749# }
750# );
d03bd989 751#
fb1e11d5 752#####################################################################
753
754
e185c027 7551;
756
757__END__
758
759=pod
760
761=head1 NAME
762
763Moose::Meta::Role - The Moose Role metaclass
764
765=head1 DESCRIPTION
766
705d4cfd 767This class is a subclass of L<Class::MOP::Module> that provides
768additional Moose-specific functionality.
769
770It's API looks a lot like L<Moose::Meta::Class>, but internally it
771implements many things differently. This may change in the future.
79592a54 772
cf3bb5c5 773=head1 INHERITANCE
774
775C<Moose::Meta::Role> is a subclass of L<Class::MOP::Module>.
776
e185c027 777=head1 METHODS
778
705d4cfd 779=head2 Construction
780
e185c027 781=over 4
782
705d4cfd 783=item B<< Moose::Meta::Role->initialize($role_name) >>
e185c027 784
705d4cfd 785This method creates a new role object with the provided name.
e185c027 786
705d4cfd 787=item B<< Moose::Meta::Role->combine( [ $role => { ... } ], [ $role ], ... ) >>
78cd1d3b 788
705d4cfd 789This method accepts a list of array references. Each array reference
560c498d 790should contain a role name or L<Moose::Meta::Role> object as its first element. The second element is
c8b8d92f 791an optional hash reference. The hash reference can contain C<-excludes>
792and C<-alias> keys to control how methods are composed from the role.
e606ae5f 793
705d4cfd 794The return value is a new L<Moose::Meta::Role::Composite> that
795represents the combined roles.
db1ab48d 796
4701ceff 797=item B<< $metarole->composition_class_roles >>
798
799When combining multiple roles using C<combine>, this method is used to obtain a
800list of role names to be applied to the L<Moose::Meta::Role::Composite>
801instance returned by C<combine>. The default implementation returns an empty
802list. Extensions that need to hook into role combination may wrap this method
803to return additional role names.
804
705d4cfd 805=item B<< Moose::Meta::Role->create($name, %options) >>
e185c027 806
705d4cfd 807This method is identical to the L<Moose::Meta::Class> C<create>
808method.
809
810=item B<< Moose::Meta::Role->create_anon_role >>
e185c027 811
705d4cfd 812This method is identical to the L<Moose::Meta::Class>
813C<create_anon_class> method.
e185c027 814
705d4cfd 815=item B<< $metarole->is_anon_role >>
e185c027 816
705d4cfd 817Returns true if the role is an anonymous role.
e185c027 818
c23885b1 819=item B<< $metarole->consumers >>
820
821Returns a list of names of classes and roles which consume this role.
822
e185c027 823=back
824
705d4cfd 825=head2 Role application
826
e185c027 827=over 4
828
705d4cfd 829=item B<< $metarole->apply( $thing, @options ) >>
80572233 830
705d4cfd 831This method applies a role to the given C<$thing>. That can be another
832L<Moose::Meta::Role>, object, a L<Moose::Meta::Class> object, or a
833(non-meta) object instance.
80572233 834
705d4cfd 835The options are passed directly to the constructor for the appropriate
836L<Moose::Meta::Role::Application> subclass.
80572233 837
d05235e1 838Note that this will apply the role even if the C<$thing> in question already
839C<does> this role. L<Moose::Util/does_role> is a convenient wrapper for
840finding out if role application is necessary.
841
80572233 842=back
843
705d4cfd 844=head2 Roles and other roles
845
80572233 846=over 4
847
705d4cfd 848=item B<< $metarole->get_roles >>
d79e62fd 849
705d4cfd 850This returns an array reference of roles which this role does. This
851list may include duplicates.
d79e62fd 852
705d4cfd 853=item B<< $metarole->calculate_all_roles >>
d79e62fd 854
705d4cfd 855This returns a I<unique> list of all roles that this role does, and
856all the roles that its roles do.
d79e62fd 857
560c498d 858=item B<< $metarole->does_role($role) >>
2b14ac61 859
560c498d 860Given a role I<name> or L<Moose::Meta::Role> object, returns true if this role
861does the given role.
d79e62fd 862
705d4cfd 863=item B<< $metarole->add_role($role) >>
d79e62fd 864
705d4cfd 865Given a L<Moose::Meta::Role> object, this adds the role to the list of
866roles that the role does.
68efb014 867
705d4cfd 868=item B<< $metarole->get_excluded_roles_list >>
be4427d0 869
705d4cfd 870Returns a list of role names which this role excludes.
e185c027 871
705d4cfd 872=item B<< $metarole->excludes_role($role_name) >>
e185c027 873
705d4cfd 874Given a role I<name>, returns true if this role excludes the named
875role.
e606ae5f 876
705d4cfd 877=item B<< $metarole->add_excluded_roles(@role_names) >>
e606ae5f 878
705d4cfd 879Given one or more role names, adds those roles to the list of excluded
880roles.
bdabd620 881
705d4cfd 882=back
e185c027 883
705d4cfd 884=head2 Methods
093b12c2 885
705d4cfd 886The methods for dealing with a role's methods are all identical in API
887and behavior to the same methods in L<Class::MOP::Class>.
638853cb 888
705d4cfd 889=over 4
53dd42d8 890
705d4cfd 891=item B<< $metarole->method_metaclass >>
e185c027 892
705d4cfd 893Returns the method metaclass name for the role. This defaults to
894L<Moose::Meta::Role::Method>.
e185c027 895
705d4cfd 896=item B<< $metarole->get_method($name) >>
e185c027 897
705d4cfd 898=item B<< $metarole->has_method($name) >>
e185c027 899
705d4cfd 900=item B<< $metarole->add_method( $name, $body ) >>
e185c027 901
705d4cfd 902=item B<< $metarole->get_method_list >>
e185c027 903
705d4cfd 904=item B<< $metarole->find_method_by_name($name) >>
905
906These methods are all identical to the methods of the same name in
8f6cbfcd 907L<Class::MOP::Package>
e185c027 908
909=back
910
705d4cfd 911=head2 Attributes
912
913As with methods, the methods for dealing with a role's attribute are
914all identical in API and behavior to the same methods in
915L<Class::MOP::Class>.
916
917However, attributes stored in this class are I<not> stored as
918objects. Rather, the attribute definition is stored as a hash
919reference. When a role is composed into a class, this hash reference
920is passed directly to the metaclass's C<add_attribute> method.
921
922This is quite likely to change in the future.
923
e185c027 924=over 4
925
705d4cfd 926=item B<< $metarole->get_attribute($attribute_name) >>
927
928=item B<< $metarole->has_attribute($attribute_name) >>
1331430a 929
705d4cfd 930=item B<< $metarole->get_attribute_list >>
1331430a 931
705d4cfd 932=item B<< $metarole->add_attribute($name, %options) >>
1331430a 933
705d4cfd 934=item B<< $metarole->remove_attribute($attribute_name) >>
1331430a 935
936=back
937
705d4cfd 938=head2 Required methods
939
0558683c 940=over 4
941
705d4cfd 942=item B<< $metarole->get_required_method_list >>
0558683c 943
705d4cfd 944Returns the list of methods required by the role.
0558683c 945
705d4cfd 946=item B<< $metarole->requires_method($name) >>
0558683c 947
705d4cfd 948Returns true if the role requires the named method.
0558683c 949
0ecff16b 950=item B<< $metarole->add_required_methods(@names) >>
0558683c 951
b3c1586a 952Adds the named methods to the role's list of required methods.
0558683c 953
705d4cfd 954=item B<< $metarole->remove_required_methods(@names) >>
0558683c 955
2dda1d28 956Removes the named methods from the role's list of required methods.
0558683c 957
bb153262 958=item B<< $metarole->add_conflicting_method(%params) >>
09eeab06 959
bb153262 960Instantiate the parameters as a L<Moose::Meta::Role::Method::Conflicting>
09eeab06 961object, then add it to the required method list.
962
705d4cfd 963=back
964
965=head2 Method modifiers
0558683c 966
fa907f2e 967These methods act like their counterparts in L<Class::MOP::Class> and
705d4cfd 968L<Moose::Meta::Class>.
969
970However, method modifiers are simply stored internally, and are not
971applied until the role itself is applied to a class.
0558683c 972
973=over 4
974
705d4cfd 975=item B<< $metarole->add_after_method_modifier($method_name, $method) >>
0558683c 976
705d4cfd 977=item B<< $metarole->add_around_method_modifier($method_name, $method) >>
0558683c 978
705d4cfd 979=item B<< $metarole->add_before_method_modifier($method_name, $method) >>
0558683c 980
705d4cfd 981=item B<< $metarole->add_override_method_modifier($method_name, $method) >>
0558683c 982
705d4cfd 983These methods all add an appropriate modifier to the internal list of
984modifiers.
0558683c 985
705d4cfd 986=item B<< $metarole->has_after_method_modifiers >>
0558683c 987
705d4cfd 988=item B<< $metarole->has_around_method_modifiers >>
989
990=item B<< $metarole->has_before_method_modifiers >>
991
992=item B<< $metarole->has_override_method_modifier >>
0558683c 993
705d4cfd 994Return true if the role has any modifiers of the given type.
0558683c 995
705d4cfd 996=item B<< $metarole->get_after_method_modifiers($method_name) >>
0558683c 997
705d4cfd 998=item B<< $metarole->get_around_method_modifiers($method_name) >>
0558683c 999
705d4cfd 1000=item B<< $metarole->get_before_method_modifiers($method_name) >>
0558683c 1001
705d4cfd 1002Given a method name, returns a list of the appropriate modifiers for
1003that method.
1004
1005=item B<< $metarole->get_override_method_modifier($method_name) >>
1006
1007Given a method name, returns the override method modifier for that
1008method, if it has one.
0558683c 1009
1010=back
1011
705d4cfd 1012=head2 Introspection
505065c4 1013
705d4cfd 1014=over 4
505065c4 1015
705d4cfd 1016=item B<< Moose::Meta::Role->meta >>
505065c4 1017
705d4cfd 1018This will return a L<Class::MOP::Class> instance for this class.
505065c4 1019
1020=back
1021
e185c027 1022=head1 BUGS
1023
d4048ef3 1024See L<Moose/BUGS> for details on reporting bugs.
e185c027 1025
1026=head1 AUTHOR
1027
1028Stevan Little E<lt>stevan@iinteractive.comE<gt>
1029
1030=head1 COPYRIGHT AND LICENSE
1031
7e0492d3 1032Copyright 2006-2010 by Infinity Interactive, Inc.
e185c027 1033
1034L<http://www.iinteractive.com>
1035
1036This library is free software; you can redistribute it and/or modify
fb1e11d5 1037it under the same terms as Perl itself.
e185c027 1038
b8aeb4dc 1039=cut