Use dzil Authority plugin - remove $AUTHORITY from code
[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
fb1e11d5 11use base 'Moose::Meta::Role::Application';
12
28412c0b 13__PACKAGE__->meta->add_attribute('role_params' => (
14 reader => 'role_params',
15 default => sub { {} }
16));
17
18sub get_exclusions_for_role {
19 my ($self, $role) = @_;
20 $role = $role->name if blessed $role;
c8b8d92f 21 my $excludes_key = exists $self->role_params->{$role}->{'-excludes'} ?
22 '-excludes' : 'excludes';
23 if ($self->role_params->{$role} && defined $self->role_params->{$role}->{$excludes_key}) {
24 if (ref $self->role_params->{$role}->{$excludes_key} eq 'ARRAY') {
25 return $self->role_params->{$role}->{$excludes_key};
28412c0b 26 }
c8b8d92f 27 return [ $self->role_params->{$role}->{$excludes_key} ];
28412c0b 28 }
29 return [];
30}
31
32sub get_method_aliases_for_role {
33 my ($self, $role) = @_;
34 $role = $role->name if blessed $role;
c8b8d92f 35 my $alias_key = exists $self->role_params->{$role}->{'-alias'} ?
36 '-alias' : 'alias';
37 if ($self->role_params->{$role} && defined $self->role_params->{$role}->{$alias_key}) {
38 return $self->role_params->{$role}->{$alias_key};
28412c0b 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
f785aad8 115 my @all_attributes;
116
117 for my $role ( @{ $c->get_roles } ) {
118 push @all_attributes,
119 map { $role->get_attribute($_) } $role->get_attribute_list;
120 }
d03bd989 121
fb1e11d5 122 my %seen;
123 foreach my $attr (@all_attributes) {
f785aad8 124 my $name = $attr->name;
125
126 if ( exists $seen{$name} ) {
127 next if $seen{$name}->is_same_as($attr);
128
129 my $role1 = $seen{$name}->associated_role->name;
130 my $role2 = $attr->associated_role->name;
131
132 require Moose;
133 Moose->throw_error(
134 "We have encountered an attribute conflict with '$name' "
135 . "during role composition. "
136 . " This attribute is defined in both $role1 and $role2."
137 . " This is fatal error and cannot be disambiguated." );
fb1e11d5 138 }
f785aad8 139
140 $seen{$name} = $attr;
fb1e11d5 141 }
142
d03bd989 143 foreach my $attr (@all_attributes) {
f785aad8 144 $c->add_attribute( $attr->clone );
fb1e11d5 145 }
146}
147
148sub apply_methods {
149 my ($self, $c) = @_;
d03bd989 150
fb1e11d5 151 my @all_methods = map {
28412c0b 152 my $role = $_;
153 my $aliases = $self->get_method_aliases_for_role($role);
154 my %excludes = map { $_ => undef } @{ $self->get_exclusions_for_role($role) };
155 (
d03bd989 156 (map {
28412c0b 157 exists $excludes{$_} ? () :
d03bd989 158 +{
28412c0b 159 role => $role,
160 name => $_,
161 method => $role->get_method($_),
162 }
ba7d613d 163 } map { $_->name }
164 grep { !$_->isa('Class::MOP::Method::Meta') }
165 $role->_get_local_methods),
d03bd989 166 (map {
167 +{
28412c0b 168 role => $role,
169 name => $aliases->{$_},
170 method => $role->get_method($_),
d03bd989 171 }
28412c0b 172 } keys %$aliases)
173 );
fb1e11d5 174 } @{$c->get_roles};
d03bd989 175
fb1e11d5 176 my (%seen, %method_map);
177 foreach my $method (@all_methods) {
49b8c93a 178 my $seen = $seen{$method->{name}};
179
180 if ($seen) {
181 if ($seen->{method}->body != $method->{method}->body) {
bb153262 182 $c->add_conflicting_method(
49b8c93a 183 name => $method->{name},
78485053 184 roles => [$method->{role}->name, $seen->{role}->name],
49b8c93a 185 );
186
fb1e11d5 187 delete $method_map{$method->{name}};
188 next;
d03bd989 189 }
190 }
191
49b8c93a 192 $seen{$method->{name}} = $method;
fb1e11d5 193 $method_map{$method->{name}} = $method->{method};
194 }
195
87e63626 196 $c->add_method($_ => $method_map{$_}) for keys %method_map;
fb1e11d5 197}
198
199sub apply_override_method_modifiers {
200 my ($self, $c) = @_;
d03bd989 201
fb1e11d5 202 my @all_overrides = map {
203 my $role = $_;
d03bd989 204 map {
205 +{
fb1e11d5 206 name => $_,
207 method => $role->get_override_method_modifier($_),
208 }
209 } $role->get_method_modifier_list('override');
210 } @{$c->get_roles};
d03bd989 211
fb1e11d5 212 my %seen;
213 foreach my $override (@all_overrides) {
70ea9161 214 if ( $c->has_method($override->{name}) ){
215 require Moose;
216 Moose->throw_error( "Role '" . $c->name . "' has encountered an 'override' method conflict " .
217 "during composition (A local method of the same name as been found). This " .
218 "is fatal error." )
219 }
fb1e11d5 220 if (exists $seen{$override->{name}}) {
70ea9161 221 if ( $seen{$override->{name}} != $override->{method} ) {
222 require Moose;
223 Moose->throw_error( "We have encountered an 'override' method conflict during " .
224 "composition (Two 'override' methods of the same name encountered). " .
225 "This is fatal error.")
226 }
fb1e11d5 227 }
228 $seen{$override->{name}} = $override->{method};
229 }
d03bd989 230
fb1e11d5 231 $c->add_override_method_modifier(
232 $_->{name}, $_->{method}
233 ) for @all_overrides;
d03bd989 234
fb1e11d5 235}
236
237sub apply_method_modifiers {
238 my ($self, $modifier_type, $c) = @_;
239 my $add = "add_${modifier_type}_method_modifier";
240 my $get = "get_${modifier_type}_method_modifiers";
241 foreach my $role (@{$c->get_roles}) {
242 foreach my $method_name ($role->get_method_modifier_list($modifier_type)) {
243 $c->$add(
244 $method_name,
245 $_
246 ) foreach $role->$get($method_name);
247 }
248 }
249}
250
fb1e11d5 2511;
252
ad46f524 253# ABSTRACT: Combine two or more roles
254
fb1e11d5 255__END__
256
257=pod
258
fb1e11d5 259=head1 DESCRIPTION
260
d03bd989 261Summation composes two traits, forming the union of non-conflicting
fb1e11d5 262bindings and 'disabling' the conflicting bindings
263
264=head2 METHODS
265
266=over 4
267
268=item B<new>
269
270=item B<meta>
271
28412c0b 272=item B<role_params>
273
274=item B<get_exclusions_for_role>
275
276=item B<get_method_aliases_for_role>
277
278=item B<is_aliased_method>
279
280=item B<is_method_aliased>
281
282=item B<is_method_excluded>
283
fb1e11d5 284=item B<apply>
285
709c321c 286=item B<check_role_exclusions>
287
fb1e11d5 288=item B<check_required_methods>
289
709c321c 290=item B<check_required_attributes>
fb1e11d5 291
292=item B<apply_attributes>
293
294=item B<apply_methods>
295
296=item B<apply_method_modifiers>
297
fb1e11d5 298=item B<apply_override_method_modifiers>
299
300=back
301
302=head1 BUGS
303
d4048ef3 304See L<Moose/BUGS> for details on reporting bugs.
fb1e11d5 305
fb1e11d5 306=cut
307