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