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