When inlining with Moose 2.0+, close over hash of allowed attrs rather than regenerat...
[gitmo/MooseX-StrictConstructor.git] / lib / MooseX / StrictConstructor / Trait / Method / Constructor.pm
index 8c36810..ae5d3fb 100644 (file)
@@ -1,12 +1,10 @@
 package MooseX::StrictConstructor::Trait::Method::Constructor;
 
-use strict;
-use warnings;
+use Moose::Role;
 
-use B ();
-use Carp ();
+use namespace::autoclean;
 
-use Moose::Role;
+use B ();
 
 around '_generate_BUILDALL' => sub {
     my $orig = shift;
@@ -28,14 +26,30 @@ my \%attrs = (@attrs);
 my \@bad = sort grep { ! \$attrs{\$_} }  keys \%{ \$params };
 
 if (\@bad) {
-    Carp::confess "Found unknown attribute(s) passed to the constructor: \@bad";
+    Moose->throw_error("Found unknown attribute(s) passed to the constructor: \@bad");
 }
 EOF
 
     return $source;
-};
+} if $Moose::VERSION < 1.9900;
+
+around _eval_environment => sub {
+    my $orig = shift;
+    my $self = shift;
+
+    my $env = $self->$orig();
+
+    my %attrs = map { $_ => 1 }
+        grep { defined }
+        map  { $_->init_arg() }
+        $self->associated_metaclass()->get_all_attributes();
+
+    $attrs{__INSTANCE__} = 1;
+
+    $env->{'%allowed_attrs'} = \%attrs;
 
-no Moose::Role;
+    return $env;
+} if $Moose::VERSION >= 1.9900;
 
 1;