From: Dave Rolsky Date: Wed, 4 Jan 2012 06:50:37 +0000 (-0600) Subject: When inlining with Moose 2.0+, close over hash of allowed attrs rather than regenerat... X-Git-Tag: v0.17~7 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=bb64c0458ed377f9a5a7f4d4071dee8cf0821312;p=gitmo%2FMooseX-StrictConstructor.git When inlining with Moose 2.0+, close over hash of allowed attrs rather than regenerating it each time. --- diff --git a/lib/MooseX/StrictConstructor.pm b/lib/MooseX/StrictConstructor.pm index 030230f..b45c3f0 100644 --- a/lib/MooseX/StrictConstructor.pm +++ b/lib/MooseX/StrictConstructor.pm @@ -7,19 +7,16 @@ use Moose 0.94 (); use Moose::Exporter; use Moose::Util::MetaRole; -{ - my %class_meta = ( class => ['MooseX::StrictConstructor::Trait::Class'] ); - - if ( $Moose::VERSION < 1.9900 ) { - require MooseX::StrictConstructor::Trait::Method::Constructor; - $class_meta{constructor} - = ['MooseX::StrictConstructor::Trait::Method::Constructor']; - } - - Moose::Exporter->setup_import_methods( - class_metaroles => \%class_meta, - ); -} +use MooseX::StrictConstructor::Trait::Class; +use MooseX::StrictConstructor::Trait::Method::Constructor;; + +Moose::Exporter->setup_import_methods( + class_metaroles => { + class => ['MooseX::StrictConstructor::Trait::Class'], + constructor => + ['MooseX::StrictConstructor::Trait::Method::Constructor'], + }, +); 1; diff --git a/lib/MooseX/StrictConstructor/Trait/Class.pm b/lib/MooseX/StrictConstructor/Trait/Class.pm index 707c75f..b96e5fd 100644 --- a/lib/MooseX/StrictConstructor/Trait/Class.pm +++ b/lib/MooseX/StrictConstructor/Trait/Class.pm @@ -47,8 +47,7 @@ around '_inline_BUILDALL' => sub { return ( @source, - 'my %attrs = (' . ( join ' ', @attrs ) . ');', - 'my @bad = sort grep { !$attrs{$_} } keys %{ $params };', + 'my @bad = sort grep { !$allowed_attrs{$_} } keys %{ $params };', 'if (@bad) {', 'Moose->throw_error("Found unknown attribute(s) passed to the constructor: @bad");', '}', diff --git a/lib/MooseX/StrictConstructor/Trait/Method/Constructor.pm b/lib/MooseX/StrictConstructor/Trait/Method/Constructor.pm index 0ceec12..ae5d3fb 100644 --- a/lib/MooseX/StrictConstructor/Trait/Method/Constructor.pm +++ b/lib/MooseX/StrictConstructor/Trait/Method/Constructor.pm @@ -31,7 +31,25 @@ if (\@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; + + return $env; +} if $Moose::VERSION >= 1.9900; 1;