dzilized all modules
[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         map  {"$_ => 1,"}
19         grep {defined}
20         map  { $_->init_arg() } @{ $self->_attributes() }
21     );
22
23     $source .= <<"EOF";
24 my \%attrs = (@attrs);
25
26 my \@bad = sort grep { ! \$attrs{\$_} }  keys \%{ \$params };
27
28 if (\@bad) {
29     Carp::confess "Found unknown attribute(s) passed to the constructor: \@bad";
30 }
31 EOF
32
33     return $source;
34 };
35
36 no Moose::Role;
37
38 1;
39
40 # ABSTRACT: A role to make immutable constructors strict
41
42 __END__
43
44 =pod
45
46 =head1 SYNOPSIS
47
48   Moose::Util::MetaRole::apply_metaclass_roles
49       ( for_class => $caller,
50         constructor_class_roles =>
51         ['MooseX::StrictConstructor::Role::Meta::Method::Constructor'],
52       );
53
54 =head1 DESCRIPTION
55
56 This role simply wraps C<_generate_BUILDALL()> (from
57 C<Moose::Meta::Method::Constructor>) so that immutable classes have a
58 strict constructor.
59
60 =cut
61