bump version to 1.09
[gitmo/Moose.git] / lib / Moose / Meta / Role / Application / RoleSummation.pm
CommitLineData
fb1e11d5 1package Moose::Meta::Role::Application::RoleSummation;
2
3use strict;
4use warnings;
5use metaclass;
6
d2682e6e 7use Scalar::Util 'blessed';
fb1e11d5 8
9use Moose::Meta::Role::Composite;
10
60f08160 11our $VERSION = '1.09';
e606ae5f 12$VERSION = eval $VERSION;
fb1e11d5 13our $AUTHORITY = 'cpan:STEVAN';
14
15use base 'Moose::Meta::Role::Application';
16
28412c0b 17__PACKAGE__->meta->add_attribute('role_params' => (
18 reader => 'role_params',
19 default => sub { {} }
20));
21
22sub 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
36sub 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
47sub 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
55sub 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
60sub 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 66sub 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
92sub 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 112sub check_required_attributes {
d03bd989 113
709c321c 114}
115
fb1e11d5 116sub apply_attributes {
117 my ($self, $c) = @_;
d03bd989 118
f785aad8 119 my @all_attributes;
120
121 for my $role ( @{ $c->get_roles } ) {
122 push @all_attributes,
123 map { $role->get_attribute($_) } $role->get_attribute_list;
124 }
d03bd989 125
fb1e11d5 126 my %seen;
127 foreach my $attr (@all_attributes) {
f785aad8 128 my $name = $attr->name;
129
130 if ( exists $seen{$name} ) {
131 next if $seen{$name}->is_same_as($attr);
132
133 my $role1 = $seen{$name}->associated_role->name;
134 my $role2 = $attr->associated_role->name;
135
136 require Moose;
137 Moose->throw_error(
138 "We have encountered an attribute conflict with '$name' "
139 . "during role composition. "
140 . " This attribute is defined in both $role1 and $role2."
141 . " This is fatal error and cannot be disambiguated." );
fb1e11d5 142 }
f785aad8 143
144 $seen{$name} = $attr;
fb1e11d5 145 }
146
d03bd989 147 foreach my $attr (@all_attributes) {
f785aad8 148 $c->add_attribute( $attr->clone );
fb1e11d5 149 }
150}
151
152sub apply_methods {
153 my ($self, $c) = @_;
d03bd989 154
fb1e11d5 155 my @all_methods = map {
28412c0b 156 my $role = $_;
157 my $aliases = $self->get_method_aliases_for_role($role);
158 my %excludes = map { $_ => undef } @{ $self->get_exclusions_for_role($role) };
a8547bc0 159 $excludes{meta} = undef;
28412c0b 160 (
d03bd989 161 (map {
28412c0b 162 exists $excludes{$_} ? () :
d03bd989 163 +{
28412c0b 164 role => $role,
165 name => $_,
166 method => $role->get_method($_),
167 }
168 } $role->get_method_list),
d03bd989 169 (map {
170 +{
28412c0b 171 role => $role,
172 name => $aliases->{$_},
173 method => $role->get_method($_),
d03bd989 174 }
28412c0b 175 } keys %$aliases)
176 );
fb1e11d5 177 } @{$c->get_roles};
d03bd989 178
fb1e11d5 179 my (%seen, %method_map);
180 foreach my $method (@all_methods) {
49b8c93a 181 my $seen = $seen{$method->{name}};
182
183 if ($seen) {
184 if ($seen->{method}->body != $method->{method}->body) {
bb153262 185 $c->add_conflicting_method(
49b8c93a 186 name => $method->{name},
78485053 187 roles => [$method->{role}->name, $seen->{role}->name],
49b8c93a 188 );
189
fb1e11d5 190 delete $method_map{$method->{name}};
191 next;
d03bd989 192 }
193 }
194
49b8c93a 195 $seen{$method->{name}} = $method;
fb1e11d5 196 $method_map{$method->{name}} = $method->{method};
197 }
198
87e63626 199 $c->add_method($_ => $method_map{$_}) for keys %method_map;
fb1e11d5 200}
201
202sub apply_override_method_modifiers {
203 my ($self, $c) = @_;
d03bd989 204
fb1e11d5 205 my @all_overrides = map {
206 my $role = $_;
d03bd989 207 map {
208 +{
fb1e11d5 209 name => $_,
210 method => $role->get_override_method_modifier($_),
211 }
212 } $role->get_method_modifier_list('override');
213 } @{$c->get_roles};
d03bd989 214
fb1e11d5 215 my %seen;
216 foreach my $override (@all_overrides) {
70ea9161 217 if ( $c->has_method($override->{name}) ){
218 require Moose;
219 Moose->throw_error( "Role '" . $c->name . "' has encountered an 'override' method conflict " .
220 "during composition (A local method of the same name as been found). This " .
221 "is fatal error." )
222 }
fb1e11d5 223 if (exists $seen{$override->{name}}) {
70ea9161 224 if ( $seen{$override->{name}} != $override->{method} ) {
225 require Moose;
226 Moose->throw_error( "We have encountered an 'override' method conflict during " .
227 "composition (Two 'override' methods of the same name encountered). " .
228 "This is fatal error.")
229 }
fb1e11d5 230 }
231 $seen{$override->{name}} = $override->{method};
232 }
d03bd989 233
fb1e11d5 234 $c->add_override_method_modifier(
235 $_->{name}, $_->{method}
236 ) for @all_overrides;
d03bd989 237
fb1e11d5 238}
239
240sub apply_method_modifiers {
241 my ($self, $modifier_type, $c) = @_;
242 my $add = "add_${modifier_type}_method_modifier";
243 my $get = "get_${modifier_type}_method_modifiers";
244 foreach my $role (@{$c->get_roles}) {
245 foreach my $method_name ($role->get_method_modifier_list($modifier_type)) {
246 $c->$add(
247 $method_name,
248 $_
249 ) foreach $role->$get($method_name);
250 }
251 }
252}
253
fb1e11d5 2541;
255
256__END__
257
258=pod
259
260=head1 NAME
261
ab76842e 262Moose::Meta::Role::Application::RoleSummation - Combine two or more roles
fb1e11d5 263
264=head1 DESCRIPTION
265
d03bd989 266Summation composes two traits, forming the union of non-conflicting
fb1e11d5 267bindings and 'disabling' the conflicting bindings
268
269=head2 METHODS
270
271=over 4
272
273=item B<new>
274
275=item B<meta>
276
28412c0b 277=item B<role_params>
278
279=item B<get_exclusions_for_role>
280
281=item B<get_method_aliases_for_role>
282
283=item B<is_aliased_method>
284
285=item B<is_method_aliased>
286
287=item B<is_method_excluded>
288
fb1e11d5 289=item B<apply>
290
709c321c 291=item B<check_role_exclusions>
292
fb1e11d5 293=item B<check_required_methods>
294
709c321c 295=item B<check_required_attributes>
fb1e11d5 296
297=item B<apply_attributes>
298
299=item B<apply_methods>
300
301=item B<apply_method_modifiers>
302
fb1e11d5 303=item B<apply_override_method_modifiers>
304
305=back
306
307=head1 BUGS
308
d4048ef3 309See L<Moose/BUGS> for details on reporting bugs.
fb1e11d5 310
311=head1 AUTHOR
312
313Stevan Little E<lt>stevan@iinteractive.comE<gt>
314
315=head1 COPYRIGHT AND LICENSE
316
7e0492d3 317Copyright 2006-2010 by Infinity Interactive, Inc.
fb1e11d5 318
319L<http://www.iinteractive.com>
320
321This library is free software; you can redistribute it and/or modify
322it under the same terms as Perl itself.
323
324=cut
325