Commits

Toby Inkster committed defbfdd

composition improvements

  • Participants
  • Parent commits b7e66bf

Comments (0)

Files changed (3)

File lib/MooseX/ErsatzMethod.pm

 	sub apply_all_ersatz_methods_to_class
 	{
 		my ($self, $class) = @_;
-		$_->apply_to_class($class) for $self->all_ersatz_methods;
+		for ($self->all_ersatz_methods)
+		{
+			next if $self->has_method($_->name);
+			$_->apply_to_class($class);
+		}
 	}
 	sub composition_class_roles
 	{
 =item C<< apply_all_ersatz_methods_to_class($class) >>
 
 Given a Moose::Meta::Class object, iterates through C<all_ersatz_methods>
-applying each to the class.
+applying each to the class. This procedure skips any ersatz method for which
+this role can provide a real method.
 
 =back
 
 
 =back
 
+=head1 CAVEATS
+
+  with 'Role1';  with 'Role2';   # No!
+  with qw( Role1 Role2 );        # Yes
+
 =head1 BUGS
 
 Please report any bugs to

File t/04requires.t

+use strict;
+use warnings;
+use Test::More tests => 2;
+
+BEGIN {
+	package Local::Test::Role1;
+	no thanks;
+	use Moose::Role;
+	use MooseX::ErsatzMethod;
+	ersatz foo => sub { 1 };
+};
+
+BEGIN {
+	package Local::Test::Role2;
+	no thanks;
+	use Moose::Role;
+	requires 'foo';
+};
+
+ok eval {
+	package Local::Test::Class1;
+	no thanks;
+	use Moose;
+	with qw(
+		Local::Test::Role1
+		Local::Test::Role2
+	);
+	1;
+};
+
+ok eval {
+	package Local::Test::Class2;
+	no thanks;
+	use Moose;
+	with qw(
+		Local::Test::Role2
+		Local::Test::Role1
+	);
+	1;
+};

File t/05combination.t

+use strict;
+use warnings;
+use Test::More tests => 4;
+
+BEGIN {
+	package Local::Test::Role1;
+	no thanks;
+	use Moose::Role;
+	use MooseX::ErsatzMethod;
+	ersatz foo => sub { 1 };
+};
+
+BEGIN {
+	package Local::Test::Role2;
+	no thanks;
+	use Moose::Role;
+	sub foo { 2 }
+};
+
+ok eval {
+	package Local::Test::Class1;
+	no thanks;
+	use Moose;
+	with qw(
+		Local::Test::Role1
+		Local::Test::Role2
+	);
+	1;
+};
+
+is(
+	Local::Test::Class1->new->foo,
+	2,
+);
+
+ok eval {
+	package Local::Test::Class2;
+	no thanks;
+	use Moose;
+	with qw(
+		Local::Test::Role2
+		Local::Test::Role1
+	);
+	1;
+};
+
+is(
+	Local::Test::Class1->new->foo,
+	2,
+);