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