From: Ricardo Signes Date: Wed, 18 May 2011 18:14:20 +0000 (-0400) Subject: MXSC must be used by Moose classes, nothing else X-Git-Tag: v0.17~11 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=4a070866bc7b2cf1792322865e0c7997ef7401a3;p=gitmo%2FMooseX-StrictConstructor.git MXSC must be used by Moose classes, nothing else --- diff --git a/Changes b/Changes index 9590625..985bf3e 100644 --- a/Changes +++ b/Changes @@ -1,5 +1,7 @@ {{$NEXT}} +- Throw an error when used by a non-class; it won't do what users think + 0.16 2011-04-22 - Don't wrap BUILDALL, since this breaks if the object doesn't inherit from diff --git a/lib/MooseX/StrictConstructor.pm b/lib/MooseX/StrictConstructor.pm index 40fa4bb..80e8453 100644 --- a/lib/MooseX/StrictConstructor.pm +++ b/lib/MooseX/StrictConstructor.pm @@ -20,6 +20,16 @@ use Moose::Util::MetaRole; Moose::Exporter->setup_import_methods( class_metaroles => \%class_meta, ); + + my $old_import = __PACKAGE__->can('import'); + no warnings 'redefine'; + *import = sub { + my $caller = caller; + Carp::croak "$_[0] can only be applied to Moose classes" + unless eval { $caller->meta->isa('Moose::Meta::Class') }; + + goto &$old_import; + }; } 1; diff --git a/t/basic.t b/t/basic.t index f69201f..b159e77 100644 --- a/t/basic.t +++ b/t/basic.t @@ -80,6 +80,26 @@ use Test::More; has 'size' => ( is => 'rw', 'init_arg' => undef ); } +{ + local $@; + eval q[package MyRole; use Moose::Role; use MooseX::StrictConstructor;]; + like( + $@, + qr/can only be applied to Moose classes/, + "can't apply MXSC to a role" + ); +} + +{ + local $@; + eval q[package Nothing; use MooseX::StrictConstructor;]; + like( + $@, + qr/can only be applied to Moose classes/, + "can't apply MXSC to a random package", + ); +} + my @classes = qw( Standard Stricter Subclass StrictSubclass OtherStrictSubclass Tricky InitArg );