Tidy
[gitmo/Mouse.git] / lib / Mouse / Meta / Role.pm
1 package Mouse::Meta::Role;
2 use strict;
3 use warnings;
4
5 use Mouse::Util qw(not_supported english_list);
6 use Mouse::Meta::Module;
7
8 use base qw(Mouse::Meta::Module);
9
10 sub method_metaclass(){ 'Mouse::Meta::Role::Method' } # required for get_method()
11
12 sub _construct_meta {
13     my $class = shift;
14
15     my %args  = @_;
16
17     $args{methods}          ||= {};
18     $args{attributes}       ||= {};
19     $args{required_methods} ||= [];
20     $args{roles}            ||= [];
21
22 #    return Mouse::Meta::Class->initialize($class)->new_object(%args)
23 #        if $class ne __PACKAGE__;
24
25     return bless \%args, ref($class) || $class;
26 }
27
28 sub create_anon_role{
29     my $self = shift;
30     return $self->create(undef, @_);
31 }
32
33 sub is_anon_role{
34     return exists $_[0]->{anon_serial_id};
35 }
36
37 sub get_roles { $_[0]->{roles} }
38
39 sub get_required_method_list{
40     return @{ $_[0]->{required_methods} };
41 }
42
43 sub add_required_methods {
44     my($self, @methods) = @_;
45     push @{$self->{required_methods}}, @methods;
46 }
47
48 sub requires_method {
49     my($self, $name) = @_;
50     return scalar( grep{ $_ eq $name } @{ $self->{required_methods} } ) != 0;
51 }
52
53 sub add_attribute {
54     my $self = shift;
55     my $name = shift;
56
57     $self->{attributes}->{$name} = (@_ == 1) ? $_[0] : { @_ };
58 }
59
60 sub _canonicalize_apply_args{
61     my($self, $applicant, %args) = @_;
62
63     if($applicant->isa('Mouse::Meta::Class')){
64         $args{_to} = 'class';
65     }
66     elsif($applicant->isa('Mouse::Meta::Role')){
67         $args{_to} = 'role';
68     }
69     else{
70         $args{_to} = 'instance';
71
72         not_supported 'Application::ToInstance';
73     }
74
75     if($args{alias} && !exists $args{-alias}){
76         $args{-alias} = $args{alias};
77     }
78     if($args{excludes} && !exists $args{-excludes}){
79         $args{-excludes} = $args{excludes};
80     }
81
82     if(my $excludes = $args{-excludes}){
83         $args{-excludes} = {}; # replace with a hash ref
84         if(ref $excludes){
85             %{$args{-excludes}} = (map{ $_ => undef } @{$excludes});
86         }
87         else{
88             $args{-excludes}{$excludes} = undef;
89         }
90     }
91
92     return \%args;
93 }
94
95 sub _check_required_methods{
96     my($role, $class, $args, @other_roles) = @_;
97
98     if($args->{_to} eq 'class'){
99         my $class_name = $class->name;
100         my $role_name  = $role->name;
101         my @missing;
102         foreach my $method_name(@{$role->{required_methods}}){
103             if(!$class_name->can($method_name)){
104                 my $has_method      = 0;
105
106                 foreach my $another_role_spec(@other_roles){
107                     my $another_role_name = $another_role_spec->[0];
108                     if($role_name ne $another_role_name && $another_role_name->can($method_name)){
109                         $has_method = 1;
110                         last;
111                     }
112                 }
113
114                 push @missing, $method_name if !$has_method;
115             }
116         }
117         if(@missing){
118             $class->throw_error("'$role_name' requires the "
119                 . (@missing == 1 ? 'method' : 'methods')
120                 . " "
121                 . english_list(map{ sprintf q{'%s'}, $_ } @missing)
122                 . " to be implemented by '$class_name'");
123         }
124     }
125     elsif($args->{_to} eq 'role'){
126         # apply role($role) to role($class)
127         foreach my $method_name($role->get_required_method_list){
128             next if $class->has_method($method_name); # already has it
129             $class->add_required_methods($method_name);
130         }
131     }
132
133     return;
134 }
135
136 sub _apply_methods{
137     my($role, $class, $args) = @_;
138
139     my $role_name  = $role->name;
140     my $class_name = $class->name;
141
142     my $alias    = $args->{-alias};
143     my $excludes = $args->{-excludes};
144
145     foreach my $method_name($role->get_method_list){
146         next if $method_name eq 'meta';
147
148         my $code = $role_name->can($method_name);
149
150         if(!exists $excludes->{$method_name}){
151             if(!$class->has_method($method_name)){
152                 $class->add_method($method_name => $code);
153             }
154         }
155
156         if($alias && $alias->{$method_name}){
157             my $dstname = $alias->{$method_name};
158
159             my $dstcode = do{ no strict 'refs'; *{$class_name . '::' . $dstname}{CODE} };
160
161             if(defined($dstcode) && $dstcode != $code){
162                 $class->throw_error("Cannot create a method alias if a local method of the same name exists");
163             }
164             else{
165                 $class->add_method($dstname => $code);
166             }
167         }
168     }
169
170     return;
171 }
172
173 sub _apply_attributes{
174     my($role, $class, $args) = @_;
175
176     if ($args->{_to} eq 'class') {
177         # apply role to class
178         for my $attr_name ($role->get_attribute_list) {
179             next if $class->has_attribute($attr_name);
180
181             my $spec = $role->get_attribute($attr_name);
182
183             $class->add_attribute($attr_name => %{$spec});
184         }
185     }
186     elsif($args->{_to} eq 'role'){
187         # apply role to role
188         for my $attr_name ($role->get_attribute_list) {
189             next if $class->has_attribute($attr_name);
190
191             my $spec = $role->get_attribute($attr_name);
192             $class->add_attribute($attr_name => $spec);
193         }
194     }
195
196     return;
197 }
198
199 sub _apply_modifiers{
200     my($role, $class, $args) = @_;
201
202     for my $modifier_type (qw/override before around after/) {
203         my $add_modifier = "add_${modifier_type}_method_modifier";
204         my $modifiers    = $role->{"${modifier_type}_method_modifiers"};
205
206         while(my($method_name, $modifier_codes) = each %{$modifiers}){
207             foreach my $code(ref($modifier_codes) eq 'ARRAY' ? @{$modifier_codes} : $modifier_codes){
208                 $class->$add_modifier($method_name => $code);
209             }
210         }
211     }
212     return;
213 }
214
215 sub _append_roles{
216     my($role, $class, $args) = @_;
217
218     my $roles = ($args->{_to} eq 'class') ? $class->roles : $class->get_roles;
219
220     foreach my $r($role, @{$role->get_roles}){
221         if(!$class->does_role($r->name)){
222             push @{$roles}, $r;
223         }
224     }
225     return;
226 }
227
228 # Moose uses Application::ToInstance, Application::ToClass, Application::ToRole
229 sub apply {
230     my $self      = shift;
231     my $applicant = shift;
232
233     my $args = $self->_canonicalize_apply_args($applicant, @_);
234
235     $self->_check_required_methods($applicant, $args);
236     $self->_apply_methods($applicant, $args);
237     $self->_apply_attributes($applicant, $args);
238     $self->_apply_modifiers($applicant, $args);
239     $self->_append_roles($applicant, $args);
240     return;
241 }
242
243 sub combine_apply {
244     my(undef, $class, @roles) = @_;
245
246     if($class->isa('Mouse::Object')){
247         not_supported 'Application::ToInstance';
248     }
249
250     # check conflicting
251     my %method_provided;
252     my @method_conflicts;
253     my %attr_provided;
254     my %override_provided;
255
256     foreach my $role_spec (@roles) {
257         my $role      = $role_spec->[0]->meta;
258         my $role_name = $role->name;
259
260         # methods
261         foreach my $method_name($role->get_method_list){
262             next if $class->has_method($method_name); # manually resolved
263
264             my $code = do{ no strict 'refs'; \&{ $role_name . '::' . $method_name } };
265
266             my $c = $method_provided{$method_name};
267
268             if($c && $c->[0] != $code){
269                 push @{$c}, $role;
270                 push @method_conflicts, $c;
271             }
272             else{
273                 $method_provided{$method_name} = [$code, $method_name, $role];
274             }
275         }
276
277         # attributes
278         foreach my $attr_name($role->get_attribute_list){
279             my $attr = $role->get_attribute($attr_name);
280             my $c    = $attr_provided{$attr_name};
281             if($c && $c != $attr){
282                 $class->throw_error("We have encountered an attribute conflict with '$attr_name' "
283                                    . "during composition. This is fatal error and cannot be disambiguated.")
284             }
285             else{
286                 $attr_provided{$attr_name} = $attr;
287             }
288         }
289
290         # override modifiers
291         foreach my $method_name($role->get_method_modifier_list('override')){
292             my $override = $role->get_override_method_modifier($method_name);
293             my $c        = $override_provided{$method_name};
294             if($c && $c != $override){
295                 $class->throw_error( "We have encountered an 'override' method conflict with '$method_name' during "
296                                    . "composition (Two 'override' methods of the same name encountered). "
297                                    . "This is fatal error.")
298             }
299             else{
300                 $override_provided{$method_name} = $override;
301             }
302         }
303     }
304     if(@method_conflicts){
305         my $error;
306
307         if(@method_conflicts == 1){
308             my($code, $method_name, @roles) = @{$method_conflicts[0]};
309             $class->throw_error(
310                 sprintf q{Due to a method name conflict in roles %s, the method '%s' must be implemented or excluded by '%s'},
311                     english_list(map{ sprintf q{'%s'}, $_->name } @roles), $method_name, $class->name
312             );
313         }
314         else{
315             @method_conflicts = sort { $a->[0] cmp $b->[0] } @method_conflicts; # to avoid hash-ordering bugs
316             my $methods = english_list(map{ sprintf q{'%s'}, $_->[1] } @method_conflicts);
317             my $roles   = english_list(
318                 map{ sprintf q{'%s'}, $_->name }
319                 map{ my($code, $method_name, @roles) = @{$_}; @roles } @method_conflicts
320             );
321
322             $class->throw_error(
323                 sprintf q{Due to method name conflicts in roles %s, the methods %s must be implemented or excluded by '%s'},
324                     $roles, $methods, $class->name
325             );
326         }
327     }
328
329     foreach my $role_spec (@roles) {
330         my($role_name, $args) = @{$role_spec};
331
332         my $role = $role_name->meta;
333
334         $args = $role->_canonicalize_apply_args($class, %{$args});
335
336         $role->_check_required_methods($class, $args, @roles);
337         $role->_apply_methods($class, $args);
338         $role->_apply_attributes($class, $args);
339         $role->_apply_modifiers($class, $args);
340         $role->_append_roles($class, $args);
341     }
342     return;
343 }
344
345 for my $modifier_type (qw/before after around/) {
346
347     my $modifier = "${modifier_type}_method_modifiers";
348     my $add_method_modifier =  sub {
349         my ($self, $method_name, $method) = @_;
350
351         push @{ $self->{$modifier}->{$method_name} ||= [] }, $method;
352         return;
353     };
354     my $has_method_modifiers = sub{
355         my($self, $method_name) = @_;
356         my $m = $self->{$modifier}->{$method_name};
357         return $m && @{$m} != 0;
358     };
359     my $get_method_modifiers = sub {
360         my ($self, $method_name) = @_;
361         return @{ $self->{$modifier}->{$method_name} ||= [] }
362     };
363
364     no strict 'refs';
365     *{ 'add_' . $modifier_type . '_method_modifier'  } = $add_method_modifier;
366     *{ 'has_' . $modifier_type . '_method_modifiers' } = $has_method_modifiers;
367     *{ 'get_' . $modifier_type . '_method_modifiers' } = $get_method_modifiers;
368 }
369
370 sub add_override_method_modifier{
371     my($self, $method_name, $method) = @_;
372
373     if($self->has_method($method_name)){
374         # This error happens in the override keyword or during role composition,
375         # so I added a message, "A local method of ...", only for compatibility (gfx)
376         $self->throw_error("Cannot add an override of method '$method_name' "
377                    . "because there is a local version of '$method_name'"
378                    . "(A local method of the same name as been found)");
379     }
380
381     $self->{override_method_modifiers}->{$method_name} = $method;
382 }
383
384 sub has_override_method_modifier {
385     my ($self, $method_name) = @_;
386     return exists $self->{override_method_modifiers}->{$method_name};
387 }
388
389 sub get_override_method_modifier {
390     my ($self, $method_name) = @_;
391     return $self->{override_method_modifiers}->{$method_name};
392 }
393
394 sub get_method_modifier_list {
395     my($self, $modifier_type) = @_;
396
397     return keys %{ $self->{$modifier_type . '_method_modifiers'} };
398 }
399
400 # This is currently not passing all the Moose tests.
401 sub does_role {
402     my ($self, $role_name) = @_;
403
404     (defined $role_name)
405         || $self->throw_error("You must supply a role name to look for");
406
407     # if we are it,.. then return true
408     return 1 if $role_name eq $self->name;
409     # otherwise.. check our children
410     for my $role (@{ $self->get_roles }) {
411         return 1 if $role->does_role($role_name);
412     }
413     return 0;
414 }
415
416
417 1;
418