Skip to content

Commit

Permalink
Fix issue with role composition onto subclasses.
Browse files Browse the repository at this point in the history
Metaclass re-initialization was happening too late - i.e. after the methods
from the roles were composed, but before the method modifiers from the roles.

This resulted in the metaclass reinitialization losing the methods from the
roles, and then trying to apply the modifiers wrapping the lost methods
which, of course, entirely failed.

The solution is that we don't actaully need to perform force reinitialization
at this point, and so removing that line fixes everything.
  • Loading branch information
bobtfish committed May 31, 2010
1 parent 3ec1c24 commit 74ff013
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 3 deletions.
4 changes: 4 additions & 0 deletions Changes
@@ -1,3 +1,7 @@
* Fix issues cauing composing multiple (normal) roles onto a subclass of a
MooseX::MethodAttributes class to fail by removing a forced metaclass
reinitialization which wasn't needed.

0.21 Fri, 07 May 2010 04:48:54 +0200
* Add more metadata, including a repository url.

Expand Down
3 changes: 0 additions & 3 deletions lib/MooseX/MethodAttributes/Role/Meta/Class.pm
Expand Up @@ -108,9 +108,6 @@ foreach my $type (qw/after before around/) {
my $meta = shift;
my ($method_name) = @_;

# Ensure the correct metaclass
$meta = MooseX::MethodAttributes->init_meta( for_class => $meta->name );

my $code = $meta->$orig(@_);
my $method = $meta->get_method($method_name);
if (
Expand Down
83 changes: 83 additions & 0 deletions t/role_comp.t
@@ -0,0 +1,83 @@
use strict;
use warnings;
use Test::More;
use Test::Exception;

use Moose::Util qw/apply_all_roles/;

{
package BaseClass;
use Moose;
use MooseX::MethodAttributes;
no Moose;
}

{
package AClass;
use Moose;
BEGIN { extends 'BaseClass' }
sub foo : Bar {}

no Moose;
}

{
package Role1;
use Moose::Role;
our $called = 0;
sub pack { $called++ }
no Moose::Role;
}

{
package Role2;
use Moose::Role;

our $called = 0;
around pack => sub {
my ($orig, $self, @rest) = @_;
$called++;
$self->$orig(@rest);
};
no Moose::Role;
}

{
package BClass;
use Moose;
BEGIN { extends 'AClass' };

sub moo : Quux {}

::lives_ok { with qw/Role1 Role2/ };
no Moose;
}

{
package CClass;
use Moose;
BEGIN { extends 'AClass' };

sub moo : Quux {}
no Moose;
}

my $c = CClass->new;
lives_ok { apply_all_roles($c, qw/Role1 Role2/) };

foreach my $i (BClass->new, $c) {
$Role1::called = $Role2::called = 0;
can_ok $i, 'pack' and $i->pack;
is $Role1::called, 1;
is $Role2::called, 1;
is_deeply(
$i->meta->find_method_by_name('foo')->attributes,
[(q{Bar})],
);
is_deeply(
$i->meta->find_method_by_name('moo')->attributes,
[(q{Quux})],
);
}

done_testing;

0 comments on commit 74ff013

Please sign in to comment.