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