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