$package_name = $class . '::__ANON__::' . $ANON_SERIAL;
}
+
# instantiate a module
{
no strict 'refs';
$meta->add_method($method_name, $method_body);
}
}
- if (defined $roles){
+ if (defined $roles and !$options{in_application_to_instance}){
Mouse::Util::apply_all_roles($package_name, @{$roles});
}
$consumer = (Mouse::Util::class_of($instance) || 'Mouse::Meta::Class')
->create_anon_class(
superclasses => [ref $instance],
+ roles => [$role],
cache => 1,
+
+ in_application_to_instance => 1, # suppress to apply roles
);
}
-#!/usr/bin/env perl
+#!perl
use strict;
use warnings;
-use Test::More tests => 4;
+use Test::More;
+my $foo = 0;
+my $bar = 0;
{
package FooRole;
use Mouse::Role;
sub foo { 'ok1' }
+
+ before method => sub { $foo++ };
}
{
package BarRole;
use Mouse::Role;
sub bar { 'ok2' }
+
+ before method => sub { $bar++ };
}
{
package Baz;
use Mouse;
+ sub method {}
no Mouse;
}
-eval { Mouse::Util::apply_all_roles('Baz', 'BarRole', 'FooRole') };
-ok !$@;
+{
+ package Qux;
+ use Mouse;
+ sub method {}
+ no Mouse;
+}
-Mouse::Util::apply_all_roles('Baz', 'BarRole');
-Mouse::Util::apply_all_roles('Baz', 'FooRole');
+Mouse::Util::apply_all_roles('Baz', 'BarRole', 'FooRole');
my $baz = Baz->new;
is $baz->foo, 'ok1';
is $baz->bar, 'ok2';
-is join(",", sort $baz->meta->get_method_list), 'bar,foo,meta';
+is join(",", sort $baz->meta->get_method_list), 'bar,foo,meta,method';
+
+# applyu to instance
+
+my $qux = Qux->new;
+Mouse::Util::apply_all_roles($qux, 'FooRole');
+note $qux;
+$foo = 0;
+$bar = 0;
+$qux->method;
+is $foo, 1;
+is $bar, 0;
+
+$qux = Qux->new;
+Mouse::Util::apply_all_roles($qux, 'BarRole');
+note $qux;
+$foo = 0;
+$bar = 0;
+$qux->method;
+is $foo, 0;
+is $bar, 1;
+
+$qux = Qux->new;
+Mouse::Util::apply_all_roles($qux, 'FooRole', 'BarRole');
+note $qux;
+$foo = 0;
+$bar = 0;
+$qux->method;
+is $foo, 1;
+is $bar, 1;
+
+done_testing;