Update Changes
[gitmo/MooseX-StrictConstructor.git] / lib / MooseX / StrictConstructor / Role / Meta / Method / Constructor.pm
CommitLineData
fbfaa61f 1package MooseX::StrictConstructor::Role::Meta::Method::Constructor;
c001451a 2
3use strict;
4use warnings;
5
5c3f24ed 6use Carp ();
c001451a 7
64c958ef 8use Moose::Role;
c001451a 9
5a0d4921 10around '_generate_BUILDALL' => sub {
64c958ef 11 my $orig = shift;
c001451a 12 my $self = shift;
13
64c958ef 14 my $source = $self->$orig();
5c3f24ed 15 $source .= ";\n" if $source;
c001451a 16
5a0d4921 17 my @attrs = (
18 map {"$_ => 1,"}
19 grep {defined}
20 map { $_->init_arg() } @{ $self->_attributes() }
21 );
c001451a 22
5c3f24ed 23 $source .= <<"EOF";
24my \%attrs = (@attrs);
c001451a 25
5c40fb22 26my \@bad = sort grep { ! \$attrs{\$_} } keys \%{ \$params };
5c3f24ed 27
28if (\@bad) {
29 Carp::confess "Found unknown attribute(s) passed to the constructor: \@bad";
30}
c001451a 31EOF
32
5c3f24ed 33 return $source;
c001451a 34};
35
64c958ef 36no Moose::Role;
c001451a 37
381;
58370717 39
40__END__
41
42=pod
43
44=head1 NAME
45
fbfaa61f 46MooseX::StrictConstructor::Role::Meta::Method::Constructor - A role to make immutable constructors strict
58370717 47
48=head1 SYNOPSIS
49
f83de654 50 Moose::Util::MetaRole::apply_metaclass_roles
51 ( for_class => $caller,
52 constructor_class_roles =>
53 ['MooseX::StrictConstructor::Role::Meta::Method::Constructor'],
54 );
58370717 55
56=head1 DESCRIPTION
57
fbfaa61f 58This role simply wraps C<_generate_BUILDALL()> (from
59C<Moose::Meta::Method::Constructor>) so that immutable classes have a
60strict constructor.
58370717 61
62=head1 AUTHOR
63
64Dave Rolsky, C<< <autarch@urth.org> >>
65
66=head1 COPYRIGHT & LICENSE
67
fbfaa61f 68Copyright 2007-2008 Dave Rolsky, All Rights Reserved.
58370717 69
70This program is free software; you can redistribute it and/or modify
71it under the same terms as Perl itself.
72
73=cut
74