1 package Moose::Meta::Role::Application::RoleSummation;
8 use Scalar::Util 'blessed';
11 use Moose::Meta::Role::Composite;
13 our $VERSION = '0.55_02';
14 $VERSION = eval $VERSION;
15 our $AUTHORITY = 'cpan:STEVAN';
17 use base 'Moose::Meta::Role::Application';
19 __PACKAGE__->meta->add_attribute('role_params' => (
20 reader => 'role_params',
24 sub get_exclusions_for_role {
25 my ($self, $role) = @_;
26 $role = $role->name if blessed $role;
27 if ($self->role_params->{$role} && defined $self->role_params->{$role}->{excludes}) {
28 if (ref $self->role_params->{$role}->{excludes} eq 'ARRAY') {
29 return $self->role_params->{$role}->{excludes};
31 return [ $self->role_params->{$role}->{excludes} ];
36 sub get_method_aliases_for_role {
37 my ($self, $role) = @_;
38 $role = $role->name if blessed $role;
39 if ($self->role_params->{$role} && defined $self->role_params->{$role}->{alias}) {
40 return $self->role_params->{$role}->{alias};
45 sub is_method_excluded {
46 my ($self, $role, $method_name) = @_;
47 foreach ($self->get_exclusions_for_role($role->name)) {
48 return 1 if $_ eq $method_name;
53 sub is_method_aliased {
54 my ($self, $role, $method_name) = @_;
55 exists $self->get_method_aliases_for_role($role->name)->{$method_name} ? 1 : 0
58 sub is_aliased_method {
59 my ($self, $role, $method_name) = @_;
60 my %aliased_names = reverse %{$self->get_method_aliases_for_role($role->name)};
61 exists $aliased_names{$method_name} ? 1 : 0;
64 # stolen from List::MoreUtils ...
65 my $uniq = sub { my %h; map { $h{$_}++ == 0 ? $_ : () } @_ };
67 sub check_role_exclusions {
70 my @all_excluded_roles = $uniq->(map {
71 $_->get_excluded_roles_list
74 foreach my $role (@{$c->get_roles}) {
75 foreach my $excluded (@all_excluded_roles) {
76 confess "Conflict detected: " . $role->name . " excludes role '" . $excluded . "'"
77 if $role->does_role($excluded);
81 $c->add_excluded_roles(@all_excluded_roles);
84 sub check_required_methods {
87 my %all_required_methods = map { $_ => undef } $uniq->(map {
88 $_->get_required_method_list
91 foreach my $role (@{$c->get_roles}) {
92 foreach my $required (keys %all_required_methods) {
94 delete $all_required_methods{$required}
95 if $role->has_method($required)
96 || $self->is_aliased_method($role, $required);
100 $c->add_required_methods(keys %all_required_methods);
103 sub check_required_attributes {
107 sub apply_attributes {
110 my @all_attributes = map {
115 attr => $role->get_attribute($_),
117 } $role->get_attribute_list
121 foreach my $attr (@all_attributes) {
122 if (exists $seen{$attr->{name}}) {
123 confess "We have encountered an attribute conflict with '" . $attr->{name} . "' "
124 . "during composition. This is fatal error and cannot be disambiguated."
125 if $seen{$attr->{name}} != $attr->{attr};
127 $seen{$attr->{name}} = $attr->{attr};
130 foreach my $attr (@all_attributes) {
131 $c->add_attribute($attr->{name}, $attr->{attr});
138 my @all_methods = map {
140 my $aliases = $self->get_method_aliases_for_role($role);
141 my %excludes = map { $_ => undef } @{ $self->get_exclusions_for_role($role) };
144 exists $excludes{$_} ? () :
148 method => $role->get_method($_),
150 } $role->get_method_list),
154 name => $aliases->{$_},
155 method => $role->get_method($_),
161 my (%seen, %method_map);
162 foreach my $method (@all_methods) {
163 if (exists $seen{$method->{name}}) {
164 if ($seen{$method->{name}}->body != $method->{method}->body) {
165 $c->add_required_methods($method->{name});
166 delete $method_map{$method->{name}};
171 $seen{$method->{name}} = $method->{method};
172 $method_map{$method->{name}} = $method->{method};
175 $c->alias_method($_ => $method_map{$_}) for keys %method_map;
178 sub apply_override_method_modifiers {
181 my @all_overrides = map {
186 method => $role->get_override_method_modifier($_),
188 } $role->get_method_modifier_list('override');
192 foreach my $override (@all_overrides) {
193 confess "Role '" . $c->name . "' has encountered an 'override' method conflict " .
194 "during composition (A local method of the same name as been found). This " .
196 if $c->has_method($override->{name});
197 if (exists $seen{$override->{name}}) {
198 confess "We have encountered an 'override' method conflict during " .
199 "composition (Two 'override' methods of the same name encountered). " .
200 "This is fatal error."
201 if $seen{$override->{name}} != $override->{method};
203 $seen{$override->{name}} = $override->{method};
206 $c->add_override_method_modifier(
207 $_->{name}, $_->{method}
208 ) for @all_overrides;
212 sub apply_method_modifiers {
213 my ($self, $modifier_type, $c) = @_;
214 my $add = "add_${modifier_type}_method_modifier";
215 my $get = "get_${modifier_type}_method_modifiers";
216 foreach my $role (@{$c->get_roles}) {
217 foreach my $method_name ($role->get_method_modifier_list($modifier_type)) {
221 ) foreach $role->$get($method_name);
234 Moose::Meta::Role::Application::RoleSummation - Combine two or more roles
238 Summation composes two traits, forming the union of non-conflicting
239 bindings and 'disabling' the conflicting bindings
251 =item B<get_exclusions_for_role>
253 =item B<get_method_aliases_for_role>
255 =item B<is_aliased_method>
257 =item B<is_method_aliased>
259 =item B<is_method_excluded>
263 =item B<check_role_exclusions>
265 =item B<check_required_methods>
267 =item B<check_required_attributes>
269 =item B<apply_attributes>
271 =item B<apply_methods>
273 =item B<apply_method_modifiers>
275 =item B<apply_override_method_modifiers>
281 All complex software has bugs lurking in it, and this module is no
282 exception. If you find a bug please either email me, or add the bug
287 Stevan Little E<lt>stevan@iinteractive.comE<gt>
289 =head1 COPYRIGHT AND LICENSE
291 Copyright 2006-2008 by Infinity Interactive, Inc.
293 L<http://www.iinteractive.com>
295 This library is free software; you can redistribute it and/or modify
296 it under the same terms as Perl itself.