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