1 package Moose::Meta::Role::Application::RoleSummation;
7 use Scalar::Util 'blessed';
9 use Moose::Meta::Role::Composite;
11 our $VERSION = '0.89';
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 my $excludes_key = exists $self->role_params->{$role}->{'-excludes'} ?
26 '-excludes' : 'excludes';
27 if ($self->role_params->{$role} && defined $self->role_params->{$role}->{$excludes_key}) {
28 if (ref $self->role_params->{$role}->{$excludes_key} eq 'ARRAY') {
29 return $self->role_params->{$role}->{$excludes_key};
31 return [ $self->role_params->{$role}->{$excludes_key} ];
36 sub get_method_aliases_for_role {
37 my ($self, $role) = @_;
38 $role = $role->name if blessed $role;
39 my $alias_key = exists $self->role_params->{$role}->{'-alias'} ?
41 if ($self->role_params->{$role} && defined $self->role_params->{$role}->{$alias_key}) {
42 return $self->role_params->{$role}->{$alias_key};
47 sub is_method_excluded {
48 my ($self, $role, $method_name) = @_;
49 foreach ($self->get_exclusions_for_role($role->name)) {
50 return 1 if $_ eq $method_name;
55 sub is_method_aliased {
56 my ($self, $role, $method_name) = @_;
57 exists $self->get_method_aliases_for_role($role->name)->{$method_name} ? 1 : 0
60 sub is_aliased_method {
61 my ($self, $role, $method_name) = @_;
62 my %aliased_names = reverse %{$self->get_method_aliases_for_role($role->name)};
63 exists $aliased_names{$method_name} ? 1 : 0;
66 sub check_role_exclusions {
70 for my $role (@{ $c->get_roles }) {
71 my $name = $role->name;
73 for my $excluded ($role->get_excluded_roles_list) {
74 push @{ $excluded_roles{$excluded} }, $name;
78 foreach my $role (@{$c->get_roles}) {
79 foreach my $excluded (keys %excluded_roles) {
80 next unless $role->does_role($excluded);
82 my @excluding = @{ $excluded_roles{$excluded} };
85 Moose->throw_error(sprintf "Conflict detected: Role%s %s exclude%s role '%s'", (@excluding == 1 ? '' : 's'), join(', ', @excluding), (@excluding == 1 ? 's' : ''), $excluded);
89 $c->add_excluded_roles(keys %excluded_roles);
92 sub check_required_methods {
95 my %all_required_methods =
96 map { $_->name => $_ }
97 map { $_->get_required_method_list }
100 foreach my $role (@{$c->get_roles}) {
101 foreach my $required (keys %all_required_methods) {
103 delete $all_required_methods{$required}
104 if $role->has_method($required)
105 || $self->is_aliased_method($role, $required);
109 $c->add_required_methods(values %all_required_methods);
112 sub check_required_attributes {
116 sub apply_attributes {
119 my @all_attributes = map {
124 attr => $role->get_attribute($_),
126 } $role->get_attribute_list
130 foreach my $attr (@all_attributes) {
131 if (exists $seen{$attr->{name}}) {
132 if ( $seen{$attr->{name}} != $attr->{attr} ) {
134 Moose->throw_error("We have encountered an attribute conflict with '" . $attr->{name} . "' "
135 . "during composition. This is fatal error and cannot be disambiguated.")
138 $seen{$attr->{name}} = $attr->{attr};
141 foreach my $attr (@all_attributes) {
142 $c->add_attribute($attr->{name}, $attr->{attr});
149 my @all_methods = map {
151 my $aliases = $self->get_method_aliases_for_role($role);
152 my %excludes = map { $_ => undef } @{ $self->get_exclusions_for_role($role) };
155 exists $excludes{$_} ? () :
159 method => $role->get_method($_),
161 } $role->get_method_list),
165 name => $aliases->{$_},
166 method => $role->get_method($_),
172 my (%seen, %method_map);
173 foreach my $method (@all_methods) {
174 my $seen = $seen{$method->{name}};
177 if ($seen->{method}->body != $method->{method}->body) {
178 $c->add_conflicting_method(
179 name => $method->{name},
180 roles => [$method->{role}->name, $seen->{role}->name],
183 delete $method_map{$method->{name}};
188 $seen{$method->{name}} = $method;
189 $method_map{$method->{name}} = $method->{method};
192 $c->add_method($_ => $method_map{$_}) for keys %method_map;
195 sub apply_override_method_modifiers {
198 my @all_overrides = map {
203 method => $role->get_override_method_modifier($_),
205 } $role->get_method_modifier_list('override');
209 foreach my $override (@all_overrides) {
210 if ( $c->has_method($override->{name}) ){
212 Moose->throw_error( "Role '" . $c->name . "' has encountered an 'override' method conflict " .
213 "during composition (A local method of the same name as been found). This " .
216 if (exists $seen{$override->{name}}) {
217 if ( $seen{$override->{name}} != $override->{method} ) {
219 Moose->throw_error( "We have encountered an 'override' method conflict during " .
220 "composition (Two 'override' methods of the same name encountered). " .
221 "This is fatal error.")
224 $seen{$override->{name}} = $override->{method};
227 $c->add_override_method_modifier(
228 $_->{name}, $_->{method}
229 ) for @all_overrides;
233 sub apply_method_modifiers {
234 my ($self, $modifier_type, $c) = @_;
235 my $add = "add_${modifier_type}_method_modifier";
236 my $get = "get_${modifier_type}_method_modifiers";
237 foreach my $role (@{$c->get_roles}) {
238 foreach my $method_name ($role->get_method_modifier_list($modifier_type)) {
242 ) foreach $role->$get($method_name);
255 Moose::Meta::Role::Application::RoleSummation - Combine two or more roles
259 Summation composes two traits, forming the union of non-conflicting
260 bindings and 'disabling' the conflicting bindings
272 =item B<get_exclusions_for_role>
274 =item B<get_method_aliases_for_role>
276 =item B<is_aliased_method>
278 =item B<is_method_aliased>
280 =item B<is_method_excluded>
284 =item B<check_role_exclusions>
286 =item B<check_required_methods>
288 =item B<check_required_attributes>
290 =item B<apply_attributes>
292 =item B<apply_methods>
294 =item B<apply_method_modifiers>
296 =item B<apply_override_method_modifiers>
302 All complex software has bugs lurking in it, and this module is no
303 exception. If you find a bug please either email me, or add the bug
308 Stevan Little E<lt>stevan@iinteractive.comE<gt>
310 =head1 COPYRIGHT AND LICENSE
312 Copyright 2006-2009 by Infinity Interactive, Inc.
314 L<http://www.iinteractive.com>
316 This library is free software; you can redistribute it and/or modify
317 it under the same terms as Perl itself.