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