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