Commit | Line | Data |
c8a39fc4 |
1 | package MooseX::StrictConstructor::Role::Meta::Method::Constructor; |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
6 | use B (); |
7 | use Carp (); |
8 | |
9 | use Moose::Role; |
10 | |
11 | around '_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"; |
26 | my \%attrs = (@attrs); |
27 | |
28 | my \@bad = sort grep { ! \$attrs{\$_} } keys \%{ \$params }; |
29 | |
30 | if (\@bad) { |
31 | Carp::confess "Found unknown attribute(s) passed to the constructor: \@bad"; |
32 | } |
33 | EOF |
34 | |
35 | return $source; |
36 | }; |
37 | |
38 | no Moose::Role; |
39 | |
40 | 1; |
41 | |
42 | # ABSTRACT: A role to make immutable constructors strict |
43 | |
44 | __END__ |
45 | |
46 | =pod |
47 | |
48 | =head1 SYNOPSIS |
49 | |
50 | Moose::Util::MetaRole::apply_metaroles( |
51 | for_class => $caller, |
52 | class => { |
53 | constructor => |
54 | ['MooseX::StrictConstructor::Role::Meta::Method::Constructor'], |
55 | }, |
56 | ); |
57 | |
58 | =head1 DESCRIPTION |
59 | |
60 | This role simply wraps C<_generate_BUILDALL()> (from |
61 | C<Moose::Meta::Method::Constructor>) so that immutable classes have a |
62 | strict constructor. |
63 | |
64 | =cut |
65 | |