From: Jesse Luehrs Date: Wed, 12 Aug 2009 06:00:03 +0000 (-0500) Subject: allow role options in the -traits parameter to use Moose X-Git-Tag: 0.89~5 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=386c056b4c3e1fc98fca3eca3c58b2dc15214237;p=gitmo%2FMoose.git allow role options in the -traits parameter to use Moose --- diff --git a/Changes b/Changes index ace1953..8c16840 100644 --- a/Changes +++ b/Changes @@ -17,6 +17,8 @@ next version * Moose::Exporter - Make "use Moose -metaclass => 'Foo'" do alias resolution, like -traits does. (doy) + - Allow specifying role options (alias, excludes, MXRP stuff) in the + arrayref passed to "use Moose -traits" (doy) * Moose::Util - Add functions meta_class_alias and meta_attribute_alias for creating diff --git a/lib/Moose/Exporter.pm b/lib/Moose/Exporter.pm index 6ca7ca6..2f619c7 100644 --- a/lib/Moose/Exporter.pm +++ b/lib/Moose/Exporter.pm @@ -464,7 +464,9 @@ sub _apply_meta_traits { . ref $meta ); my @resolved_traits - = map { Moose::Util::resolve_metatrait_alias( $type => $_ ) } + = map { + ref $_ ? $_ : Moose::Util::resolve_metatrait_alias( $type => $_ ) + } @$traits; return unless @resolved_traits; diff --git a/lib/Moose/Meta/Class.pm b/lib/Moose/Meta/Class.pm index 7cf3ca5..eac228c 100644 --- a/lib/Moose/Meta/Class.pm +++ b/lib/Moose/Meta/Class.pm @@ -673,7 +673,7 @@ These all default to the appropriate Moose class. This overrides the parent's method in order to accept a C option. This should be an array reference containing one more roles -that the class does. +that the class does, each optionally followed by a hashref of options. my $metaclass = Moose::Meta::Class->create( 'New::Class', roles => [...] ); diff --git a/lib/Moose/Util/MetaRole.pm b/lib/Moose/Util/MetaRole.pm index 8294197..636b949 100644 --- a/lib/Moose/Util/MetaRole.pm +++ b/lib/Moose/Util/MetaRole.pm @@ -105,7 +105,8 @@ sub _make_new_class { my $meta = Class::MOP::Class->initialize($existing_class); return $existing_class - if $meta->can('does_role') && all { $meta->does_role($_) } @{$roles}; + if $meta->can('does_role') && all { $meta->does_role($_) } + grep { !ref $_ } @{$roles}; return Moose::Meta::Class->create_anon_class( superclasses => $superclasses, diff --git a/t/020_attributes/024_attribute_traits_parameterized.t b/t/020_attributes/024_attribute_traits_parameterized.t index 2561b34..86cc961 100644 --- a/t/020_attributes/024_attribute_traits_parameterized.t +++ b/t/020_attributes/024_attribute_traits_parameterized.t @@ -1,7 +1,7 @@ #!/usr/bin/env perl use strict; use warnings; -use Test::More tests => 4; +use Test::More tests => 5; { package My::Attribute::Trait; @@ -39,6 +39,7 @@ use Test::More tests => 4; alias => { reversed_name => 'reversed', }, + excludes => 'reversed_name', }, ], is => 'bare', @@ -52,4 +53,5 @@ ok(!$attr->can('reversed'), "the method was not installed under the other class' my $other_attr = My::Other::Class->meta->get_attribute('foo'); is($other_attr->reversed, 'oof', 'the aliased method is in the attribute'); ok(!$other_attr->can('enam'), "the method was not installed under the other class' alias"); +ok(!$other_attr->can('reversed_name'), "the method was not installed under the original name when that was excluded"); diff --git a/t/050_metaclasses/020_metaclass_parameterized_traits.t b/t/050_metaclasses/020_metaclass_parameterized_traits.t index cd6e5ad..776be95 100644 --- a/t/050_metaclasses/020_metaclass_parameterized_traits.t +++ b/t/050_metaclasses/020_metaclass_parameterized_traits.t @@ -1,8 +1,7 @@ #!/usr/bin/env perl use strict; use warnings; -use Test::More skip_all => "Feature not implemented yet"; -#use Test::More tests => 1; +use Test::More tests => 5; { package My::Trait; @@ -25,5 +24,24 @@ use Test::More skip_all => "Feature not implemented yet"; ]; } -is(My::Class->meta->enam, 'ssalC::yM', 'parameterized trait applied'); +{ + package My::Other::Class; + use Moose -traits => [ + 'My::Trait' => { + alias => { + reversed_name => 'reversed', + }, + excludes => 'reversed_name', + }, + ]; +} + +my $meta = My::Class->meta; +is($meta->enam, 'ssalC::yM', 'parameterized trait applied'); +ok(!$meta->can('reversed'), "the method was not installed under the other class' alias"); + +my $other_meta = My::Other::Class->meta; +is($other_meta->reversed, 'ssalC::rehtO::yM', 'parameterized trait applied'); +ok(!$other_meta->can('enam'), "the method was not installed under the other class' alias"); +ok(!$other_meta->can('reversed_name'), "the method was not installed under the original name when that was excluded");