respect init_arg in immutable classes
[gitmo/MooseX-StrictConstructor.git] / lib / MooseX / StrictConstructor / Meta / Method / Constructor.pm
CommitLineData
c001451a 1package MooseX::StrictConstructor::Meta::Method::Constructor;
2
3use strict;
4use warnings;
5
5c3f24ed 6use Carp ();
c001451a 7use Moose;
8
9extends 'Moose::Meta::Method::Constructor';
10
58370717 11override '_generate_BUILDALL' => sub ## no critic RequireArgUnpacking
c001451a 12{
13 my $self = shift;
14
02f0d848 15 my $source = super();
5c3f24ed 16 $source .= ";\n" if $source;
c001451a 17
0f795b43 18 my @attrs =
19 ( map { "$_ => 1," }
20 grep { defined }
21 map { $_->init_arg() }
22 @{ $self->attributes() }
23 );
c001451a 24
5c3f24ed 25 $source .= <<"EOF";
26my \%attrs = (@attrs);
c001451a 27
5c3f24ed 28my \@bad = sort grep { ! \$attrs{\$_} } keys \%params;
29
30if (\@bad) {
31 Carp::confess "Found unknown attribute(s) passed to the constructor: \@bad";
32}
c001451a 33EOF
34
5c3f24ed 35 return $source;
c001451a 36};
37
58370717 38no Moose;
39
c001451a 40
411;
58370717 42
43__END__
44
45=pod
46
47=head1 NAME
48
49MooseX::StrictConstructor::Meta::Method::Constructor - A meta class to make immutable constructors strict
50
51=head1 SYNOPSIS
52
53 use MooseX::StrictConstructor;
54
55=head1 DESCRIPTION
56
57This class simply overrides C<_generate_BUILDALL()> in
58C<Moose::Meta::Method::Constructor> so that classes that are made
59immutable have a strict constructor.
60
61You should never have to use this class directly.
62
63=head1 AUTHOR
64
65Dave Rolsky, C<< <autarch@urth.org> >>
66
67=head1 COPYRIGHT & LICENSE
68
69Copyright 2007 Dave Rolsky, All Rights Reserved.
70
71This program is free software; you can redistribute it and/or modify
72it under the same terms as Perl itself.
73
74=cut
75