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