From: Florian Ragwitz Date: Mon, 13 Jul 2009 15:30:47 +0000 (+0200) Subject: Allow MetaRole to apply roles to a meta role's role application classes. X-Git-Tag: 0.88~16 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=d401dc204aa460ead163768cd5b08e02f2667c72;p=gitmo%2FMoose.git Allow MetaRole to apply roles to a meta role's role application classes. --- diff --git a/lib/Moose/Util/MetaRole.pm b/lib/Moose/Util/MetaRole.pm index 3bc73f6..2e296fb 100644 --- a/lib/Moose/Util/MetaRole.pm +++ b/lib/Moose/Util/MetaRole.pm @@ -51,6 +51,9 @@ sub _make_new_metaclass { method_metaclass wrapped_method_metaclass instance_metaclass + application_to_class_class + application_to_role_class + application_to_instance_class ); my $old_meta = Class::MOP::class_of($for); @@ -67,6 +70,9 @@ sub _make_new_metaclass { method_metaclass wrapped_method_metaclass instance_metaclass + application_to_class_class + application_to_role_class + application_to_instance_class ); return $new_metaclass->reinitialize( $for, %classes ); @@ -203,6 +209,12 @@ This specifies the class for which to alter the meta classes. =item * destructor_class_roles => \@roles +=item * application_to_class_class_roles => \@roles + +=item * application_to_role_class_roles => \@roles + +=item * application_to_instance_class_roles => \@roles + These parameter all specify one or more roles to be applied to the specified metaclass. You can pass any or all of these parameters at once. diff --git a/t/050_metaclasses/015_metarole.t b/t/050_metaclasses/015_metarole.t index 365db0c..79833c3 100644 --- a/t/050_metaclasses/015_metarole.t +++ b/t/050_metaclasses/015_metarole.t @@ -5,7 +5,7 @@ use warnings; use lib 't/lib', 'lib'; -use Test::More tests => 80; +use Test::More tests => 89; use Test::Exception; use Moose::Util::MetaRole; @@ -30,6 +30,11 @@ use Moose::Util::MetaRole; } { + package My::Role; + use Moose::Role; +} + +{ Moose::Util::MetaRole::apply_metaclass_roles( for_class => 'My::Class', metaclass_roles => ['Role::Foo'], @@ -161,6 +166,51 @@ use Moose::Util::MetaRole; } { + Moose::Util::MetaRole::apply_metaclass_roles( + for_class => 'My::Role', + application_to_class_class_roles => ['Role::Foo'], + ); + + ok( My::Role->meta->application_to_class_class->meta->does_role('Role::Foo'), + q{apply Role::Foo to My::Role->meta's application_to_class class} ); + + is( My::Role->meta->application_to_class_class->new->foo, 10, + q{... call foo() on an application_to_class instance} ); +} + +{ + Moose::Util::MetaRole::apply_metaclass_roles( + for_class => 'My::Role', + application_to_role_class_roles => ['Role::Foo'], + ); + + ok( My::Role->meta->application_to_role_class->meta->does_role('Role::Foo'), + q{apply Role::Foo to My::Role->meta's application_to_role class} ); + ok( My::Role->meta->application_to_class_class->meta->does_role('Role::Foo'), + q{... My::Role->meta's application_to_class class still does Role::Foo} ); + + is( My::Role->meta->application_to_role_class->new->foo, 10, + q{... call foo() on an application_to_role instance} ); +} + +{ + Moose::Util::MetaRole::apply_metaclass_roles( + for_class => 'My::Role', + application_to_instance_class_roles => ['Role::Foo'], + ); + + ok( My::Role->meta->application_to_instance_class->meta->does_role('Role::Foo'), + q{apply Role::Foo to My::Role->meta's application_to_instance class} ); + ok( My::Role->meta->application_to_role_class->meta->does_role('Role::Foo'), + q{... My::Role->meta's application_to_role class still does Role::Foo} ); + ok( My::Role->meta->application_to_class_class->meta->does_role('Role::Foo'), + q{... My::Role->meta's application_to_class class still does Role::Foo} ); + + is( My::Role->meta->application_to_instance_class->new->foo, 10, + q{... call foo() on an application_to_instance instance} ); +} + +{ Moose::Util::MetaRole::apply_base_class_roles( for_class => 'My::Class', roles => ['Role::Foo'],