Move the eval_environment wrapper to the Class trait from Constructor
[gitmo/MooseX-StrictConstructor.git] / lib / MooseX / StrictConstructor / Trait / Method / Constructor.pm
1 package MooseX::StrictConstructor::Trait::Method::Constructor;
2
3 use Moose::Role;
4
5 use namespace::autoclean;
6
7 use B ();
8
9 around _generate_BUILDALL => sub {
10     my $orig = shift;
11     my $self = shift;
12
13     my $source = $self->$orig();
14     $source .= ";\n" if $source;
15
16     my @attrs = '__INSTANCE__ => 1,';
17     push @attrs, map { B::perlstring($_) . ' => 1,' }
18         grep { defined }
19         map  { $_->init_arg() } @{ $self->_attributes() };
20
21     $source .= <<"EOF";
22 my \%attrs = (@attrs);
23
24 my \@bad = sort grep { ! \$attrs{\$_} }  keys \%{ \$params };
25
26 if (\@bad) {
27     Moose->throw_error("Found unknown attribute(s) passed to the constructor: \@bad");
28 }
29 EOF
30
31     return $source;
32 } if $Moose::VERSION < 1.9900;
33
34 1;
35
36 # ABSTRACT: A role to make immutable constructors strict
37
38 __END__
39
40 =pod
41
42 =head1 DESCRIPTION
43
44 This role simply wraps C<_generate_BUILDALL()> (from
45 C<Moose::Meta::Method::Constructor>) so that immutable classes have a
46 strict constructor.
47
48 =cut
49