modernize Moose usage
[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 use Carp ();
9
10 around '_generate_BUILDALL' => sub {
11     my $orig = shift;
12     my $self = shift;
13
14     my $source = $self->$orig();
15     $source .= ";\n" if $source;
16
17     my @attrs = (
18         '__INSTANCE__ => 1,',
19         map { B::perlstring($_) . ' => 1,' }
20         grep {defined}
21         map  { $_->init_arg() } @{ $self->_attributes() }
22     );
23
24     $source .= <<"EOF";
25 my \%attrs = (@attrs);
26
27 my \@bad = sort grep { ! \$attrs{\$_} }  keys \%{ \$params };
28
29 if (\@bad) {
30     Carp::confess "Found unknown attribute(s) passed to the constructor: \@bad";
31 }
32 EOF
33
34     return $source;
35 };
36
37 1;
38
39 # ABSTRACT: A role to make immutable constructors strict
40
41 __END__
42
43 =pod
44
45 =head1 DESCRIPTION
46
47 This role simply wraps C<_generate_BUILDALL()> (from
48 C<Moose::Meta::Method::Constructor>) so that immutable classes have a
49 strict constructor.
50
51 =cut
52