modernize Moose usage
[gitmo/MooseX-StrictConstructor.git] / lib / MooseX / StrictConstructor / Trait / Method / Constructor.pm
CommitLineData
1a4f7732 1package MooseX::StrictConstructor::Trait::Method::Constructor;
c8a39fc4 2
d99e6f32 3use Moose::Role;
4
5use namespace::autoclean;
c8a39fc4 6
7use B ();
8use Carp ();
9
c8a39fc4 10around '_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";
25my \%attrs = (@attrs);
26
27my \@bad = sort grep { ! \$attrs{\$_} } keys \%{ \$params };
28
29if (\@bad) {
30 Carp::confess "Found unknown attribute(s) passed to the constructor: \@bad";
31}
32EOF
33
34 return $source;
35};
36
c8a39fc4 371;
38
39# ABSTRACT: A role to make immutable constructors strict
40
41__END__
42
43=pod
44
c8a39fc4 45=head1 DESCRIPTION
46
47This role simply wraps C<_generate_BUILDALL()> (from
48C<Moose::Meta::Method::Constructor>) so that immutable classes have a
49strict constructor.
50
51=cut
52