1 package Moose::Meta::Role::Application::RoleSummation;
7 use Scalar::Util 'blessed';
9 use Moose::Meta::Role::Composite;
11 our $VERSION = '1.9900';
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 {
121 for my $role ( @{ $c->get_roles } ) {
122 push @all_attributes,
123 map { $role->get_attribute($_) } $role->get_attribute_list;
127 foreach my $attr (@all_attributes) {
128 my $name = $attr->name;
130 if ( exists $seen{$name} ) {
131 next if $seen{$name}->is_same_as($attr);
133 my $role1 = $seen{$name}->associated_role->name;
134 my $role2 = $attr->associated_role->name;
138 "We have encountered an attribute conflict with '$name' "
139 . "during role composition. "
140 . " This attribute is defined in both $role1 and $role2."
141 . " This is fatal error and cannot be disambiguated." );
144 $seen{$name} = $attr;
147 foreach my $attr (@all_attributes) {
148 $c->add_attribute( $attr->clone );
155 my @all_methods = map {
157 my $aliases = $self->get_method_aliases_for_role($role);
158 my %excludes = map { $_ => undef } @{ $self->get_exclusions_for_role($role) };
161 exists $excludes{$_} ? () :
165 method => $role->get_method($_),
168 grep { !$_->isa('Class::MOP::Method::Meta') }
169 $role->_get_local_methods),
173 name => $aliases->{$_},
174 method => $role->get_method($_),
180 my (%seen, %method_map);
181 foreach my $method (@all_methods) {
182 my $seen = $seen{$method->{name}};
185 if ($seen->{method}->body != $method->{method}->body) {
186 $c->add_conflicting_method(
187 name => $method->{name},
188 roles => [$method->{role}->name, $seen->{role}->name],
191 delete $method_map{$method->{name}};
196 $seen{$method->{name}} = $method;
197 $method_map{$method->{name}} = $method->{method};
200 $c->add_method($_ => $method_map{$_}) for keys %method_map;
203 sub apply_override_method_modifiers {
206 my @all_overrides = map {
211 method => $role->get_override_method_modifier($_),
213 } $role->get_method_modifier_list('override');
217 foreach my $override (@all_overrides) {
218 if ( $c->has_method($override->{name}) ){
220 Moose->throw_error( "Role '" . $c->name . "' has encountered an 'override' method conflict " .
221 "during composition (A local method of the same name as been found). This " .
224 if (exists $seen{$override->{name}}) {
225 if ( $seen{$override->{name}} != $override->{method} ) {
227 Moose->throw_error( "We have encountered an 'override' method conflict during " .
228 "composition (Two 'override' methods of the same name encountered). " .
229 "This is fatal error.")
232 $seen{$override->{name}} = $override->{method};
235 $c->add_override_method_modifier(
236 $_->{name}, $_->{method}
237 ) for @all_overrides;
241 sub apply_method_modifiers {
242 my ($self, $modifier_type, $c) = @_;
243 my $add = "add_${modifier_type}_method_modifier";
244 my $get = "get_${modifier_type}_method_modifiers";
245 foreach my $role (@{$c->get_roles}) {
246 foreach my $method_name ($role->get_method_modifier_list($modifier_type)) {
250 ) foreach $role->$get($method_name);
263 Moose::Meta::Role::Application::RoleSummation - Combine two or more roles
267 Summation composes two traits, forming the union of non-conflicting
268 bindings and 'disabling' the conflicting bindings
280 =item B<get_exclusions_for_role>
282 =item B<get_method_aliases_for_role>
284 =item B<is_aliased_method>
286 =item B<is_method_aliased>
288 =item B<is_method_excluded>
292 =item B<check_role_exclusions>
294 =item B<check_required_methods>
296 =item B<check_required_attributes>
298 =item B<apply_attributes>
300 =item B<apply_methods>
302 =item B<apply_method_modifiers>
304 =item B<apply_override_method_modifiers>
310 See L<Moose/BUGS> for details on reporting bugs.
314 Stevan Little E<lt>stevan@iinteractive.comE<gt>
316 =head1 COPYRIGHT AND LICENSE
318 Copyright 2006-2010 by Infinity Interactive, Inc.
320 L<http://www.iinteractive.com>
322 This library is free software; you can redistribute it and/or modify
323 it under the same terms as Perl itself.