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 (); |
c8a39fc4 |
8 | |
c8a39fc4 |
9 | around '_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 | |
4e914932 |
23 | my $MY = 'my'; |
24 | if ($] >= 5.009004) { |
25 | $source .= "use feature 'state';\n"; |
26 | $MY = 'state'; |
27 | } |
c8a39fc4 |
28 | |
4e914932 |
29 | $source .= <<"EOF"; |
30 | $MY \$attrs = { @attrs }; |
31 | if (my \@bad = sort grep { ! \$attrs->{\$_} } keys %\$params) { |
714128ef |
32 | Moose->throw_error("Found unknown attribute(s) passed to the constructor: \@bad"); |
c8a39fc4 |
33 | } |
34 | EOF |
35 | |
36 | return $source; |
37 | }; |
38 | |
c8a39fc4 |
39 | 1; |
40 | |
41 | # ABSTRACT: A role to make immutable constructors strict |
42 | |
43 | __END__ |
44 | |
45 | =pod |
46 | |
c8a39fc4 |
47 | =head1 DESCRIPTION |
48 | |
49 | This role simply wraps C<_generate_BUILDALL()> (from |
50 | C<Moose::Meta::Method::Constructor>) so that immutable classes have a |
51 | strict constructor. |
52 | |
53 | =cut |
54 | |