Commit | Line | Data |
1a4f7732 |
1 | package MooseX::StrictConstructor::Trait::Method::Constructor; |
c8a39fc4 |
2 | |
d99e6f32 |
3 | use Moose::Role; |
4 | |
5 | use namespace::autoclean; |
c8a39fc4 |
6 | |
7 | use B (); |
8 | use Carp (); |
9 | |
c8a39fc4 |
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 | |
c8a39fc4 |
37 | 1; |
38 | |
39 | # ABSTRACT: A role to make immutable constructors strict |
40 | |
41 | __END__ |
42 | |
43 | =pod |
44 | |
c8a39fc4 |
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 | |