bump version to 0.80
[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
3ee7b5ad 11our $VERSION = '0.80';
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;
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
34sub 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 }
d03bd989 40 return {};
28412c0b 41}
42
43sub 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
51sub 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
56sub is_aliased_method {
57 my ($self, $role, $method_name) = @_;
d03bd989 58 my %aliased_names = reverse %{$self->get_method_aliases_for_role($role->name)};
28412c0b 59 exists $aliased_names{$method_name} ? 1 : 0;
60}
61
fb1e11d5 62sub check_role_exclusions {
63 my ($self, $c) = @_;
64
88cfb4cb 65 my %excluded_roles;
66 for my $role (@{ $c->get_roles }) {
67 my $name = $role->name;
68
69 for my $excluded ($role->get_excluded_roles_list) {
70 push @{ $excluded_roles{$excluded} }, $name;
71 }
72 }
fb1e11d5 73
74 foreach my $role (@{$c->get_roles}) {
88cfb4cb 75 foreach my $excluded (keys %excluded_roles) {
76 next unless $role->does_role($excluded);
77
78 my @excluding = @{ $excluded_roles{$excluded} };
70ea9161 79
80 require Moose;
5b5b7c12 81 Moose->throw_error(sprintf "Conflict detected: Role%s %s exclude%s role '%s'", (@excluding == 1 ? '' : 's'), join(', ', @excluding), (@excluding == 1 ? 's' : ''), $excluded);
fb1e11d5 82 }
83 }
84
88cfb4cb 85 $c->add_excluded_roles(keys %excluded_roles);
fb1e11d5 86}
87
88sub check_required_methods {
89 my ($self, $c) = @_;
90
d2682e6e 91 my %all_required_methods =
7e79d987 92 map { $_->name => $_ }
d2682e6e 93 map { $_->get_required_method_list }
94 @{$c->get_roles};
fb1e11d5 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
7e79d987 105 $c->add_required_methods(values %all_required_methods);
fb1e11d5 106}
107
709c321c 108sub check_required_attributes {
d03bd989 109
709c321c 110}
111
fb1e11d5 112sub 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
142sub 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) {
49b8c93a 170 my $seen = $seen{$method->{name}};
171
172 if ($seen) {
173 if ($seen->{method}->body != $method->{method}->body) {
bb153262 174 $c->add_conflicting_method(
49b8c93a 175 name => $method->{name},
78485053 176 roles => [$method->{role}->name, $seen->{role}->name],
49b8c93a 177 );
178
fb1e11d5 179 delete $method_map{$method->{name}};
180 next;
d03bd989 181 }
182 }
183
49b8c93a 184 $seen{$method->{name}} = $method;
fb1e11d5 185 $method_map{$method->{name}} = $method->{method};
186 }
187
87e63626 188 $c->add_method($_ => $method_map{$_}) for keys %method_map;
fb1e11d5 189}
190
191sub apply_override_method_modifiers {
192 my ($self, $c) = @_;
d03bd989 193
fb1e11d5 194 my @all_overrides = map {
195 my $role = $_;
d03bd989 196 map {
197 +{
fb1e11d5 198 name => $_,
199 method => $role->get_override_method_modifier($_),
200 }
201 } $role->get_method_modifier_list('override');
202 } @{$c->get_roles};
d03bd989 203
fb1e11d5 204 my %seen;
205 foreach my $override (@all_overrides) {
70ea9161 206 if ( $c->has_method($override->{name}) ){
207 require Moose;
208 Moose->throw_error( "Role '" . $c->name . "' has encountered an 'override' method conflict " .
209 "during composition (A local method of the same name as been found). This " .
210 "is fatal error." )
211 }
fb1e11d5 212 if (exists $seen{$override->{name}}) {
70ea9161 213 if ( $seen{$override->{name}} != $override->{method} ) {
214 require Moose;
215 Moose->throw_error( "We have encountered an 'override' method conflict during " .
216 "composition (Two 'override' methods of the same name encountered). " .
217 "This is fatal error.")
218 }
fb1e11d5 219 }
220 $seen{$override->{name}} = $override->{method};
221 }
d03bd989 222
fb1e11d5 223 $c->add_override_method_modifier(
224 $_->{name}, $_->{method}
225 ) for @all_overrides;
d03bd989 226
fb1e11d5 227}
228
229sub apply_method_modifiers {
230 my ($self, $modifier_type, $c) = @_;
231 my $add = "add_${modifier_type}_method_modifier";
232 my $get = "get_${modifier_type}_method_modifiers";
233 foreach my $role (@{$c->get_roles}) {
234 foreach my $method_name ($role->get_method_modifier_list($modifier_type)) {
235 $c->$add(
236 $method_name,
237 $_
238 ) foreach $role->$get($method_name);
239 }
240 }
241}
242
fb1e11d5 2431;
244
245__END__
246
247=pod
248
249=head1 NAME
250
ab76842e 251Moose::Meta::Role::Application::RoleSummation - Combine two or more roles
fb1e11d5 252
253=head1 DESCRIPTION
254
d03bd989 255Summation composes two traits, forming the union of non-conflicting
fb1e11d5 256bindings and 'disabling' the conflicting bindings
257
258=head2 METHODS
259
260=over 4
261
262=item B<new>
263
264=item B<meta>
265
28412c0b 266=item B<role_params>
267
268=item B<get_exclusions_for_role>
269
270=item B<get_method_aliases_for_role>
271
272=item B<is_aliased_method>
273
274=item B<is_method_aliased>
275
276=item B<is_method_excluded>
277
fb1e11d5 278=item B<apply>
279
709c321c 280=item B<check_role_exclusions>
281
fb1e11d5 282=item B<check_required_methods>
283
709c321c 284=item B<check_required_attributes>
fb1e11d5 285
286=item B<apply_attributes>
287
288=item B<apply_methods>
289
290=item B<apply_method_modifiers>
291
fb1e11d5 292=item B<apply_override_method_modifiers>
293
294=back
295
296=head1 BUGS
297
298All complex software has bugs lurking in it, and this module is no
299exception. If you find a bug please either email me, or add the bug
300to cpan-RT.
301
302=head1 AUTHOR
303
304Stevan Little E<lt>stevan@iinteractive.comE<gt>
305
306=head1 COPYRIGHT AND LICENSE
307
2840a3b2 308Copyright 2006-2009 by Infinity Interactive, Inc.
fb1e11d5 309
310L<http://www.iinteractive.com>
311
312This library is free software; you can redistribute it and/or modify
313it under the same terms as Perl itself.
314
315=cut
316