respect init_arg in immutable classes
[gitmo/MooseX-StrictConstructor.git] / lib / MooseX / StrictConstructor / Meta / Method / Constructor.pm
1 package MooseX::StrictConstructor::Meta::Method::Constructor;
2
3 use strict;
4 use warnings;
5
6 use Carp ();
7 use Moose;
8
9 extends 'Moose::Meta::Method::Constructor';
10
11 override '_generate_BUILDALL' => sub ## no critic RequireArgUnpacking
12 {
13     my $self = shift;
14
15     my $source = super();
16     $source .= ";\n" if $source;
17
18     my @attrs =
19         ( map { "$_ => 1," }
20           grep { defined }
21           map { $_->init_arg() }
22           @{ $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;
39
40
41 1;
42
43 __END__
44
45 =pod
46
47 =head1 NAME
48
49 MooseX::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
57 This class simply overrides C<_generate_BUILDALL()> in
58 C<Moose::Meta::Method::Constructor> so that classes that are made
59 immutable have a strict constructor.
60
61 You should never have to use this class directly.
62
63 =head1 AUTHOR
64
65 Dave Rolsky, C<< <autarch@urth.org> >>
66
67 =head1 COPYRIGHT & LICENSE
68
69 Copyright 2007 Dave Rolsky, All Rights Reserved.
70
71 This program is free software; you can redistribute it and/or modify
72 it under the same terms as Perl itself.
73
74 =cut
75