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