0.10
[gitmo/Moose.git] / lib / Moose / Meta / Role.pm
1
2 package Moose::Meta::Role;
3
4 use strict;
5 use warnings;
6 use metaclass;
7
8 use Carp         'confess';
9 use Scalar::Util 'blessed';
10 use B            'svref_2object';
11
12 use Moose::Meta::Class;
13
14 our $VERSION = '0.04';
15
16 ## Attributes
17
18 ## the meta for the role package
19
20 __PACKAGE__->meta->add_attribute('_role_meta' => (
21     reader   => '_role_meta',
22     init_arg => ':role_meta'
23 ));
24
25 ## roles
26
27 __PACKAGE__->meta->add_attribute('roles' => (
28     reader  => 'get_roles',
29     default => sub { [] }
30 ));
31
32 ## excluded roles
33
34 __PACKAGE__->meta->add_attribute('excluded_roles_map' => (
35     reader  => 'get_excluded_roles_map',
36     default => sub { {} }
37 ));
38
39 ## attributes
40
41 __PACKAGE__->meta->add_attribute('attribute_map' => (
42     reader   => 'get_attribute_map',
43     default  => sub { {} }
44 ));
45
46 ## required methods
47
48 __PACKAGE__->meta->add_attribute('required_methods' => (
49     reader  => 'get_required_methods_map',
50     default => sub { {} }
51 ));
52
53 ## method modifiers
54
55 __PACKAGE__->meta->add_attribute('before_method_modifiers' => (
56     reader  => 'get_before_method_modifiers_map',
57     default => sub { {} } # (<name> => [ (CODE) ])
58 ));
59
60 __PACKAGE__->meta->add_attribute('after_method_modifiers' => (
61     reader  => 'get_after_method_modifiers_map',
62     default => sub { {} } # (<name> => [ (CODE) ])
63 ));
64
65 __PACKAGE__->meta->add_attribute('around_method_modifiers' => (
66     reader  => 'get_around_method_modifiers_map',
67     default => sub { {} } # (<name> => [ (CODE) ])
68 ));
69
70 __PACKAGE__->meta->add_attribute('override_method_modifiers' => (
71     reader  => 'get_override_method_modifiers_map',
72     default => sub { {} } # (<name> => CODE) 
73 ));
74
75 ## Methods 
76
77 sub new {
78     my $class   = shift;
79     my %options = @_;
80     $options{':role_meta'} = Moose::Meta::Class->initialize(
81         $options{role_name},
82         ':method_metaclass' => 'Moose::Meta::Role::Method'
83     ) unless defined $options{':role_meta'} && 
84              $options{':role_meta'}->isa('Moose::Meta::Class');
85     my $self = $class->meta->new_object(%options);
86     return $self;
87 }
88
89 ## subroles
90
91 sub add_role {
92     my ($self, $role) = @_;
93     (blessed($role) && $role->isa('Moose::Meta::Role'))
94         || confess "Roles must be instances of Moose::Meta::Role";
95     push @{$self->get_roles} => $role;
96 }
97
98 sub calculate_all_roles {
99     my $self = shift;
100     my %seen;
101     grep { !$seen{$_->name}++ } $self, map { $_->calculate_all_roles } @{ $self->get_roles };
102 }
103
104 sub does_role {
105     my ($self, $role_name) = @_;
106     (defined $role_name)
107         || confess "You must supply a role name to look for";
108     # if we are it,.. then return true
109     return 1 if $role_name eq $self->name;
110     # otherwise.. check our children
111     foreach my $role (@{$self->get_roles}) {
112         return 1 if $role->does_role($role_name);
113     }
114     return 0;
115 }
116
117 ## excluded roles
118
119 sub add_excluded_roles {
120     my ($self, @excluded_role_names) = @_;
121     $self->get_excluded_roles_map->{$_} = undef foreach @excluded_role_names;
122 }
123
124 sub get_excluded_roles_list {
125     my ($self) = @_;
126     keys %{$self->get_excluded_roles_map};
127 }
128
129 sub excludes_role {
130     my ($self, $role_name) = @_;
131     exists $self->get_excluded_roles_map->{$role_name} ? 1 : 0;
132 }
133
134 ## required methods
135
136 sub add_required_methods {
137     my ($self, @methods) = @_;
138     $self->get_required_methods_map->{$_} = undef foreach @methods;
139 }
140
141 sub remove_required_methods {
142     my ($self, @methods) = @_;
143     delete $self->get_required_methods_map->{$_} foreach @methods;
144 }
145
146 sub get_required_method_list {
147     my ($self) = @_;
148     keys %{$self->get_required_methods_map};
149 }
150
151 sub requires_method {
152     my ($self, $method_name) = @_;
153     exists $self->get_required_methods_map->{$method_name} ? 1 : 0;
154 }
155
156 sub _clean_up_required_methods {
157     my $self = shift;
158     foreach my $method ($self->get_required_method_list) {
159         $self->remove_required_methods($method)
160             if $self->has_method($method);
161     } 
162 }
163
164 ## methods
165
166 # NOTE:
167 # we delegate to some role_meta methods for convience here
168 # the Moose::Meta::Role is meant to be a read-only interface
169 # to the underlying role package, if you want to manipulate 
170 # that, just use ->role_meta
171
172 sub name    { (shift)->_role_meta->name    }
173 sub version { (shift)->_role_meta->version }
174
175 sub get_method      { (shift)->_role_meta->get_method(@_)   }
176 sub has_method      { (shift)->_role_meta->has_method(@_)   }
177 sub alias_method    { (shift)->_role_meta->alias_method(@_) }
178 sub get_method_list { 
179     my ($self) = @_;
180     grep { 
181         # NOTE:
182         # this is a kludge for now,... these functions 
183         # should not be showing up in the list at all, 
184         # but they do, so we need to switch Moose::Role
185         # and Moose to use Sub::Exporter to prevent this
186         !/^(meta|has|extends|blessed|confess|augment|inner|override|super|before|after|around|with|requires)$/ 
187     } $self->_role_meta->get_method_list;
188 }
189
190 # ... however the items in statis (attributes & method modifiers)
191 # can be removed and added to through this API
192
193 # attributes
194
195 sub add_attribute {
196     my $self = shift;
197     my $name = shift;
198     my $attr_desc;
199     if (scalar @_ == 1 && ref($_[0]) eq 'HASH') {
200         $attr_desc = $_[0];
201     }
202     else {
203         $attr_desc = { @_ };
204     }
205     $self->get_attribute_map->{$name} = $attr_desc;
206 }
207
208 sub has_attribute {
209     my ($self, $name) = @_;
210     exists $self->get_attribute_map->{$name} ? 1 : 0;
211 }
212
213 sub get_attribute {
214     my ($self, $name) = @_;
215     $self->get_attribute_map->{$name}
216 }
217
218 sub remove_attribute {
219     my ($self, $name) = @_;
220     delete $self->get_attribute_map->{$name}
221 }
222
223 sub get_attribute_list {
224     my ($self) = @_;
225     keys %{$self->get_attribute_map};
226 }
227
228 # method modifiers
229
230 # mimic the metaclass API
231 sub add_before_method_modifier { (shift)->_add_method_modifier('before', @_) }
232 sub add_around_method_modifier { (shift)->_add_method_modifier('around', @_) }
233 sub add_after_method_modifier  { (shift)->_add_method_modifier('after',  @_) }
234
235 sub _add_method_modifier {
236     my ($self, $modifier_type, $method_name, $method) = @_;
237     my $accessor = "get_${modifier_type}_method_modifiers_map";
238     $self->$accessor->{$method_name} = [] 
239         unless exists $self->$accessor->{$method_name};
240     my $modifiers = $self->$accessor->{$method_name};
241     # NOTE:
242     # check to see that we aren't adding the 
243     # same code twice. We err in favor of the 
244     # first on here, this may not be as expected
245     foreach my $modifier (@{$modifiers}) {
246         return if $modifier == $method;
247     }
248     push @{$modifiers} => $method;
249 }
250
251 sub add_override_method_modifier {
252     my ($self, $method_name, $method) = @_;
253     (!$self->has_method($method_name))
254         || confess "Cannot add an override of method '$method_name' " . 
255                    "because there is a local version of '$method_name'";
256     $self->get_override_method_modifiers_map->{$method_name} = $method;    
257 }
258
259 sub has_before_method_modifiers { (shift)->_has_method_modifiers('before', @_) }
260 sub has_around_method_modifiers { (shift)->_has_method_modifiers('around', @_) }
261 sub has_after_method_modifiers  { (shift)->_has_method_modifiers('after',  @_) }
262
263 # override just checks for one,.. 
264 # but we can still re-use stuff
265 sub has_override_method_modifier { (shift)->_has_method_modifiers('override',  @_) }
266
267 sub _has_method_modifiers {
268     my ($self, $modifier_type, $method_name) = @_;
269     my $accessor = "get_${modifier_type}_method_modifiers_map";   
270     # NOTE:
271     # for now we assume that if it exists,.. 
272     # it has at least one modifier in it
273     (exists $self->$accessor->{$method_name}) ? 1 : 0;
274 }
275
276 sub get_before_method_modifiers { (shift)->_get_method_modifiers('before', @_) }
277 sub get_around_method_modifiers { (shift)->_get_method_modifiers('around', @_) }
278 sub get_after_method_modifiers  { (shift)->_get_method_modifiers('after',  @_) }
279
280 sub _get_method_modifiers {
281     my ($self, $modifier_type, $method_name) = @_;
282     my $accessor = "get_${modifier_type}_method_modifiers_map";
283     @{$self->$accessor->{$method_name}};
284 }
285
286 sub get_override_method_modifier {
287     my ($self, $method_name) = @_;
288     $self->get_override_method_modifiers_map->{$method_name};    
289 }
290
291 sub get_method_modifier_list {
292     my ($self, $modifier_type) = @_;
293     my $accessor = "get_${modifier_type}_method_modifiers_map";    
294     keys %{$self->$accessor};
295 }
296
297 ## applying a role to a class ...
298
299 sub _check_excluded_roles {
300     my ($self, $other) = @_;
301     if ($other->excludes_role($self->name)) {
302         confess "Conflict detected: " . $other->name . " excludes role '" . $self->name . "'";
303     }
304     foreach my $excluded_role_name ($self->get_excluded_roles_list) {
305         if ($other->does_role($excluded_role_name)) { 
306             confess "The class " . $other->name . " does the excluded role '$excluded_role_name'";
307         }
308         else {
309             if ($other->isa('Moose::Meta::Role')) {
310                 $other->add_excluded_roles($excluded_role_name);
311             }
312             # else -> ignore it :) 
313         }
314     }    
315 }
316
317 sub _check_required_methods {
318     my ($self, $other) = @_;
319     # NOTE:
320     # we might need to move this down below the 
321     # the attributes so that we can require any 
322     # attribute accessors. However I am thinking 
323     # that maybe those are somehow exempt from 
324     # the require methods stuff.  
325     foreach my $required_method_name ($self->get_required_method_list) {
326         unless ($other->has_method($required_method_name)) {
327             if ($other->isa('Moose::Meta::Role')) {
328                 $other->add_required_methods($required_method_name);
329             }
330             else {
331                 confess "'" . $self->name . "' requires the method '$required_method_name' " . 
332                         "to be implemented by '" . $other->name . "'";
333             }
334         }
335         else {
336             # NOTE:
337             # we need to make sure that the method is 
338             # not a method modifier, because those do 
339             # not satisfy the requirements ...
340             my $method = $other->get_method($required_method_name);
341             # check if it is an override or a generated accessor ..
342             (!$method->isa('Moose::Meta::Method::Overriden') &&
343              !$method->isa('Class::MOP::Attribute::Accessor'))
344                 || confess "'" . $self->name . "' requires the method '$required_method_name' " . 
345                            "to be implemented by '" . $other->name . "', the method is only a method modifier";
346             # before/after/around methods are a little trickier
347             # since we wrap the original local method (if applicable)
348             # so we need to check if the original wrapped method is 
349             # from the same package, and not a wrap of the super method 
350             if ($method->isa('Class::MOP::Method::Wrapped')) {
351                 ($method->get_original_method->package_name eq $other->name)
352                     || confess "'" . $self->name . "' requires the method '$required_method_name' " . 
353                                "to be implemented by '" . $other->name . "', the method is only a method modifier";            
354             }
355         }
356     }    
357 }
358
359 sub _apply_attributes {
360     my ($self, $other) = @_;    
361     foreach my $attribute_name ($self->get_attribute_list) {
362         # it if it has one already
363         if ($other->has_attribute($attribute_name) &&
364             # make sure we haven't seen this one already too
365             $other->get_attribute($attribute_name) != $self->get_attribute($attribute_name)) {
366             # see if we are being composed  
367             # into a role or not
368             if ($other->isa('Moose::Meta::Role')) {                
369                 # all attribute conflicts between roles 
370                 # result in an immediate fatal error 
371                 confess "Role '" . $self->name . "' has encountered an attribute conflict " . 
372                         "during composition. This is fatal error and cannot be disambiguated.";
373             }
374             else {
375                 # but if this is a class, we 
376                 # can safely skip adding the 
377                 # attribute to the class
378                 next;
379             }
380         }
381         else {
382             $other->add_attribute(
383                 $attribute_name,
384                 $self->get_attribute($attribute_name)
385             );
386         }
387     }    
388 }
389
390 sub _apply_methods {
391     my ($self, $other) = @_;   
392     foreach my $method_name ($self->get_method_list) {
393         # it if it has one already
394         if ($other->has_method($method_name) &&
395             # and if they are not the same thing ...
396             $other->get_method($method_name) != $self->get_method($method_name)) {
397             # see if we are composing into a role
398             if ($other->isa('Moose::Meta::Role')) { 
399                 # method conflicts between roles result 
400                 # in the method becoming a requirement
401                 $other->add_required_methods($method_name);
402                 # NOTE:
403                 # we have to remove the method from our 
404                 # role, if this is being called from combine()
405                 # which means the meta is an anon class
406                 # this *may* cause problems later, but it 
407                 # is probably fairly safe to assume that 
408                 # anon classes will only be used internally
409                 # or by people who know what they are doing
410                 $other->_role_meta->remove_method($method_name)
411                     if $other->_role_meta->name =~ /__ANON__/;
412             }
413             else {
414                 next;
415             }
416         }
417         else {
418             # add it, although it could be overriden 
419             $other->alias_method(
420                 $method_name,
421                 $self->get_method($method_name)
422             );
423         }
424     }     
425 }
426
427 sub _apply_override_method_modifiers {
428     my ($self, $other) = @_;    
429     foreach my $method_name ($self->get_method_modifier_list('override')) {
430         # it if it has one already then ...
431         if ($other->has_method($method_name)) {
432             # if it is being composed into another role
433             # we have a conflict here, because you cannot 
434             # combine an overriden method with a locally
435             # defined one 
436             if ($other->isa('Moose::Meta::Role')) { 
437                 confess "Role '" . $self->name . "' has encountered an 'override' method conflict " . 
438                         "during composition (A local method of the same name as been found). This " . 
439                         "is fatal error.";
440             }
441             else {
442                 # if it is a class, then we 
443                 # just ignore this here ...
444                 next;
445             }
446         }
447         else {
448             # if no local method is found, then we 
449             # must check if we are a role or class
450             if ($other->isa('Moose::Meta::Role')) { 
451                 # if we are a role, we need to make sure 
452                 # we dont have a conflict with the role 
453                 # we are composing into
454                 if ($other->has_override_method_modifier($method_name) &&
455                     $other->get_override_method_modifier($method_name) != $self->get_override_method_modifier($method_name)) {
456                     confess "Role '" . $self->name . "' has encountered an 'override' method conflict " . 
457                             "during composition (Two 'override' methods of the same name encountered). " . 
458                             "This is fatal error.";
459                 }
460                 else {   
461                     # if there is no conflict,
462                     # just add it to the role  
463                     $other->add_override_method_modifier(
464                         $method_name, 
465                         $self->get_override_method_modifier($method_name)
466                     );                    
467                 }
468             }
469             else {
470                 # if this is not a role, then we need to 
471                 # find the original package of the method
472                 # so that we can tell the class were to 
473                 # find the right super() method
474                 my $method = $self->get_override_method_modifier($method_name);
475                 my $package = svref_2object($method)->GV->STASH->NAME;
476                 # if it is a class, we just add it
477                 $other->add_override_method_modifier($method_name, $method, $package);
478             }
479         }
480     }    
481 }
482
483 sub _apply_method_modifiers {
484     my ($self, $modifier_type, $other) = @_;    
485     my $add = "add_${modifier_type}_method_modifier";
486     my $get = "get_${modifier_type}_method_modifiers";    
487     foreach my $method_name ($self->get_method_modifier_list($modifier_type)) {
488         $other->$add(
489             $method_name,
490             $_
491         ) foreach $self->$get($method_name);
492     }    
493 }
494
495 sub _apply_before_method_modifiers { (shift)->_apply_method_modifiers('before' => @_) }
496 sub _apply_around_method_modifiers { (shift)->_apply_method_modifiers('around' => @_) }
497 sub _apply_after_method_modifiers  { (shift)->_apply_method_modifiers('after'  => @_) }
498
499 sub apply {
500     my ($self, $other) = @_;
501     
502     $self->_check_excluded_roles($other);
503     $self->_check_required_methods($other);  
504
505     $self->_apply_attributes($other);         
506     $self->_apply_methods($other);         
507          
508     $self->_apply_override_method_modifiers($other);                  
509     $self->_apply_before_method_modifiers($other);                  
510     $self->_apply_around_method_modifiers($other);                  
511     $self->_apply_after_method_modifiers($other);                              
512     
513     $other->add_role($self);
514 }
515
516 sub combine {
517     my ($class, @roles) = @_;
518     
519     my $combined = $class->new(
520         ':role_meta' => Moose::Meta::Class->create_anon_class()
521     );
522     
523     foreach my $role (@roles) {
524         $role->apply($combined);
525     }
526     
527     $combined->_clean_up_required_methods;   
528     
529     return $combined;
530 }
531
532 package Moose::Meta::Role::Method;
533
534 use strict;
535 use warnings;
536
537 our $VERSION = '0.01';
538
539 use base 'Class::MOP::Method';
540
541 1;
542
543 __END__
544
545 =pod
546
547 =head1 NAME
548
549 Moose::Meta::Role - The Moose Role metaclass
550
551 =head1 DESCRIPTION
552
553 Moose's Roles are being actively developed, please see L<Moose::Role> 
554 for more information. For the most part, this has no user-serviceable 
555 parts inside. It's API is still subject to some change (although 
556 probably not that much really).
557
558 =head1 METHODS
559
560 =over 4
561
562 =item B<meta>
563
564 =item B<new>
565
566 =item B<apply>
567
568 =item B<combine>
569
570 =back
571
572 =over 4
573
574 =item B<name>
575
576 =item B<version>
577
578 =item B<role_meta>
579
580 =back
581
582 =over 4
583
584 =item B<get_roles>
585
586 =item B<add_role>
587
588 =item B<does_role>
589
590 =back
591
592 =over 4
593
594 =item B<add_excluded_roles>
595
596 =item B<excludes_role>
597
598 =item B<get_excluded_roles_list>
599
600 =item B<get_excluded_roles_map>
601
602 =item B<calculate_all_roles>
603
604 =back
605
606 =over 4
607
608 =item B<get_method>
609
610 =item B<has_method>
611
612 =item B<alias_method>
613
614 =item B<get_method_list>
615
616 =back
617
618 =over 4
619
620 =item B<add_attribute>
621
622 =item B<has_attribute>
623
624 =item B<get_attribute>
625
626 =item B<get_attribute_list>
627
628 =item B<get_attribute_map>
629
630 =item B<remove_attribute>
631
632 =back
633
634 =over 4
635
636 =item B<add_required_methods>
637
638 =item B<remove_required_methods>
639
640 =item B<get_required_method_list>
641
642 =item B<get_required_methods_map>
643
644 =item B<requires_method>
645
646 =back
647
648 =over 4
649
650 =item B<add_after_method_modifier>
651
652 =item B<add_around_method_modifier>
653
654 =item B<add_before_method_modifier>
655
656 =item B<add_override_method_modifier>
657
658 =over 4
659
660 =back
661
662 =item B<has_after_method_modifiers>
663
664 =item B<has_around_method_modifiers>
665
666 =item B<has_before_method_modifiers>
667
668 =item B<has_override_method_modifier>
669
670 =over 4
671
672 =back
673
674 =item B<get_after_method_modifiers>
675
676 =item B<get_around_method_modifiers>
677
678 =item B<get_before_method_modifiers>
679
680 =item B<get_method_modifier_list>
681
682 =over 4
683
684 =back
685
686 =item B<get_override_method_modifier>
687
688 =item B<get_after_method_modifiers_map>
689
690 =item B<get_around_method_modifiers_map>
691
692 =item B<get_before_method_modifiers_map>
693
694 =item B<get_override_method_modifiers_map>
695
696 =back
697
698 =head1 BUGS
699
700 All complex software has bugs lurking in it, and this module is no 
701 exception. If you find a bug please either email me, or add the bug
702 to cpan-RT.
703
704 =head1 AUTHOR
705
706 Stevan Little E<lt>stevan@iinteractive.comE<gt>
707
708 =head1 COPYRIGHT AND LICENSE
709
710 Copyright 2006 by Infinity Interactive, Inc.
711
712 L<http://www.iinteractive.com>
713
714 This library is free software; you can redistribute it and/or modify
715 it under the same terms as Perl itself. 
716
717 =cut