From: Chris Prather Date: Sat, 21 Aug 2010 07:09:07 +0000 (-0400) Subject: initialize a metaclass when trying to apply all roles X-Git-Tag: 2.0103~24 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=eea9eb4d0a9d6d7453cfb6fca6bb6aae618254c4;p=gitmo%2FMoose.git initialize a metaclass when trying to apply all roles Applying a role to a non-Moose class currently requires manually initializing the metaclass before calling Moose::Util::apply_all_roles(). This commit changes that by swapping find_meta for initialize() when setting up $applicant. --- diff --git a/lib/Moose/Util.pm b/lib/Moose/Util.pm index 469e281..c62701d 100644 --- a/lib/Moose/Util.pm +++ b/lib/Moose/Util.pm @@ -118,7 +118,7 @@ sub _apply_all_roles { } else { Class::MOP::load_class( $role->[0] , $role->[1] ); - $meta = Class::MOP::class_of( $role->[0] ); + $meta = find_meta( $role->[0] ); } unless ($meta && $meta->isa('Moose::Meta::Role') ) { @@ -137,7 +137,7 @@ sub _apply_all_roles { return unless @role_metas; - my $meta = ( blessed $applicant ? $applicant : find_meta($applicant) ); + my $meta = ( blessed $applicant ? $applicant : Moose::Meta::Class->initialize($applicant) ); if ( scalar @role_metas == 1 ) { my ( $role, $params ) = @{ $role_metas[0] }; diff --git a/t/roles/apply_role.t b/t/roles/apply_role.t index 1c2c38e..2f8d07f 100644 --- a/t/roles/apply_role.t +++ b/t/roles/apply_role.t @@ -63,6 +63,11 @@ use Test::Fatal; with 'FooRole', 'BarRole'; } +{ + package PlainJane; + sub new { return bless {}, __PACKAGE__; } +} + my $foo_class_meta = FooClass->meta; isa_ok( $foo_class_meta, 'Moose::Meta::Class' ); @@ -212,4 +217,13 @@ foreach my $foo ( $foo, $foobar ) { } } +{ + ok(!Moose::Util::find_meta('PlainJane'), 'not initialized'); + Moose::Util::apply_all_roles('PlainJane', 'BarRole'); + ok(Moose::Util::find_meta('PlainJane'), 'initialized'); + ok(Moose::Util::find_meta('PlainJane')->does_role('BarRole'), 'does BarRole'); + my $pj = PlainJane->new(); + ok($pj->can('woot'), 'can woot'); +} + done_testing;