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