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