comments specifying how these traits are applied, just to keep it straight
[gitmo/MooseX-StrictConstructor.git] / lib / MooseX / StrictConstructor / Trait / Method / Constructor.pm
CommitLineData
1a4f7732 1package MooseX::StrictConstructor::Trait::Method::Constructor;
c8a39fc4 2
d6932ec6 3# applied as class_metaroles => { constructor => [ __PACKAGE__ ] }, for older Moose
4
d99e6f32 5use Moose::Role;
6
7use namespace::autoclean;
c8a39fc4 8
9use B ();
c8a39fc4 10
c8a39fc4 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) {
675cf837 31 Moose->throw_error("Found unknown attribute(s) passed to the constructor: \@bad");
c8a39fc4 32}
33EOF
34
35 return $source;
36};
37
c8a39fc4 381;
39
40# ABSTRACT: A role to make immutable constructors strict
41
42__END__
43
44=pod
45
c8a39fc4 46=head1 DESCRIPTION
47
48This role simply wraps C<_generate_BUILDALL()> (from
49C<Moose::Meta::Method::Constructor>) so that immutable classes have a
50strict constructor.
51
52=cut
53