Checking in changes prior to tagging of version 0.93. Changelog diff is:
[gitmo/Moose.git] / lib / Moose / Meta / Role / Application / RoleSummation.pm
1 package Moose::Meta::Role::Application::RoleSummation;
2
3 use strict;
4 use warnings;
5 use metaclass;
6
7 use Scalar::Util 'blessed';
8
9 use Moose::Meta::Role::Composite;
10
11 our $VERSION   = '0.93';
12 $VERSION = eval $VERSION;
13 our $AUTHORITY = 'cpan:STEVAN';
14
15 use base 'Moose::Meta::Role::Application';
16
17 __PACKAGE__->meta->add_attribute('role_params' => (
18     reader  => 'role_params',
19     default => sub { {} }
20 ));
21
22 sub get_exclusions_for_role {
23     my ($self, $role) = @_;
24     $role = $role->name if blessed $role;
25     my $excludes_key = exists $self->role_params->{$role}->{'-excludes'} ?
26                            '-excludes' : 'excludes';
27     if ($self->role_params->{$role} && defined $self->role_params->{$role}->{$excludes_key}) {
28         if (ref $self->role_params->{$role}->{$excludes_key} eq 'ARRAY') {
29             return $self->role_params->{$role}->{$excludes_key};
30         }
31         return [ $self->role_params->{$role}->{$excludes_key} ];
32     }
33     return [];
34 }
35
36 sub get_method_aliases_for_role {
37     my ($self, $role) = @_;
38     $role = $role->name if blessed $role;
39     my $alias_key = exists $self->role_params->{$role}->{'-alias'} ?
40                         '-alias' : 'alias';
41     if ($self->role_params->{$role} && defined $self->role_params->{$role}->{$alias_key}) {
42         return $self->role_params->{$role}->{$alias_key};
43     }
44     return {};
45 }
46
47 sub is_method_excluded {
48     my ($self, $role, $method_name) = @_;
49     foreach ($self->get_exclusions_for_role($role->name)) {
50         return 1 if $_ eq $method_name;
51     }
52     return 0;
53 }
54
55 sub is_method_aliased {
56     my ($self, $role, $method_name) = @_;
57     exists $self->get_method_aliases_for_role($role->name)->{$method_name} ? 1 : 0
58 }
59
60 sub is_aliased_method {
61     my ($self, $role, $method_name) = @_;
62     my %aliased_names = reverse %{$self->get_method_aliases_for_role($role->name)};
63     exists $aliased_names{$method_name} ? 1 : 0;
64 }
65
66 sub check_role_exclusions {
67     my ($self, $c) = @_;
68
69     my %excluded_roles;
70     for my $role (@{ $c->get_roles }) {
71         my $name = $role->name;
72
73         for my $excluded ($role->get_excluded_roles_list) {
74             push @{ $excluded_roles{$excluded} }, $name;
75         }
76     }
77
78     foreach my $role (@{$c->get_roles}) {
79         foreach my $excluded (keys %excluded_roles) {
80             next unless $role->does_role($excluded);
81
82             my @excluding = @{ $excluded_roles{$excluded} };
83
84             require Moose;
85             Moose->throw_error(sprintf "Conflict detected: Role%s %s exclude%s role '%s'", (@excluding == 1 ? '' : 's'), join(', ', @excluding), (@excluding == 1 ? 's' : ''), $excluded);
86         }
87     }
88
89     $c->add_excluded_roles(keys %excluded_roles);
90 }
91
92 sub check_required_methods {
93     my ($self, $c) = @_;
94
95     my %all_required_methods =
96         map { $_->name => $_ }
97         map { $_->get_required_method_list }
98         @{$c->get_roles};
99
100     foreach my $role (@{$c->get_roles}) {
101         foreach my $required (keys %all_required_methods) {
102
103             delete $all_required_methods{$required}
104                 if $role->has_method($required)
105                 || $self->is_aliased_method($role, $required);
106         }
107     }
108
109     $c->add_required_methods(values %all_required_methods);
110 }
111
112 sub check_required_attributes {
113
114 }
115
116 sub apply_attributes {
117     my ($self, $c) = @_;
118
119     my @all_attributes = map {
120         my $role = $_;
121         map {
122             +{
123                 name => $_,
124                 attr => $role->get_attribute($_),
125             }
126         } $role->get_attribute_list
127     } @{$c->get_roles};
128
129     my %seen;
130     foreach my $attr (@all_attributes) {
131         if (exists $seen{$attr->{name}}) {
132             if ( $seen{$attr->{name}} != $attr->{attr} ) {
133                 require Moose;
134                 Moose->throw_error("We have encountered an attribute conflict with '" . $attr->{name} . "' "
135                                    . "during composition. This is fatal error and cannot be disambiguated.")
136             }
137         }
138         $seen{$attr->{name}} = $attr->{attr};
139     }
140
141     foreach my $attr (@all_attributes) {
142         $c->add_attribute($attr->{name}, $attr->{attr});
143     }
144 }
145
146 sub apply_methods {
147     my ($self, $c) = @_;
148
149     my @all_methods = map {
150         my $role     = $_;
151         my $aliases  = $self->get_method_aliases_for_role($role);
152         my %excludes = map { $_ => undef } @{ $self->get_exclusions_for_role($role) };
153         $excludes{meta} = undef;
154         (
155             (map {
156                 exists $excludes{$_} ? () :
157                 +{
158                     role   => $role,
159                     name   => $_,
160                     method => $role->get_method($_),
161                 }
162             } $role->get_method_list),
163             (map {
164                 +{
165                     role   => $role,
166                     name   => $aliases->{$_},
167                     method => $role->get_method($_),
168                 }
169             } keys %$aliases)
170         );
171     } @{$c->get_roles};
172
173     my (%seen, %method_map);
174     foreach my $method (@all_methods) {
175         my $seen = $seen{$method->{name}};
176
177         if ($seen) {
178             if ($seen->{method}->body != $method->{method}->body) {
179                 $c->add_conflicting_method(
180                     name  => $method->{name},
181                     roles => [$method->{role}->name, $seen->{role}->name],
182                 );
183
184                 delete $method_map{$method->{name}};
185                 next;
186             }
187         }
188
189         $seen{$method->{name}}       = $method;
190         $method_map{$method->{name}} = $method->{method};
191     }
192
193     $c->add_method($_ => $method_map{$_}) for keys %method_map;
194 }
195
196 sub apply_override_method_modifiers {
197     my ($self, $c) = @_;
198
199     my @all_overrides = map {
200         my $role = $_;
201         map {
202             +{
203                 name   => $_,
204                 method => $role->get_override_method_modifier($_),
205             }
206         } $role->get_method_modifier_list('override');
207     } @{$c->get_roles};
208
209     my %seen;
210     foreach my $override (@all_overrides) {
211         if ( $c->has_method($override->{name}) ){
212             require Moose;
213             Moose->throw_error( "Role '" . $c->name . "' has encountered an 'override' method conflict " .
214                                 "during composition (A local method of the same name as been found). This " .
215                                 "is fatal error." )
216         }
217         if (exists $seen{$override->{name}}) {
218             if ( $seen{$override->{name}} != $override->{method} ) {
219                 require Moose;
220                 Moose->throw_error( "We have encountered an 'override' method conflict during " .
221                                     "composition (Two 'override' methods of the same name encountered). " .
222                                     "This is fatal error.")
223             }
224         }
225         $seen{$override->{name}} = $override->{method};
226     }
227
228     $c->add_override_method_modifier(
229         $_->{name}, $_->{method}
230     ) for @all_overrides;
231
232 }
233
234 sub apply_method_modifiers {
235     my ($self, $modifier_type, $c) = @_;
236     my $add = "add_${modifier_type}_method_modifier";
237     my $get = "get_${modifier_type}_method_modifiers";
238     foreach my $role (@{$c->get_roles}) {
239         foreach my $method_name ($role->get_method_modifier_list($modifier_type)) {
240             $c->$add(
241                 $method_name,
242                 $_
243             ) foreach $role->$get($method_name);
244         }
245     }
246 }
247
248 1;
249
250 __END__
251
252 =pod
253
254 =head1 NAME
255
256 Moose::Meta::Role::Application::RoleSummation - Combine two or more roles
257
258 =head1 DESCRIPTION
259
260 Summation composes two traits, forming the union of non-conflicting
261 bindings and 'disabling' the conflicting bindings
262
263 =head2 METHODS
264
265 =over 4
266
267 =item B<new>
268
269 =item B<meta>
270
271 =item B<role_params>
272
273 =item B<get_exclusions_for_role>
274
275 =item B<get_method_aliases_for_role>
276
277 =item B<is_aliased_method>
278
279 =item B<is_method_aliased>
280
281 =item B<is_method_excluded>
282
283 =item B<apply>
284
285 =item B<check_role_exclusions>
286
287 =item B<check_required_methods>
288
289 =item B<check_required_attributes>
290
291 =item B<apply_attributes>
292
293 =item B<apply_methods>
294
295 =item B<apply_method_modifiers>
296
297 =item B<apply_override_method_modifiers>
298
299 =back
300
301 =head1 BUGS
302
303 All complex software has bugs lurking in it, and this module is no
304 exception. If you find a bug please either email me, or add the bug
305 to cpan-RT.
306
307 =head1 AUTHOR
308
309 Stevan Little E<lt>stevan@iinteractive.comE<gt>
310
311 =head1 COPYRIGHT AND LICENSE
312
313 Copyright 2006-2009 by Infinity Interactive, Inc.
314
315 L<http://www.iinteractive.com>
316
317 This library is free software; you can redistribute it and/or modify
318 it under the same terms as Perl itself.
319
320 =cut
321