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