1 package Moose::Meta::Role::Application::RoleSummation;
8 use Scalar::Util 'blessed';
11 use Moose::Meta::Role::Composite;
13 our $VERSION = '0.52';
14 our $AUTHORITY = 'cpan:STEVAN';
16 use base 'Moose::Meta::Role::Application';
18 __PACKAGE__->meta->add_attribute('role_params' => (
19 reader => 'role_params',
23 sub get_exclusions_for_role {
24 my ($self, $role) = @_;
25 $role = $role->name if blessed $role;
26 if ($self->role_params->{$role} && defined $self->role_params->{$role}->{excludes}) {
27 if (ref $self->role_params->{$role}->{excludes} eq 'ARRAY') {
28 return $self->role_params->{$role}->{excludes};
30 return [ $self->role_params->{$role}->{excludes} ];
35 sub get_method_aliases_for_role {
36 my ($self, $role) = @_;
37 $role = $role->name if blessed $role;
38 if ($self->role_params->{$role} && defined $self->role_params->{$role}->{alias}) {
39 return $self->role_params->{$role}->{alias};
44 sub 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;
52 sub is_method_aliased {
53 my ($self, $role, $method_name) = @_;
54 exists $self->get_method_aliases_for_role($role->name)->{$method_name} ? 1 : 0
57 sub is_aliased_method {
58 my ($self, $role, $method_name) = @_;
59 my %aliased_names = reverse %{$self->get_method_aliases_for_role($role->name)};
60 exists $aliased_names{$method_name} ? 1 : 0;
63 # stolen from List::MoreUtils ...
64 my $uniq = sub { my %h; map { $h{$_}++ == 0 ? $_ : () } @_ };
66 sub check_role_exclusions {
69 my @all_excluded_roles = $uniq->(map {
70 $_->get_excluded_roles_list
73 foreach my $role (@{$c->get_roles}) {
74 foreach my $excluded (@all_excluded_roles) {
75 confess "Conflict detected: " . $role->name . " excludes role '" . $excluded . "'"
76 if $role->does_role($excluded);
80 $c->add_excluded_roles(@all_excluded_roles);
83 sub check_required_methods {
86 my %all_required_methods = map { $_ => undef } $uniq->(map {
87 $_->get_required_method_list
90 foreach my $role (@{$c->get_roles}) {
91 foreach my $required (keys %all_required_methods) {
93 delete $all_required_methods{$required}
94 if $role->has_method($required)
95 || $self->is_aliased_method($role, $required);
99 $c->add_required_methods(keys %all_required_methods);
102 sub check_required_attributes {
106 sub apply_attributes {
109 my @all_attributes = map {
114 attr => $role->get_attribute($_),
116 } $role->get_attribute_list
120 foreach my $attr (@all_attributes) {
121 if (exists $seen{$attr->{name}}) {
122 confess "We have encountered an attribute conflict with '" . $attr->{name} . "' "
123 . "during composition. This is fatal error and cannot be disambiguated."
124 if $seen{$attr->{name}} != $attr->{attr};
126 $seen{$attr->{name}} = $attr->{attr};
129 foreach my $attr (@all_attributes) {
130 $c->add_attribute($attr->{name}, $attr->{attr});
137 my @all_methods = map {
139 my $aliases = $self->get_method_aliases_for_role($role);
140 my %excludes = map { $_ => undef } @{ $self->get_exclusions_for_role($role) };
143 exists $excludes{$_} ? () :
147 method => $role->get_method($_),
149 } $role->get_method_list),
153 name => $aliases->{$_},
154 method => $role->get_method($_),
160 my (%seen, %method_map);
161 foreach my $method (@all_methods) {
162 if (exists $seen{$method->{name}}) {
163 if ($seen{$method->{name}}->body != $method->{method}->body) {
164 $c->add_required_methods($method->{name});
165 delete $method_map{$method->{name}};
170 $seen{$method->{name}} = $method->{method};
171 $method_map{$method->{name}} = $method->{method};
174 $c->alias_method($_ => $method_map{$_}) for keys %method_map;
177 sub apply_override_method_modifiers {
180 my @all_overrides = map {
185 method => $role->get_override_method_modifier($_),
187 } $role->get_method_modifier_list('override');
191 foreach my $override (@all_overrides) {
192 confess "Role '" . $c->name . "' has encountered an 'override' method conflict " .
193 "during composition (A local method of the same name as been found). This " .
195 if $c->has_method($override->{name});
196 if (exists $seen{$override->{name}}) {
197 confess "We have encountered an 'override' method conflict during " .
198 "composition (Two 'override' methods of the same name encountered). " .
199 "This is fatal error."
200 if $seen{$override->{name}} != $override->{method};
202 $seen{$override->{name}} = $override->{method};
205 $c->add_override_method_modifier(
206 $_->{name}, $_->{method}
207 ) for @all_overrides;
211 sub apply_method_modifiers {
212 my ($self, $modifier_type, $c) = @_;
213 my $add = "add_${modifier_type}_method_modifier";
214 my $get = "get_${modifier_type}_method_modifiers";
215 foreach my $role (@{$c->get_roles}) {
216 foreach my $method_name ($role->get_method_modifier_list($modifier_type)) {
220 ) foreach $role->$get($method_name);
233 Moose::Meta::Role::Application::RoleSummation - Combine two or more roles
237 Summation composes two traits, forming the union of non-conflicting
238 bindings and 'disabling' the conflicting bindings
250 =item B<get_exclusions_for_role>
252 =item B<get_method_aliases_for_role>
254 =item B<is_aliased_method>
256 =item B<is_method_aliased>
258 =item B<is_method_excluded>
262 =item B<check_role_exclusions>
264 =item B<check_required_methods>
266 =item B<check_required_attributes>
268 =item B<apply_attributes>
270 =item B<apply_methods>
272 =item B<apply_method_modifiers>
274 =item B<apply_override_method_modifiers>
280 All complex software has bugs lurking in it, and this module is no
281 exception. If you find a bug please either email me, or add the bug
286 Stevan Little E<lt>stevan@iinteractive.comE<gt>
288 =head1 COPYRIGHT AND LICENSE
290 Copyright 2006-2008 by Infinity Interactive, Inc.
292 L<http://www.iinteractive.com>
294 This library is free software; you can redistribute it and/or modify
295 it under the same terms as Perl itself.