bump version to 1.19
[gitmo/Moose.git] / lib / Moose / Meta / Role / Application / RoleSummation.pm
index 66c2381..6153b6e 100644 (file)
@@ -4,11 +4,11 @@ use strict;
 use warnings;
 use metaclass;
 
-use Scalar::Util    'blessed';
+use Scalar::Util 'blessed';
 
 use Moose::Meta::Role::Composite;
 
-our $VERSION   = '0.71';
+our $VERSION   = '1.19';
 $VERSION = eval $VERSION;
 our $AUTHORITY = 'cpan:STEVAN';
 
@@ -22,11 +22,13 @@ __PACKAGE__->meta->add_attribute('role_params' => (
 sub get_exclusions_for_role {
     my ($self, $role) = @_;
     $role = $role->name if blessed $role;
-    if ($self->role_params->{$role} && defined $self->role_params->{$role}->{excludes}) {
-        if (ref $self->role_params->{$role}->{excludes} eq 'ARRAY') {
-            return $self->role_params->{$role}->{excludes};
+    my $excludes_key = exists $self->role_params->{$role}->{'-excludes'} ?
+                           '-excludes' : 'excludes';
+    if ($self->role_params->{$role} && defined $self->role_params->{$role}->{$excludes_key}) {
+        if (ref $self->role_params->{$role}->{$excludes_key} eq 'ARRAY') {
+            return $self->role_params->{$role}->{$excludes_key};
         }
-        return [ $self->role_params->{$role}->{excludes} ];
+        return [ $self->role_params->{$role}->{$excludes_key} ];
     }
     return [];
 }
@@ -34,10 +36,12 @@ sub get_exclusions_for_role {
 sub get_method_aliases_for_role {
     my ($self, $role) = @_;
     $role = $role->name if blessed $role;
-    if ($self->role_params->{$role} && defined $self->role_params->{$role}->{alias}) {
-        return $self->role_params->{$role}->{alias};
+    my $alias_key = exists $self->role_params->{$role}->{'-alias'} ?
+                        '-alias' : 'alias';
+    if ($self->role_params->{$role} && defined $self->role_params->{$role}->{$alias_key}) {
+        return $self->role_params->{$role}->{$alias_key};
     }
-    return {};    
+    return {};
 }
 
 sub is_method_excluded {
@@ -55,13 +59,10 @@ sub is_method_aliased {
 
 sub is_aliased_method {
     my ($self, $role, $method_name) = @_;
-    my %aliased_names = reverse %{$self->get_method_aliases_for_role($role->name)};    
+    my %aliased_names = reverse %{$self->get_method_aliases_for_role($role->name)};
     exists $aliased_names{$method_name} ? 1 : 0;
 }
 
-# stolen from List::MoreUtils ...
-my $uniq = sub { my %h; map { $h{$_}++ == 0 ? $_ : () } @_ };
-
 sub check_role_exclusions {
     my ($self, $c) = @_;
 
@@ -91,93 +92,108 @@ sub check_role_exclusions {
 sub check_required_methods {
     my ($self, $c) = @_;
 
-    my %all_required_methods = map { $_ => undef } $uniq->(map {
-        $_->get_required_method_list
-    } @{$c->get_roles});
+    my %all_required_methods =
+        map { $_->name => $_ }
+        map { $_->get_required_method_list }
+        @{$c->get_roles};
 
     foreach my $role (@{$c->get_roles}) {
         foreach my $required (keys %all_required_methods) {
-            
+
             delete $all_required_methods{$required}
                 if $role->has_method($required)
                 || $self->is_aliased_method($role, $required);
         }
     }
 
-    $c->add_required_methods(keys %all_required_methods);
+    $c->add_required_methods(values %all_required_methods);
 }
 
 sub check_required_attributes {
-    
+
 }
 
 sub apply_attributes {
     my ($self, $c) = @_;
-    
-    my @all_attributes = map {
-        my $role = $_;
-        map { 
-            +{ 
-                name => $_,
-                attr => $role->get_attribute($_),
-            }
-        } $role->get_attribute_list
-    } @{$c->get_roles};
-    
+
+    my @all_attributes;
+
+    for my $role ( @{ $c->get_roles } ) {
+        push @all_attributes,
+            map { $role->get_attribute($_) } $role->get_attribute_list;
+    }
+
     my %seen;
     foreach my $attr (@all_attributes) {
-        if (exists $seen{$attr->{name}}) {
-            if ( $seen{$attr->{name}} != $attr->{attr} ) {
-                require Moose;
-                Moose->throw_error("We have encountered an attribute conflict with '" . $attr->{name} . "' " 
-                                   . "during composition. This is fatal error and cannot be disambiguated.")
-            }
+        my $name = $attr->name;
+
+        if ( exists $seen{$name} ) {
+            next if $seen{$name}->is_same_as($attr);
+
+            my $role1 = $seen{$name}->associated_role->name;
+            my $role2 = $attr->associated_role->name;
+
+            require Moose;
+            Moose->throw_error(
+                "We have encountered an attribute conflict with '$name' "
+                    . "during role composition. "
+                    . " This attribute is defined in both $role1 and $role2."
+                    . " This is fatal error and cannot be disambiguated." );
         }
-        $seen{$attr->{name}} = $attr->{attr};
+
+        $seen{$name} = $attr;
     }
 
-    foreach my $attr (@all_attributes) {    
-        $c->add_attribute($attr->{name}, $attr->{attr});
+    foreach my $attr (@all_attributes) {
+        $c->add_attribute( $attr->clone );
     }
 }
 
 sub apply_methods {
     my ($self, $c) = @_;
-    
+
     my @all_methods = map {
         my $role     = $_;
         my $aliases  = $self->get_method_aliases_for_role($role);
         my %excludes = map { $_ => undef } @{ $self->get_exclusions_for_role($role) };
         (
-            (map { 
+            (map {
                 exists $excludes{$_} ? () :
-                +{ 
+                +{
                     role   => $role,
                     name   => $_,
                     method => $role->get_method($_),
                 }
-            } $role->get_method_list),
-            (map { 
-                +{ 
+            } map { $_->name }
+              grep { !$_->isa('Class::MOP::Method::Meta') }
+                   $role->_get_local_methods),
+            (map {
+                +{
                     role   => $role,
                     name   => $aliases->{$_},
                     method => $role->get_method($_),
-                }            
+                }
             } keys %$aliases)
         );
     } @{$c->get_roles};
-    
+
     my (%seen, %method_map);
     foreach my $method (@all_methods) {
-        if (exists $seen{$method->{name}}) {
-            if ($seen{$method->{name}}->body != $method->{method}->body) {
-                $c->add_required_methods($method->{name});
+        my $seen = $seen{$method->{name}};
+
+        if ($seen) {
+            if ($seen->{method}->body != $method->{method}->body) {
+                $c->add_conflicting_method(
+                    name  => $method->{name},
+                    roles => [$method->{role}->name, $seen->{role}->name],
+                );
+
                 delete $method_map{$method->{name}};
                 next;
-            }           
-        }       
-        
-        $seen{$method->{name}}       = $method->{method};
+            }
+        }
+
+        $seen{$method->{name}}       = $method;
         $method_map{$method->{name}} = $method->{method};
     }
 
@@ -186,17 +202,17 @@ sub apply_methods {
 
 sub apply_override_method_modifiers {
     my ($self, $c) = @_;
-    
+
     my @all_overrides = map {
         my $role = $_;
-        map { 
-            +{ 
+        map {
+            +{
                 name   => $_,
                 method => $role->get_override_method_modifier($_),
             }
         } $role->get_method_modifier_list('override');
     } @{$c->get_roles};
-    
+
     my %seen;
     foreach my $override (@all_overrides) {
         if ( $c->has_method($override->{name}) ){
@@ -215,11 +231,11 @@ sub apply_override_method_modifiers {
         }
         $seen{$override->{name}} = $override->{method};
     }
-        
+
     $c->add_override_method_modifier(
         $_->{name}, $_->{method}
     ) for @all_overrides;
-            
+
 }
 
 sub apply_method_modifiers {
@@ -248,7 +264,7 @@ Moose::Meta::Role::Application::RoleSummation - Combine two or more roles
 
 =head1 DESCRIPTION
 
-Summation composes two traits, forming the union of non-conflicting 
+Summation composes two traits, forming the union of non-conflicting
 bindings and 'disabling' the conflicting bindings
 
 =head2 METHODS
@@ -291,9 +307,7 @@ bindings and 'disabling' the conflicting bindings
 
 =head1 BUGS
 
-All complex software has bugs lurking in it, and this module is no
-exception. If you find a bug please either email me, or add the bug
-to cpan-RT.
+See L<Moose/BUGS> for details on reporting bugs.
 
 =head1 AUTHOR
 
@@ -301,7 +315,7 @@ Stevan Little E<lt>stevan@iinteractive.comE<gt>
 
 =head1 COPYRIGHT AND LICENSE
 
-Copyright 2006-2009 by Infinity Interactive, Inc.
+Copyright 2006-2010 by Infinity Interactive, Inc.
 
 L<http://www.iinteractive.com>