Revert "MXSC must be used by Moose classes, nothing else"
[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 ();
c8a39fc4 8
c8a39fc4 9around '_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 = (
17 '__INSTANCE__ => 1,',
18 map { B::perlstring($_) . ' => 1,' }
19 grep {defined}
20 map { $_->init_arg() } @{ $self->_attributes() }
21 );
22
23 $source .= <<"EOF";
24my \%attrs = (@attrs);
25
26my \@bad = sort grep { ! \$attrs{\$_} } keys \%{ \$params };
27
28if (\@bad) {
714128ef 29 Moose->throw_error("Found unknown attribute(s) passed to the constructor: \@bad");
c8a39fc4 30}
31EOF
32
33 return $source;
34};
35
c8a39fc4 361;
37
38# ABSTRACT: A role to make immutable constructors strict
39
40__END__
41
42=pod
43
c8a39fc4 44=head1 DESCRIPTION
45
46This role simply wraps C<_generate_BUILDALL()> (from
47C<Moose::Meta::Method::Constructor>) so that immutable classes have a
48strict constructor.
49
50=cut
51