Convert Moose->throw_error to Moose::Util::throw
[gitmo/Moose.git] / lib / Moose / Meta / Role / Application / RoleSummation.pm
index 43af8d0..d07837d 100644 (file)
@@ -8,15 +8,12 @@ use Scalar::Util 'blessed';
 
 use Moose::Meta::Role::Composite;
 
-our $VERSION   = '0.91';
-$VERSION = eval $VERSION;
-our $AUTHORITY = 'cpan:STEVAN';
-
 use base 'Moose::Meta::Role::Application';
 
 __PACKAGE__->meta->add_attribute('role_params' => (
     reader  => 'role_params',
-    default => sub { {} }
+    default => sub { {} },
+    Class::MOP::_definition_context(),
 ));
 
 sub get_exclusions_for_role {
@@ -81,8 +78,7 @@ sub check_role_exclusions {
 
             my @excluding = @{ $excluded_roles{$excluded} };
 
-            require Moose;
-            Moose->throw_error(sprintf "Conflict detected: Role%s %s exclude%s role '%s'", (@excluding == 1 ? '' : 's'), join(', ', @excluding), (@excluding == 1 ? 's' : ''), $excluded);
+            Moose::Util::throw(sprintf "Conflict detected: Role%s %s exclude%s role '%s'", (@excluding == 1 ? '' : 's'), join(', ', @excluding), (@excluding == 1 ? 's' : ''), $excluded);
         }
     }
 
@@ -116,30 +112,35 @@ 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;
+
+            Moose::Util::throw(
+                "We have encountered an attribute conflict with '$name' "
+                    . "during role composition. "
+                    . " This attribute is defined in both $role1 and $role2."
+                    . " This is a 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});
+        $c->add_attribute( $attr->clone );
     }
 }
 
@@ -150,7 +151,6 @@ sub apply_methods {
         my $role     = $_;
         my $aliases  = $self->get_method_aliases_for_role($role);
         my %excludes = map { $_ => undef } @{ $self->get_exclusions_for_role($role) };
-        $excludes{meta} = undef;
         (
             (map {
                 exists $excludes{$_} ? () :
@@ -159,7 +159,9 @@ sub apply_methods {
                     name   => $_,
                     method => $role->get_method($_),
                 }
-            } $role->get_method_list),
+            } map { $_->name }
+              grep { !$_->isa('Class::MOP::Method::Meta') }
+                   $role->_get_local_methods),
             (map {
                 +{
                     role   => $role,
@@ -170,8 +172,9 @@ sub apply_methods {
         );
     } @{$c->get_roles};
 
-    my (%seen, %method_map);
+    my (%seen, %conflicts, %method_map);
     foreach my $method (@all_methods) {
+        next if $conflicts{$method->{name}};
         my $seen = $seen{$method->{name}};
 
         if ($seen) {
@@ -182,6 +185,7 @@ sub apply_methods {
                 );
 
                 delete $method_map{$method->{name}};
+                $conflicts{$method->{name}} = 1;
                 next;
             }
         }
@@ -209,15 +213,13 @@ sub apply_override_method_modifiers {
     my %seen;
     foreach my $override (@all_overrides) {
         if ( $c->has_method($override->{name}) ){
-            require Moose;
-            Moose->throw_error( "Role '" . $c->name . "' has encountered an 'override' method conflict " .
+            Moose::Util::throw( "Role '" . $c->name . "' has encountered an 'override' method conflict " .
                                 "during composition (A local method of the same name as been found). This " .
                                 "is fatal error." )
         }
         if (exists $seen{$override->{name}}) {
             if ( $seen{$override->{name}} != $override->{method} ) {
-                require Moose;
-                Moose->throw_error( "We have encountered an 'override' method conflict during " .
+                Moose::Util::throw( "We have encountered an 'override' method conflict during " .
                                     "composition (Two 'override' methods of the same name encountered). " .
                                     "This is fatal error.")
             }
@@ -247,14 +249,12 @@ sub apply_method_modifiers {
 
 1;
 
+# ABSTRACT: Combine two or more roles
+
 __END__
 
 =pod
 
-=head1 NAME
-
-Moose::Meta::Role::Application::RoleSummation - Combine two or more roles
-
 =head1 DESCRIPTION
 
 Summation composes two traits, forming the union of non-conflicting
@@ -300,22 +300,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.
-
-=head1 AUTHOR
-
-Stevan Little E<lt>stevan@iinteractive.comE<gt>
-
-=head1 COPYRIGHT AND LICENSE
-
-Copyright 2006-2009 by Infinity Interactive, Inc.
-
-L<http://www.iinteractive.com>
-
-This library is free software; you can redistribute it and/or modify
-it under the same terms as Perl itself.
+See L<Moose/BUGS> for details on reporting bugs.
 
 =cut