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