Merge 'Moose-moosex_compile_support' into 'trunk'
[gitmo/Moose.git] / lib / Moose / Meta / Role.pm
CommitLineData
e185c027 1
2package Moose::Meta::Role;
3
4use strict;
5use warnings;
6use metaclass;
7
fb1e11d5 8use Sub::Name 'subname';
bdabd620 9use Carp 'confess';
fb1e11d5 10use Scalar::Util 'blessed', 'reftype';
bdabd620 11
fb1e11d5 12our $VERSION = '0.12';
d44714be 13our $AUTHORITY = 'cpan:STEVAN';
e185c027 14
8ee73eeb 15use Moose::Meta::Class;
39b3bc94 16use Moose::Meta::Role::Method;
d67145ed 17use Moose::Meta::Role::Method::Required;
8ee73eeb 18
68efb014 19use base 'Class::MOP::Module';
80572233 20
fb1e11d5 21## ------------------------------------------------------------------
22## NOTE:
23## I normally don't do this, but I am doing
24## a whole bunch of meta-programmin in this
25## module, so it just makes sense. For a clearer
26## picture of what is going on in the next
27## several lines of code, look at the really
28## big comment at the end of this file (right
29## before the POD).
30## - SL
31## ------------------------------------------------------------------
80572233 32
21716c07 33my $META = __PACKAGE__->meta;
34
35## ------------------------------------------------------------------
36## attributes ...
e185c027 37
21716c07 38# NOTE:
39# since roles are lazy, we hold all the attributes
fb1e11d5 40# of the individual role in 'statis' until which
41# time when it is applied to a class. This means
42# keeping a lot of things in hash maps, so we are
21716c07 43# using a little of that meta-programmin' magic
fb1e11d5 44# here an saving lots of extra typin. And since
45# many of these attributes above require similar
46# functionality to support them, so we again use
47# the wonders of meta-programmin' to deliver a
21716c07 48# very compact solution to this normally verbose
49# problem.
50# - SL
51
52foreach my $action (
fb1e11d5 53 {
54 name => 'excluded_roles_map',
55 attr_reader => 'get_excluded_roles_map' ,
21716c07 56 methods => {
fb1e11d5 57 add => 'add_excluded_roles',
58 get_list => 'get_excluded_roles_list',
59 existence => 'excludes_role',
21716c07 60 }
61 },
fb1e11d5 62 {
63 name => 'required_methods',
21716c07 64 attr_reader => 'get_required_methods_map',
65 methods => {
fb1e11d5 66 add => 'add_required_methods',
21716c07 67 remove => 'remove_required_methods',
68 get_list => 'get_required_method_list',
69 existence => 'requires_method',
70 }
709c321c 71 },
21716c07 72 {
fb1e11d5 73 name => 'attribute_map',
21716c07 74 attr_reader => 'get_attribute_map',
75 methods => {
76 get => 'get_attribute',
77 get_list => 'get_attribute_list',
78 existence => 'has_attribute',
79 remove => 'remove_attribute',
80 }
81 }
82) {
fb1e11d5 83
21716c07 84 my $attr_reader = $action->{attr_reader};
85 my $methods = $action->{methods};
fb1e11d5 86
87 # create the attribute
88 $META->add_attribute($action->{name} => (
89 reader => $attr_reader,
90 default => sub { {} }
91 ));
92
93 # create some helper methods
21716c07 94 $META->add_method($methods->{add} => sub {
95 my ($self, @values) = @_;
fb1e11d5 96 $self->$attr_reader->{$_} = undef foreach @values;
21716c07 97 }) if exists $methods->{add};
fb1e11d5 98
21716c07 99 $META->add_method($methods->{get_list} => sub {
100 my ($self) = @_;
fb1e11d5 101 keys %{$self->$attr_reader};
102 }) if exists $methods->{get_list};
103
21716c07 104 $META->add_method($methods->{get} => sub {
105 my ($self, $name) = @_;
fb1e11d5 106 $self->$attr_reader->{$name}
107 }) if exists $methods->{get};
108
21716c07 109 $META->add_method($methods->{existence} => sub {
110 my ($self, $name) = @_;
fb1e11d5 111 exists $self->$attr_reader->{$name} ? 1 : 0;
112 }) if exists $methods->{existence};
113
21716c07 114 $META->add_method($methods->{remove} => sub {
115 my ($self, @values) = @_;
116 delete $self->$attr_reader->{$_} foreach @values;
fb1e11d5 117 }) if exists $methods->{remove};
21716c07 118}
d79e62fd 119
21716c07 120## some things don't always fit, so they go here ...
80572233 121
21716c07 122sub add_attribute {
123 my $self = shift;
124 my $name = shift;
125 my $attr_desc;
126 if (scalar @_ == 1 && ref($_[0]) eq 'HASH') {
127 $attr_desc = $_[0];
128 }
129 else {
130 $attr_desc = { @_ };
131 }
132 $self->get_attribute_map->{$name} = $attr_desc;
133}
e185c027 134
bca01282 135# DEPRECATED
136# sub _clean_up_required_methods {
137# my $self = shift;
138# foreach my $method ($self->get_required_method_list) {
139# $self->remove_required_methods($method)
140# if $self->has_method($method);
141# }
142# }
1331430a 143
21716c07 144## ------------------------------------------------------------------
145## method modifiers
146
21716c07 147# NOTE:
fb1e11d5 148# the before/around/after method modifiers are
21716c07 149# stored by name, but there can be many methods
150# then associated with that name. So again we have
151# lots of similar functionality, so we can do some
152# meta-programmin' and save some time.
153# - SL
154
155foreach my $modifier_type (qw[ before around after ]) {
fb1e11d5 156
157 my $attr_reader = "get_${modifier_type}_method_modifiers_map";
21716c07 158
fb1e11d5 159 # create the attribute ...
160 $META->add_attribute("${modifier_type}_method_modifiers" => (
161 reader => $attr_reader,
162 default => sub { {} }
163 ));
164
165 # and some helper methods ...
21716c07 166 $META->add_method("get_${modifier_type}_method_modifiers" => sub {
167 my ($self, $method_name) = @_;
fb1e11d5 168 #return () unless exists $self->$attr_reader->{$method_name};
21716c07 169 @{$self->$attr_reader->{$method_name}};
170 });
fb1e11d5 171
21716c07 172 $META->add_method("has_${modifier_type}_method_modifiers" => sub {
173 my ($self, $method_name) = @_;
174 # NOTE:
fb1e11d5 175 # for now we assume that if it exists,..
21716c07 176 # it has at least one modifier in it
177 (exists $self->$attr_reader->{$method_name}) ? 1 : 0;
fb1e11d5 178 });
179
21716c07 180 $META->add_method("add_${modifier_type}_method_modifier" => sub {
181 my ($self, $method_name, $method) = @_;
fb1e11d5 182
183 $self->$attr_reader->{$method_name} = []
21716c07 184 unless exists $self->$attr_reader->{$method_name};
fb1e11d5 185
21716c07 186 my $modifiers = $self->$attr_reader->{$method_name};
fb1e11d5 187
21716c07 188 # NOTE:
fb1e11d5 189 # check to see that we aren't adding the
190 # same code twice. We err in favor of the
21716c07 191 # first on here, this may not be as expected
192 foreach my $modifier (@{$modifiers}) {
193 return if $modifier == $method;
194 }
fb1e11d5 195
21716c07 196 push @{$modifiers} => $method;
197 });
fb1e11d5 198
21716c07 199}
1331430a 200
21716c07 201## ------------------------------------------------------------------
202## override method mofidiers
0558683c 203
fb1e11d5 204$META->add_attribute('override_method_modifiers' => (
205 reader => 'get_override_method_modifiers_map',
206 default => sub { {} }
207));
208
21716c07 209# NOTE:
fb1e11d5 210# these are a little different because there
21716c07 211# can only be one per name, whereas the other
212# method modifiers can have multiples.
213# - SL
0558683c 214
21716c07 215sub add_override_method_modifier {
216 my ($self, $method_name, $method) = @_;
217 (!$self->has_method($method_name))
fb1e11d5 218 || confess "Cannot add an override of method '$method_name' " .
21716c07 219 "because there is a local version of '$method_name'";
fb1e11d5 220 $self->get_override_method_modifiers_map->{$method_name} = $method;
21716c07 221}
0558683c 222
21716c07 223sub has_override_method_modifier {
224 my ($self, $method_name) = @_;
225 # NOTE:
fb1e11d5 226 # for now we assume that if it exists,..
21716c07 227 # it has at least one modifier in it
fb1e11d5 228 (exists $self->get_override_method_modifiers_map->{$method_name}) ? 1 : 0;
21716c07 229}
0558683c 230
21716c07 231sub get_override_method_modifier {
232 my ($self, $method_name) = @_;
fb1e11d5 233 $self->get_override_method_modifiers_map->{$method_name};
21716c07 234}
0558683c 235
21716c07 236## general list accessor ...
80572233 237
21716c07 238sub get_method_modifier_list {
239 my ($self, $modifier_type) = @_;
fb1e11d5 240 my $accessor = "get_${modifier_type}_method_modifiers_map";
21716c07 241 keys %{$self->$accessor};
242}
e185c027 243
21716c07 244## ------------------------------------------------------------------
80572233 245## subroles
246
21716c07 247__PACKAGE__->meta->add_attribute('roles' => (
248 reader => 'get_roles',
249 default => sub { [] }
250));
251
80572233 252sub add_role {
253 my ($self, $role) = @_;
254 (blessed($role) && $role->isa('Moose::Meta::Role'))
255 || confess "Roles must be instances of Moose::Meta::Role";
256 push @{$self->get_roles} => $role;
257}
258
b8aeb4dc 259sub calculate_all_roles {
260 my $self = shift;
261 my %seen;
fb1e11d5 262 grep {
263 !$seen{$_->name}++
264 } ($self, map {
265 $_->calculate_all_roles
266 } @{ $self->get_roles });
b8aeb4dc 267}
268
80572233 269sub does_role {
270 my ($self, $role_name) = @_;
271 (defined $role_name)
272 || confess "You must supply a role name to look for";
bdabd620 273 # if we are it,.. then return true
274 return 1 if $role_name eq $self->name;
275 # otherwise.. check our children
80572233 276 foreach my $role (@{$self->get_roles}) {
bdabd620 277 return 1 if $role->does_role($role_name);
80572233 278 }
279 return 0;
280}
281
21716c07 282## ------------------------------------------------------------------
fb1e11d5 283## methods
1331430a 284
21716c07 285sub method_metaclass { 'Moose::Meta::Role::Method' }
80572233 286
fb1e11d5 287sub get_method_map {
093b12c2 288 my $self = shift;
fb1e11d5 289 my $map = {};
290
291 my $role_name = $self->name;
292 my $method_metaclass = $self->method_metaclass;
293
294 foreach my $symbol ($self->list_all_package_symbols('CODE')) {
295
296 my $code = $self->get_package_symbol('&' . $symbol);
297
298 my ($pkg, $name) = Class::MOP::get_code_info($code);
299
300 if ($pkg->can('meta')
301 # NOTE:
302 # we don't know what ->meta we are calling
303 # here, so we need to be careful cause it
304 # just might blow up at us, or just complain
305 # loudly (in the case of Curses.pm) so we
306 # just be a little overly cautious here.
307 # - SL
308 && eval { no warnings; blessed($pkg->meta) }
309 && $pkg->meta->isa('Moose::Meta::Role')) {
310 my $role = $pkg->meta->name;
311 next unless $self->does_role($role);
312 }
313 else {
314 next if ($pkg || '') ne $role_name &&
315 ($name || '') ne '__ANON__';
316 }
c4538447 317
fb1e11d5 318 $map->{$symbol} = $method_metaclass->wrap($code);
319 }
320
321 return $map;
093b12c2 322}
323
fb1e11d5 324sub get_method {
325 my ($self, $name) = @_;
c4538447 326 $self->get_method_map->{$name};
fb1e11d5 327}
40e89659 328
fb1e11d5 329sub has_method {
330 my ($self, $name) = @_;
331 exists $self->get_method_map->{$name} ? 1 : 0
e185c027 332}
333
1db8ecc7 334sub find_method_by_name { (shift)->get_method(@_) }
093b12c2 335
fb1e11d5 336sub get_method_list {
337 my $self = shift;
338 grep { !/^meta$/ } keys %{$self->get_method_map};
339}
340
341sub alias_method {
342 my ($self, $method_name, $method) = @_;
343 (defined $method_name && $method_name)
344 || confess "You must define a method name";
345
346 my $body = (blessed($method) ? $method->body : $method);
347 ('CODE' eq (reftype($body) || ''))
348 || confess "Your code block must be a CODE reference";
349
350 $self->add_package_symbol("&${method_name}" => $body);
351}
352
21716c07 353## ------------------------------------------------------------------
fb1e11d5 354## role construction
21716c07 355## ------------------------------------------------------------------
e185c027 356
21716c07 357sub apply {
bca01282 358 my ($self, $other, @args) = @_;
359
360 (blessed($other))
361 || confess "You must pass in an blessed instance";
362
1c9db35c 363 if ($other->isa('Moose::Meta::Role')) {
364 require Moose::Meta::Role::Application::ToRole;
709c321c 365 return Moose::Meta::Role::Application::ToRole->new(@args)->apply($self, $other);
1c9db35c 366 }
367 elsif ($other->isa('Moose::Meta::Class')) {
368 require Moose::Meta::Role::Application::ToClass;
709c321c 369 return Moose::Meta::Role::Application::ToClass->new(@args)->apply($self, $other);
1c9db35c 370 }
371 else {
372 require Moose::Meta::Role::Application::ToInstance;
709c321c 373 return Moose::Meta::Role::Application::ToInstance->new(@args)->apply($self, $other);
1c9db35c 374 }
0558683c 375}
376
21716c07 377sub combine {
378 my ($class, @roles) = @_;
379
fb1e11d5 380 require Moose::Meta::Role::Application::RoleSummation;
381 require Moose::Meta::Role::Composite;
21716c07 382
fb1e11d5 383 my $c = Moose::Meta::Role::Composite->new(roles => \@roles);
384 Moose::Meta::Role::Application::RoleSummation->new->apply($c);
385 return $c;
0558683c 386}
387
fb1e11d5 388#####################################################################
389## NOTE:
390## This is Moose::Meta::Role as defined by Moose (plus the use of
391## MooseX::AttributeHelpers module). It is here as a reference to
392## make it easier to see what is happening above with all the meta
393## programming. - SL
394#####################################################################
395#
396# has 'roles' => (
397# metaclass => 'Collection::Array',
398# reader => 'get_roles',
399# isa => 'ArrayRef[Moose::Meta::Roles]',
400# default => sub { [] },
401# provides => {
402# 'push' => 'add_role',
403# }
404# );
405#
406# has 'excluded_roles_map' => (
407# metaclass => 'Collection::Hash',
408# reader => 'get_excluded_roles_map',
409# isa => 'HashRef[Str]',
410# provides => {
411# # Not exactly set, cause it sets multiple
412# 'set' => 'add_excluded_roles',
413# 'keys' => 'get_excluded_roles_list',
414# 'exists' => 'excludes_role',
415# }
416# );
417#
418# has 'attribute_map' => (
419# metaclass => 'Collection::Hash',
420# reader => 'get_attribute_map',
421# isa => 'HashRef[Str]',
422# provides => {
423# # 'set' => 'add_attribute' # has some special crap in it
424# 'get' => 'get_attribute',
425# 'keys' => 'get_attribute_list',
426# 'exists' => 'has_attribute',
427# # Not exactly delete, cause it sets multiple
428# 'delete' => 'remove_attribute',
429# }
430# );
431#
432# has 'required_methods' => (
433# metaclass => 'Collection::Hash',
434# reader => 'get_required_methods_map',
435# isa => 'HashRef[Str]',
436# provides => {
437# # not exactly set, or delete since it works for multiple
438# 'set' => 'add_required_methods',
439# 'delete' => 'remove_required_methods',
440# 'keys' => 'get_required_method_list',
441# 'exists' => 'requires_method',
442# }
443# );
444#
445# # the before, around and after modifiers are
446# # HASH keyed by method-name, with ARRAY of
447# # CODE refs to apply in that order
448#
449# has 'before_method_modifiers' => (
450# metaclass => 'Collection::Hash',
451# reader => 'get_before_method_modifiers_map',
452# isa => 'HashRef[ArrayRef[CodeRef]]',
453# provides => {
454# 'keys' => 'get_before_method_modifiers',
455# 'exists' => 'has_before_method_modifiers',
456# # This actually makes sure there is an
457# # ARRAY at the given key, and pushed onto
458# # it. It also checks for duplicates as well
459# # 'add' => 'add_before_method_modifier'
460# }
461# );
462#
463# has 'after_method_modifiers' => (
464# metaclass => 'Collection::Hash',
465# reader =>'get_after_method_modifiers_map',
466# isa => 'HashRef[ArrayRef[CodeRef]]',
467# provides => {
468# 'keys' => 'get_after_method_modifiers',
469# 'exists' => 'has_after_method_modifiers',
470# # This actually makes sure there is an
471# # ARRAY at the given key, and pushed onto
472# # it. It also checks for duplicates as well
473# # 'add' => 'add_after_method_modifier'
474# }
475# );
476#
477# has 'around_method_modifiers' => (
478# metaclass => 'Collection::Hash',
479# reader =>'get_around_method_modifiers_map',
480# isa => 'HashRef[ArrayRef[CodeRef]]',
481# provides => {
482# 'keys' => 'get_around_method_modifiers',
483# 'exists' => 'has_around_method_modifiers',
484# # This actually makes sure there is an
485# # ARRAY at the given key, and pushed onto
486# # it. It also checks for duplicates as well
487# # 'add' => 'add_around_method_modifier'
488# }
489# );
490#
491# # override is similar to the other modifiers
492# # except that it is not an ARRAY of code refs
493# # but instead just a single name->code mapping
494#
495# has 'override_method_modifiers' => (
496# metaclass => 'Collection::Hash',
497# reader =>'get_override_method_modifiers_map',
498# isa => 'HashRef[CodeRef]',
499# provides => {
500# 'keys' => 'get_override_method_modifier',
501# 'exists' => 'has_override_method_modifier',
502# 'add' => 'add_override_method_modifier', # checks for local method ..
503# }
504# );
505#
506#####################################################################
507
508
e185c027 5091;
510
511__END__
512
513=pod
514
515=head1 NAME
516
517Moose::Meta::Role - The Moose Role metaclass
518
519=head1 DESCRIPTION
520
fb1e11d5 521Please see L<Moose::Role> for more information about roles.
d44714be 522For the most part, this has no user-serviceable parts inside
fb1e11d5 523this module. It's API is still subject to some change (although
02a0fb52 524probably not that much really).
79592a54 525
e185c027 526=head1 METHODS
527
528=over 4
529
530=item B<meta>
531
532=item B<new>
533
78cd1d3b 534=item B<apply>
535
db1ab48d 536=item B<combine>
537
e185c027 538=back
539
540=over 4
541
542=item B<name>
543
544=item B<version>
545
546=item B<role_meta>
547
548=back
549
550=over 4
551
80572233 552=item B<get_roles>
553
554=item B<add_role>
555
556=item B<does_role>
557
558=back
559
560=over 4
561
d79e62fd 562=item B<add_excluded_roles>
563
564=item B<excludes_role>
565
566=item B<get_excluded_roles_list>
567
568=item B<get_excluded_roles_map>
569
2b14ac61 570=item B<calculate_all_roles>
571
d79e62fd 572=back
573
574=over 4
575
68efb014 576=item B<method_metaclass>
577
be4427d0 578=item B<find_method_by_name>
579
e185c027 580=item B<get_method>
581
582=item B<has_method>
583
bdabd620 584=item B<alias_method>
585
e185c027 586=item B<get_method_list>
587
093b12c2 588=item B<get_method_map>
589
638853cb 590=item B<update_package_cache_flag>
591
53dd42d8 592=item B<reset_package_cache_flag>
593
e185c027 594=back
595
596=over 4
597
598=item B<add_attribute>
599
600=item B<has_attribute>
601
602=item B<get_attribute>
603
604=item B<get_attribute_list>
605
606=item B<get_attribute_map>
607
608=item B<remove_attribute>
609
610=back
611
612=over 4
613
1331430a 614=item B<add_required_methods>
615
38f1204c 616=item B<remove_required_methods>
617
1331430a 618=item B<get_required_method_list>
619
620=item B<get_required_methods_map>
621
622=item B<requires_method>
623
624=back
625
0558683c 626=over 4
627
628=item B<add_after_method_modifier>
629
630=item B<add_around_method_modifier>
631
632=item B<add_before_method_modifier>
633
634=item B<add_override_method_modifier>
635
636=over 4
637
638=back
639
640=item B<has_after_method_modifiers>
641
642=item B<has_around_method_modifiers>
643
644=item B<has_before_method_modifiers>
645
646=item B<has_override_method_modifier>
647
648=over 4
649
650=back
651
652=item B<get_after_method_modifiers>
653
654=item B<get_around_method_modifiers>
655
656=item B<get_before_method_modifiers>
657
658=item B<get_method_modifier_list>
659
660=over 4
661
662=back
663
664=item B<get_override_method_modifier>
665
666=item B<get_after_method_modifiers_map>
667
668=item B<get_around_method_modifiers_map>
669
670=item B<get_before_method_modifiers_map>
671
672=item B<get_override_method_modifiers_map>
673
674=back
675
e185c027 676=head1 BUGS
677
fb1e11d5 678All complex software has bugs lurking in it, and this module is no
e185c027 679exception. If you find a bug please either email me, or add the bug
680to cpan-RT.
681
682=head1 AUTHOR
683
684Stevan Little E<lt>stevan@iinteractive.comE<gt>
685
686=head1 COPYRIGHT AND LICENSE
687
778db3ac 688Copyright 2006-2008 by Infinity Interactive, Inc.
e185c027 689
690L<http://www.iinteractive.com>
691
692This library is free software; you can redistribute it and/or modify
fb1e11d5 693it under the same terms as Perl itself.
e185c027 694
b8aeb4dc 695=cut