Commit | Line | Data |
---|---|---|
fbfaa61f | 1 | package MooseX::StrictConstructor::Role::Meta::Method::Constructor; |
c001451a | 2 | |
3 | use strict; | |
4 | use warnings; | |
5 | ||
79b37c7d | 6 | use B (); |
5c3f24ed | 7 | use Carp (); |
c001451a | 8 | |
64c958ef | 9 | use Moose::Role; |
c001451a | 10 | |
5a0d4921 | 11 | around '_generate_BUILDALL' => sub { |
64c958ef | 12 | my $orig = shift; |
c001451a | 13 | my $self = shift; |
14 | ||
64c958ef | 15 | my $source = $self->$orig(); |
5c3f24ed | 16 | $source .= ";\n" if $source; |
c001451a | 17 | |
5a0d4921 | 18 | my @attrs = ( |
79b37c7d | 19 | "__INSTANCE__ => 1,", |
20 | map { B::perlstring($_) . ' => 1,' } | |
5a0d4921 | 21 | grep {defined} |
22 | map { $_->init_arg() } @{ $self->_attributes() } | |
23 | ); | |
c001451a | 24 | |
5c3f24ed | 25 | $source .= <<"EOF"; |
26 | my \%attrs = (@attrs); | |
c001451a | 27 | |
5c40fb22 | 28 | my \@bad = sort grep { ! \$attrs{\$_} } keys \%{ \$params }; |
5c3f24ed | 29 | |
30 | if (\@bad) { | |
31 | Carp::confess "Found unknown attribute(s) passed to the constructor: \@bad"; | |
32 | } | |
c001451a | 33 | EOF |
34 | ||
5c3f24ed | 35 | return $source; |
c001451a | 36 | }; |
37 | ||
64c958ef | 38 | no Moose::Role; |
c001451a | 39 | |
40 | 1; | |
58370717 | 41 | |
0639c294 | 42 | # ABSTRACT: A role to make immutable constructors strict |
43 | ||
58370717 | 44 | __END__ |
45 | ||
46 | =pod | |
47 | ||
58370717 | 48 | =head1 SYNOPSIS |
49 | ||
48ad7621 | 50 | Moose::Util::MetaRole::apply_metaroles( |
51 | for_class => $caller, | |
52 | class => { | |
53 | constructor => | |
54 | ['MooseX::StrictConstructor::Role::Meta::Method::Constructor'], | |
55 | }, | |
56 | ); | |
58370717 | 57 | |
58 | =head1 DESCRIPTION | |
59 | ||
fbfaa61f | 60 | This role simply wraps C<_generate_BUILDALL()> (from |
61 | C<Moose::Meta::Method::Constructor>) so that immutable classes have a | |
62 | strict constructor. | |
58370717 | 63 | |
58370717 | 64 | =cut |
65 |