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