remove modifiers from docs
[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 ## Methods 
54
55 sub new {
56     my $class   = shift;
57     my %options = @_;
58     $options{':role_meta'} = Moose::Meta::Class->initialize(
59         $options{role_name},
60         ':method_metaclass' => 'Moose::Meta::Role::Method'
61     ) unless defined $options{':role_meta'} && 
62              $options{':role_meta'}->isa('Moose::Meta::Class');
63     my $self = $class->meta->new_object(%options);
64     return $self;
65 }
66
67 ## subroles
68
69 sub add_role {
70     my ($self, $role) = @_;
71     (blessed($role) && $role->isa('Moose::Meta::Role'))
72         || confess "Roles must be instances of Moose::Meta::Role";
73     push @{$self->get_roles} => $role;
74 }
75
76 sub calculate_all_roles {
77     my $self = shift;
78     my %seen;
79     grep { !$seen{$_->name}++ } $self, map { $_->calculate_all_roles } @{ $self->get_roles };
80 }
81
82 sub does_role {
83     my ($self, $role_name) = @_;
84     (defined $role_name)
85         || confess "You must supply a role name to look for";
86     # if we are it,.. then return true
87     return 1 if $role_name eq $self->name;
88     # otherwise.. check our children
89     foreach my $role (@{$self->get_roles}) {
90         return 1 if $role->does_role($role_name);
91     }
92     return 0;
93 }
94
95 ## excluded roles
96
97 sub add_excluded_roles {
98     my ($self, @excluded_role_names) = @_;
99     $self->get_excluded_roles_map->{$_} = undef foreach @excluded_role_names;
100 }
101
102 sub get_excluded_roles_list {
103     my ($self) = @_;
104     keys %{$self->get_excluded_roles_map};
105 }
106
107 sub excludes_role {
108     my ($self, $role_name) = @_;
109     exists $self->get_excluded_roles_map->{$role_name} ? 1 : 0;
110 }
111
112 ## required methods
113
114 sub add_required_methods {
115     my ($self, @methods) = @_;
116     $self->get_required_methods_map->{$_} = undef foreach @methods;
117 }
118
119 sub remove_required_methods {
120     my ($self, @methods) = @_;
121     delete $self->get_required_methods_map->{$_} foreach @methods;
122 }
123
124 sub get_required_method_list {
125     my ($self) = @_;
126     keys %{$self->get_required_methods_map};
127 }
128
129 sub requires_method {
130     my ($self, $method_name) = @_;
131     exists $self->get_required_methods_map->{$method_name} ? 1 : 0;
132 }
133
134 sub _clean_up_required_methods {
135     my $self = shift;
136     foreach my $method ($self->get_required_method_list) {
137         $self->remove_required_methods($method)
138             if $self->has_method($method);
139     } 
140 }
141
142 ## methods
143
144 # NOTE:
145 # we delegate to some role_meta methods for convience here
146 # the Moose::Meta::Role is meant to be a read-only interface
147 # to the underlying role package, if you want to manipulate 
148 # that, just use ->role_meta
149
150 sub name    { (shift)->_role_meta->name    }
151 sub version { (shift)->_role_meta->version }
152
153 sub get_method      { (shift)->_role_meta->get_method(@_)   }
154 sub has_method      { (shift)->_role_meta->has_method(@_)   }
155 sub alias_method    { (shift)->_role_meta->alias_method(@_) }
156 sub get_method_list { 
157     my ($self) = @_;
158     grep { 
159         # NOTE:
160         # this is a kludge for now,... these functions 
161         # should not be showing up in the list at all, 
162         # but they do, so we need to switch Moose::Role
163         # and Moose to use Sub::Exporter to prevent this
164         !/^(meta|has|extends|blessed|confess|augment|inner|override|super|before|after|around|with|requires)$/ 
165     } $self->_role_meta->get_method_list;
166 }
167
168 # ... however the items in statis (attributes & method modifiers)
169 # can be removed and added to through this API
170
171 # attributes
172
173 sub add_attribute {
174     my $self = shift;
175     my $name = shift;
176     my $attr_desc;
177     if (scalar @_ == 1 && ref($_[0]) eq 'HASH') {
178         $attr_desc = $_[0];
179     }
180     else {
181         $attr_desc = { @_ };
182     }
183     $self->get_attribute_map->{$name} = $attr_desc;
184 }
185
186 sub has_attribute {
187     my ($self, $name) = @_;
188     exists $self->get_attribute_map->{$name} ? 1 : 0;
189 }
190
191 sub get_attribute {
192     my ($self, $name) = @_;
193     $self->get_attribute_map->{$name}
194 }
195
196 sub remove_attribute {
197     my ($self, $name) = @_;
198     delete $self->get_attribute_map->{$name}
199 }
200
201 sub get_attribute_list {
202     my ($self) = @_;
203     keys %{$self->get_attribute_map};
204 }
205
206
207 ## applying a role to a class ...
208
209 sub _check_excluded_roles {
210     my ($self, $other) = @_;
211     if ($other->excludes_role($self->name)) {
212         confess "Conflict detected: " . $other->name . " excludes role '" . $self->name . "'";
213     }
214     foreach my $excluded_role_name ($self->get_excluded_roles_list) {
215         if ($other->does_role($excluded_role_name)) { 
216             confess "The class " . $other->name . " does the excluded role '$excluded_role_name'";
217         }
218         else {
219             if ($other->isa('Moose::Meta::Role')) {
220                 $other->add_excluded_roles($excluded_role_name);
221             }
222             # else -> ignore it :) 
223         }
224     }    
225 }
226
227 sub _check_required_methods {
228     my ($self, $other) = @_;
229     # NOTE:
230     # we might need to move this down below the 
231     # the attributes so that we can require any 
232     # attribute accessors. However I am thinking 
233     # that maybe those are somehow exempt from 
234     # the require methods stuff.  
235     foreach my $required_method_name ($self->get_required_method_list) {
236         
237         # FIXME:
238         # This should not call has_method, instead it should
239         # call find_method_by_name (to be added to Class::MOP)
240         unless ($other->has_method($required_method_name)) {
241             if ($other->isa('Moose::Meta::Role')) {
242                 $other->add_required_methods($required_method_name);
243             }
244             else {
245                 confess "'" . $self->name . "' requires the method '$required_method_name' " . 
246                         "to be implemented by '" . $other->name . "'";
247             }
248         }
249         else {
250             # NOTE:
251             # we need to make sure that the method is 
252             # not a method modifier, because those do 
253             # not satisfy the requirements ...
254             
255             # FIXME:
256             # This should also call find_method_by_name
257             my $method = $other->get_method($required_method_name);
258             # check if it is an override or a generated accessor ..
259             (!$method->isa('Moose::Meta::Method::Overriden') &&
260              !$method->isa('Class::MOP::Attribute::Accessor'))
261                 || confess "'" . $self->name . "' requires the method '$required_method_name' " . 
262                            "to be implemented by '" . $other->name . "', the method is only a method modifier";
263             # before/after/around methods are a little trickier
264             # since we wrap the original local method (if applicable)
265             # so we need to check if the original wrapped method is 
266             # from the same package, and not a wrap of the super method 
267             if ($method->isa('Class::MOP::Method::Wrapped')) {
268                 ($method->get_original_method->package_name eq $other->name)
269                     || confess "'" . $self->name . "' requires the method '$required_method_name' " . 
270                                "to be implemented by '" . $other->name . "', the method is only a method modifier";            
271             }
272         }
273     }    
274 }
275
276 sub _apply_attributes {
277     my ($self, $other) = @_;    
278     foreach my $attribute_name ($self->get_attribute_list) {
279         # it if it has one already
280         if ($other->has_attribute($attribute_name) &&
281             # make sure we haven't seen this one already too
282             $other->get_attribute($attribute_name) != $self->get_attribute($attribute_name)) {
283             # see if we are being composed  
284             # into a role or not
285             if ($other->isa('Moose::Meta::Role')) {                
286                 # all attribute conflicts between roles 
287                 # result in an immediate fatal error 
288                 confess "Role '" . $self->name . "' has encountered an attribute conflict " . 
289                         "during composition. This is fatal error and cannot be disambiguated.";
290             }
291             else {
292                 # but if this is a class, we 
293                 # can safely skip adding the 
294                 # attribute to the class
295                 next;
296             }
297         }
298         else {
299             $other->add_attribute(
300                 $attribute_name,
301                 $self->get_attribute($attribute_name)
302             );
303         }
304     }    
305 }
306
307 sub _apply_methods {
308     my ($self, $other) = @_;   
309     foreach my $method_name ($self->get_method_list) {
310         # it if it has one already
311         if ($other->has_method($method_name) &&
312             # and if they are not the same thing ...
313             $other->get_method($method_name) != $self->get_method($method_name)) {
314             # see if we are composing into a role
315             if ($other->isa('Moose::Meta::Role')) { 
316                 # method conflicts between roles result 
317                 # in the method becoming a requirement
318                 $other->add_required_methods($method_name);
319                 # NOTE:
320                 # we have to remove the method from our 
321                 # role, if this is being called from combine()
322                 # which means the meta is an anon class
323                 # this *may* cause problems later, but it 
324                 # is probably fairly safe to assume that 
325                 # anon classes will only be used internally
326                 # or by people who know what they are doing
327                 $other->_role_meta->remove_method($method_name)
328                     if $other->_role_meta->name =~ /__ANON__/;
329             }
330             else {
331                 next;
332             }
333         }
334         else {
335             # add it, although it could be overriden 
336             $other->alias_method(
337                 $method_name,
338                 $self->get_method($method_name)
339             );
340         }
341     }     
342 }
343
344 sub apply {
345     my ($self, $other) = @_;
346     
347     $self->_check_excluded_roles($other);
348     $self->_check_required_methods($other);  
349
350     $self->_apply_attributes($other);         
351     $self->_apply_methods($other);         
352
353     $other->add_role($self);
354 }
355
356 sub combine {
357     my ($class, @roles) = @_;
358     
359     my $combined = $class->new(
360         ':role_meta' => Moose::Meta::Class->create_anon_class()
361     );
362     
363     foreach my $role (@roles) {
364         $role->apply($combined);
365     }
366     
367     $combined->_clean_up_required_methods;   
368     
369     return $combined;
370 }
371
372 package Moose::Meta::Role::Method;
373
374 use strict;
375 use warnings;
376
377 our $VERSION = '0.01';
378
379 use base 'Class::MOP::Method';
380
381 1;
382
383 __END__
384
385 =pod
386
387 =head1 NAME
388
389 Moose::Meta::Role - The Moose Role metaclass
390
391 =head1 DESCRIPTION
392
393 Moose's Roles are being actively developed, please see L<Moose::Role> 
394 for more information. For the most part, this has no user-serviceable 
395 parts inside. It's API is still subject to some change (although 
396 probably not that much really).
397
398 =head1 METHODS
399
400 =over 4
401
402 =item B<meta>
403
404 =item B<new>
405
406 =item B<apply>
407
408 =item B<combine>
409
410 =back
411
412 =over 4
413
414 =item B<name>
415
416 =item B<version>
417
418 =item B<role_meta>
419
420 =back
421
422 =over 4
423
424 =item B<get_roles>
425
426 =item B<add_role>
427
428 =item B<does_role>
429
430 =back
431
432 =over 4
433
434 =item B<add_excluded_roles>
435
436 =item B<excludes_role>
437
438 =item B<get_excluded_roles_list>
439
440 =item B<get_excluded_roles_map>
441
442 =item B<calculate_all_roles>
443
444 =back
445
446 =over 4
447
448 =item B<get_method>
449
450 =item B<has_method>
451
452 =item B<alias_method>
453
454 =item B<get_method_list>
455
456 =back
457
458 =over 4
459
460 =item B<add_attribute>
461
462 =item B<has_attribute>
463
464 =item B<get_attribute>
465
466 =item B<get_attribute_list>
467
468 =item B<get_attribute_map>
469
470 =item B<remove_attribute>
471
472 =back
473
474 =over 4
475
476 =item B<add_required_methods>
477
478 =item B<remove_required_methods>
479
480 =item B<get_required_method_list>
481
482 =item B<get_required_methods_map>
483
484 =item B<requires_method>
485
486 =back
487
488 =head1 BUGS
489
490 All complex software has bugs lurking in it, and this module is no 
491 exception. If you find a bug please either email me, or add the bug
492 to cpan-RT.
493
494 =head1 AUTHOR
495
496 Stevan Little E<lt>stevan@iinteractive.comE<gt>
497
498 =head1 COPYRIGHT AND LICENSE
499
500 Copyright 2006 by Infinity Interactive, Inc.
501
502 L<http://www.iinteractive.com>
503
504 This library is free software; you can redistribute it and/or modify
505 it under the same terms as Perl itself. 
506
507 =cut