Ignore __INSTANCE__ as constructor arg
[gitmo/MooseX-StrictConstructor.git] / lib / MooseX / StrictConstructor / Role / Meta / Method / Constructor.pm
1 package MooseX::StrictConstructor::Role::Meta::Method::Constructor;
2
3 use strict;
4 use warnings;
5
6 use Carp ();
7
8 use Moose::Role;
9
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  {"$_ => 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
37 no Moose::Role;
38
39 1;
40
41 # ABSTRACT: A role to make immutable constructors strict
42
43 __END__
44
45 =pod
46
47 =head1 SYNOPSIS
48
49   Moose::Util::MetaRole::apply_metaclass_roles
50       ( for_class => $caller,
51         constructor_class_roles =>
52         ['MooseX::StrictConstructor::Role::Meta::Method::Constructor'],
53       );
54
55 =head1 DESCRIPTION
56
57 This role simply wraps C<_generate_BUILDALL()> (from
58 C<Moose::Meta::Method::Constructor>) so that immutable classes have a
59 strict constructor.
60
61 =cut
62