1 package Moose::Meta::Role::Application::RoleSummation;
7 use Scalar::Util 'blessed';
9 use Moose::Meta::Role::Composite;
11 our $VERSION = '0.79';
12 $VERSION = eval $VERSION;
13 our $AUTHORITY = 'cpan:STEVAN';
15 use base 'Moose::Meta::Role::Application';
17 __PACKAGE__->meta->add_attribute('role_params' => (
18 reader => 'role_params',
22 sub 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};
29 return [ $self->role_params->{$role}->{excludes} ];
34 sub 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};
43 sub 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;
51 sub is_method_aliased {
52 my ($self, $role, $method_name) = @_;
53 exists $self->get_method_aliases_for_role($role->name)->{$method_name} ? 1 : 0
56 sub 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;
62 sub check_role_exclusions {
66 for my $role (@{ $c->get_roles }) {
67 my $name = $role->name;
69 for my $excluded ($role->get_excluded_roles_list) {
70 push @{ $excluded_roles{$excluded} }, $name;
74 foreach my $role (@{$c->get_roles}) {
75 foreach my $excluded (keys %excluded_roles) {
76 next unless $role->does_role($excluded);
78 my @excluding = @{ $excluded_roles{$excluded} };
81 Moose->throw_error(sprintf "Conflict detected: Role%s %s exclude%s role '%s'", (@excluding == 1 ? '' : 's'), join(', ', @excluding), (@excluding == 1 ? 's' : ''), $excluded);
85 $c->add_excluded_roles(keys %excluded_roles);
88 sub check_required_methods {
91 my %all_required_methods =
92 map { $_->name => $_ }
93 map { $_->get_required_method_list }
96 foreach my $role (@{$c->get_roles}) {
97 foreach my $required (keys %all_required_methods) {
99 delete $all_required_methods{$required}
100 if $role->has_method($required)
101 || $self->is_aliased_method($role, $required);
105 $c->add_required_methods(values %all_required_methods);
108 sub check_required_attributes {
112 sub apply_attributes {
115 my @all_attributes = map {
120 attr => $role->get_attribute($_),
122 } $role->get_attribute_list
126 foreach my $attr (@all_attributes) {
127 if (exists $seen{$attr->{name}}) {
128 if ( $seen{$attr->{name}} != $attr->{attr} ) {
130 Moose->throw_error("We have encountered an attribute conflict with '" . $attr->{name} . "' "
131 . "during composition. This is fatal error and cannot be disambiguated.")
134 $seen{$attr->{name}} = $attr->{attr};
137 foreach my $attr (@all_attributes) {
138 $c->add_attribute($attr->{name}, $attr->{attr});
145 my @all_methods = map {
147 my $aliases = $self->get_method_aliases_for_role($role);
148 my %excludes = map { $_ => undef } @{ $self->get_exclusions_for_role($role) };
151 exists $excludes{$_} ? () :
155 method => $role->get_method($_),
157 } $role->get_method_list),
161 name => $aliases->{$_},
162 method => $role->get_method($_),
168 my (%seen, %method_map);
169 foreach my $method (@all_methods) {
170 my $seen = $seen{$method->{name}};
173 if ($seen->{method}->body != $method->{method}->body) {
174 $c->add_conflicted_method(
175 name => $method->{name},
176 roles => [$method->{role}->name, $seen->{role}->name],
179 delete $method_map{$method->{name}};
184 $seen{$method->{name}} = $method;
185 $method_map{$method->{name}} = $method->{method};
188 $c->add_method($_ => $method_map{$_}) for keys %method_map;
191 sub apply_override_method_modifiers {
194 my @all_overrides = map {
199 method => $role->get_override_method_modifier($_),
201 } $role->get_method_modifier_list('override');
205 foreach my $override (@all_overrides) {
206 if ( $c->has_method($override->{name}) ){
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 " .
212 if (exists $seen{$override->{name}}) {
213 if ( $seen{$override->{name}} != $override->{method} ) {
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.")
220 $seen{$override->{name}} = $override->{method};
223 $c->add_override_method_modifier(
224 $_->{name}, $_->{method}
225 ) for @all_overrides;
229 sub 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)) {
238 ) foreach $role->$get($method_name);
251 Moose::Meta::Role::Application::RoleSummation - Combine two or more roles
255 Summation composes two traits, forming the union of non-conflicting
256 bindings and 'disabling' the conflicting bindings
268 =item B<get_exclusions_for_role>
270 =item B<get_method_aliases_for_role>
272 =item B<is_aliased_method>
274 =item B<is_method_aliased>
276 =item B<is_method_excluded>
280 =item B<check_role_exclusions>
282 =item B<check_required_methods>
284 =item B<check_required_attributes>
286 =item B<apply_attributes>
288 =item B<apply_methods>
290 =item B<apply_method_modifiers>
292 =item B<apply_override_method_modifiers>
298 All complex software has bugs lurking in it, and this module is no
299 exception. If you find a bug please either email me, or add the bug
304 Stevan Little E<lt>stevan@iinteractive.comE<gt>
306 =head1 COPYRIGHT AND LICENSE
308 Copyright 2006-2009 by Infinity Interactive, Inc.
310 L<http://www.iinteractive.com>
312 This library is free software; you can redistribute it and/or modify
313 it under the same terms as Perl itself.