1 package Moose::Meta::Role::Application::RoleSummation;
7 use Scalar::Util 'blessed';
9 use Moose::Meta::Role::Composite;
11 our $AUTHORITY = 'cpan:STEVAN';
13 use base 'Moose::Meta::Role::Application';
15 __PACKAGE__->meta->add_attribute('role_params' => (
16 reader => 'role_params',
20 sub get_exclusions_for_role {
21 my ($self, $role) = @_;
22 $role = $role->name if blessed $role;
23 my $excludes_key = exists $self->role_params->{$role}->{'-excludes'} ?
24 '-excludes' : 'excludes';
25 if ($self->role_params->{$role} && defined $self->role_params->{$role}->{$excludes_key}) {
26 if (ref $self->role_params->{$role}->{$excludes_key} eq 'ARRAY') {
27 return $self->role_params->{$role}->{$excludes_key};
29 return [ $self->role_params->{$role}->{$excludes_key} ];
34 sub get_method_aliases_for_role {
35 my ($self, $role) = @_;
36 $role = $role->name if blessed $role;
37 my $alias_key = exists $self->role_params->{$role}->{'-alias'} ?
39 if ($self->role_params->{$role} && defined $self->role_params->{$role}->{$alias_key}) {
40 return $self->role_params->{$role}->{$alias_key};
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 sub check_role_exclusions {
68 for my $role (@{ $c->get_roles }) {
69 my $name = $role->name;
71 for my $excluded ($role->get_excluded_roles_list) {
72 push @{ $excluded_roles{$excluded} }, $name;
76 foreach my $role (@{$c->get_roles}) {
77 foreach my $excluded (keys %excluded_roles) {
78 next unless $role->does_role($excluded);
80 my @excluding = @{ $excluded_roles{$excluded} };
83 Moose->throw_error(sprintf "Conflict detected: Role%s %s exclude%s role '%s'", (@excluding == 1 ? '' : 's'), join(', ', @excluding), (@excluding == 1 ? 's' : ''), $excluded);
87 $c->add_excluded_roles(keys %excluded_roles);
90 sub check_required_methods {
93 my %all_required_methods =
94 map { $_->name => $_ }
95 map { $_->get_required_method_list }
98 foreach my $role (@{$c->get_roles}) {
99 foreach my $required (keys %all_required_methods) {
101 delete $all_required_methods{$required}
102 if $role->has_method($required)
103 || $self->is_aliased_method($role, $required);
107 $c->add_required_methods(values %all_required_methods);
110 sub check_required_attributes {
114 sub apply_attributes {
119 for my $role ( @{ $c->get_roles } ) {
120 push @all_attributes,
121 map { $role->get_attribute($_) } $role->get_attribute_list;
125 foreach my $attr (@all_attributes) {
126 my $name = $attr->name;
128 if ( exists $seen{$name} ) {
129 next if $seen{$name}->is_same_as($attr);
131 my $role1 = $seen{$name}->associated_role->name;
132 my $role2 = $attr->associated_role->name;
136 "We have encountered an attribute conflict with '$name' "
137 . "during role composition. "
138 . " This attribute is defined in both $role1 and $role2."
139 . " This is fatal error and cannot be disambiguated." );
142 $seen{$name} = $attr;
145 foreach my $attr (@all_attributes) {
146 $c->add_attribute( $attr->clone );
153 my @all_methods = map {
155 my $aliases = $self->get_method_aliases_for_role($role);
156 my %excludes = map { $_ => undef } @{ $self->get_exclusions_for_role($role) };
159 exists $excludes{$_} ? () :
163 method => $role->get_method($_),
166 grep { !$_->isa('Class::MOP::Method::Meta') }
167 $role->_get_local_methods),
171 name => $aliases->{$_},
172 method => $role->get_method($_),
178 my (%seen, %method_map);
179 foreach my $method (@all_methods) {
180 my $seen = $seen{$method->{name}};
183 if ($seen->{method}->body != $method->{method}->body) {
184 $c->add_conflicting_method(
185 name => $method->{name},
186 roles => [$method->{role}->name, $seen->{role}->name],
189 delete $method_map{$method->{name}};
194 $seen{$method->{name}} = $method;
195 $method_map{$method->{name}} = $method->{method};
198 $c->add_method($_ => $method_map{$_}) for keys %method_map;
201 sub apply_override_method_modifiers {
204 my @all_overrides = map {
209 method => $role->get_override_method_modifier($_),
211 } $role->get_method_modifier_list('override');
215 foreach my $override (@all_overrides) {
216 if ( $c->has_method($override->{name}) ){
218 Moose->throw_error( "Role '" . $c->name . "' has encountered an 'override' method conflict " .
219 "during composition (A local method of the same name as been found). This " .
222 if (exists $seen{$override->{name}}) {
223 if ( $seen{$override->{name}} != $override->{method} ) {
225 Moose->throw_error( "We have encountered an 'override' method conflict during " .
226 "composition (Two 'override' methods of the same name encountered). " .
227 "This is fatal error.")
230 $seen{$override->{name}} = $override->{method};
233 $c->add_override_method_modifier(
234 $_->{name}, $_->{method}
235 ) for @all_overrides;
239 sub apply_method_modifiers {
240 my ($self, $modifier_type, $c) = @_;
241 my $add = "add_${modifier_type}_method_modifier";
242 my $get = "get_${modifier_type}_method_modifiers";
243 foreach my $role (@{$c->get_roles}) {
244 foreach my $method_name ($role->get_method_modifier_list($modifier_type)) {
248 ) foreach $role->$get($method_name);
255 # ABSTRACT: Combine two or more roles
263 Summation composes two traits, forming the union of non-conflicting
264 bindings and 'disabling' the conflicting bindings
276 =item B<get_exclusions_for_role>
278 =item B<get_method_aliases_for_role>
280 =item B<is_aliased_method>
282 =item B<is_method_aliased>
284 =item B<is_method_excluded>
288 =item B<check_role_exclusions>
290 =item B<check_required_methods>
292 =item B<check_required_attributes>
294 =item B<apply_attributes>
296 =item B<apply_methods>
298 =item B<apply_method_modifiers>
300 =item B<apply_override_method_modifiers>
306 See L<Moose/BUGS> for details on reporting bugs.