Make the meta classes more Moose-y.
[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 = $self->SUPER::_generate_BUILDALL(@_);
16     $source .= ";\n" if $source;
17
18     my @attrs = map { $_->name() . ' => 1,' } @{ $self->attributes() };
19
20     $source .= <<"EOF";
21 my \%attrs = (@attrs);
22
23 my \@bad = sort grep { ! \$attrs{\$_} }  keys \%params;
24
25 if (\@bad) {
26     Carp::confess "Found unknown attribute(s) passed to the constructor: \@bad";
27 }
28 EOF
29
30     return $source;
31 };
32
33 no Moose;
34
35
36 1;
37
38 __END__
39
40 =pod
41
42 =head1 NAME
43
44 MooseX::StrictConstructor::Meta::Method::Constructor - A meta class to make immutable constructors strict
45
46 =head1 SYNOPSIS
47
48   use MooseX::StrictConstructor;
49
50 =head1 DESCRIPTION
51
52 This class simply overrides C<_generate_BUILDALL()> in
53 C<Moose::Meta::Method::Constructor> so that classes that are made
54 immutable have a strict constructor.
55
56 You should never have to use this class directly.
57
58 =head1 AUTHOR
59
60 Dave Rolsky, C<< <autarch@urth.org> >>
61
62 =head1 COPYRIGHT & LICENSE
63
64 Copyright 2007 Dave Rolsky, All Rights Reserved.
65
66 This program is free software; you can redistribute it and/or modify
67 it under the same terms as Perl itself.
68
69 =cut
70