$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;
};
splice @_, $idx, 2;
+ $traits = [ $traits ] unless ref $traits;
+
return ( $traits, @_ );
}
return Moose->init_meta( @_, metaclass => 'My::Metaclass' );
}
+=head1 METACLASS TRAITS
+
+The C<import> method generated by C<Moose::Exporter> 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 E<lt>autarch@urth.orgE<gt>
use strict;
use warnings;
-use Test::More 'no_plan';
+use Test::More tests => 23;
+use Test::Exception;
{
package My::SimpleTrait;
'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;
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' );
+}