From: Dave Rolsky Date: Tue, 12 Aug 2008 16:26:49 +0000 (+0000) Subject: If traits are provided but the exporting class does not call X-Git-Tag: 0_55_01~40 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=f9dfa78b1df96c4d96ff76f4a19db7ceea69be1b;p=gitmo%2FMoose.git If traits are provided but the exporting class does not call init_meta, we throw an error, instead of just ignoring it. --- diff --git a/lib/Moose/Exporter.pm b/lib/Moose/Exporter.pm index eecb2f7..571c9b2 100644 --- a/lib/Moose/Exporter.pm +++ b/lib/Moose/Exporter.pm @@ -201,8 +201,13 @@ sub _make_sub_exporter_params { $did_init_meta = 1; } - _apply_meta_traits( $CALLER, $traits ) - if $did_init_meta; + if ($did_init_meta) { + _apply_meta_traits( $CALLER, $traits ); + } + elsif ( $traits && @{$traits} ) { + confess + "Cannot provide traits when $class does not have an init_meta() method"; + } goto $exporter; }; @@ -218,6 +223,8 @@ sub _strip_traits { splice @_, $idx, 2; + $traits = [ $traits ] unless ref $traits; + return ( $traits, @_ ); } @@ -409,6 +416,18 @@ Moose->init_meta >> to do the real work: return Moose->init_meta( @_, metaclass => 'My::Metaclass' ); } +=head1 METACLASS TRAITS + +The C method generated by C will allow the +user of your module to specify metaclass traits in a C<-traits> +parameter passed as part of the import: + + use Moose -traits => 'My::Meta::Trait'; + + use Moose -traits => [ 'My::Meta::Trait', 'My::Other::Trait' ]; + +These traits will be applied to the caller's metaclass instance. These traits will be ignored + =head1 AUTHOR Dave Rolsky Eautarch@urth.orgE diff --git a/t/050_metaclasses/013_metaclass_traits.t b/t/050_metaclasses/013_metaclass_traits.t index f6519f3..a74fd3c 100644 --- a/t/050_metaclasses/013_metaclass_traits.t +++ b/t/050_metaclasses/013_metaclass_traits.t @@ -3,7 +3,8 @@ use strict; use warnings; -use Test::More 'no_plan'; +use Test::More tests => 23; +use Test::Exception; { package My::SimpleTrait; @@ -24,6 +25,16 @@ is( Foo->meta()->simple(), 5, 'Foo->meta()->simple() returns expected value' ); { + package Bar; + + use Moose -traits => 'My::SimpleTrait'; +} + +can_ok( Bar->meta(), 'simple' ); +is( Bar->meta()->simple(), 5, + 'Foo->meta()->simple() returns expected value' ); + +{ package My::SimpleTrait2; use Moose::Role; @@ -151,3 +162,11 @@ ok( RanOutOfNames->meta()->meta()->has_method('whatever'), can_ok( Role::Foo->meta(), 'simple' ); is( Role::Foo->meta()->simple(), 5, 'Role::Foo->meta()->simple() returns expected value' ); + +{ + require Moose::Util::TypeConstraints; + dies_ok( sub { Moose::Util::TypeConstraints->import( -traits => 'My::SimpleTrait' ) }, + 'cannot provide -traits to an exporting module that does not init_meta' ); + like( $@, qr/does not have an init_meta/, + '... and error provides a useful explanation' ); +}