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