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