From: Dave Rolsky <autarch@urth.org>
Date: Thu, 15 Nov 2007 21:00:52 +0000 (+0000)
Subject: Really make the strict constructor work after immutabilization.
X-Git-Tag: 0.02~1
X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=5c3f24ed7e5aa011dd3d356bf9fcb076fd970a75;p=gitmo%2FMooseX-StrictConstructor.git

Really make the strict constructor work after immutabilization.

Also, add an optimization to the immutable version. Pre-compute the
set of applicable attributes once when generating the constructor,
rather than on every call to new().
---

diff --git a/lib/MooseX/StrictConstructor/Meta/Method/Constructor.pm b/lib/MooseX/StrictConstructor/Meta/Method/Constructor.pm
index 7d2017c..f72fa66 100644
--- a/lib/MooseX/StrictConstructor/Meta/Method/Constructor.pm
+++ b/lib/MooseX/StrictConstructor/Meta/Method/Constructor.pm
@@ -3,28 +3,32 @@ package MooseX::StrictConstructor::Meta::Method::Constructor;
 use strict;
 use warnings;
 
+use Carp ();
 use Moose;
 
 extends 'Moose::Meta::Method::Constructor';
 
+# using 
 sub _generate_BUILDALL ## no critic RequireArgUnpacking
 {
     my $self = shift;
 
-    my $calls = $self->SUPER::_generate_BUILDALL(@_);
+    my $source = $self->SUPER::_generate_BUILDALL(@_);
+    $source .= ";\n" if $source;
 
-    $calls .= <<'EOF';
-    my %attrs = map { $_->name() => 1 } $self->meta()->compute_all_applicable_attributes();
+    my @attrs = map { $_->name() . ' => 1,' } @{ $self->attributes() };
 
-    my @bad = sort grep { ! $attrs{$_} }  keys %params;
+    $source .= <<"EOF";
+my \%attrs = (@attrs);
 
-    if (@bad)
-    {
-        confess "Found unknown attribute(s) passed to the constructor: @bad";
-    }
+my \@bad = sort grep { ! \$attrs{\$_} }  keys \%params;
+
+if (\@bad) {
+    Carp::confess "Found unknown attribute(s) passed to the constructor: \@bad";
+}
 EOF
 
-    return $calls;
+    return $source;
 };
 
 
diff --git a/t/basic.t b/t/basic.t
index cb0fec7..9c26f8d 100644
--- a/t/basic.t
+++ b/t/basic.t
@@ -71,6 +71,9 @@ use Test::More tests => 9;
 
         delete $params->{spy};
     }
+
+    no Moose;
+    __PACKAGE__->meta()->make_immutable();
 }