dzilized all modules
[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
0639c294 40# ABSTRACT: A role to make immutable constructors strict
41
58370717 42__END__
43
44=pod
45
58370717 46=head1 SYNOPSIS
47
f83de654 48 Moose::Util::MetaRole::apply_metaclass_roles
49 ( for_class => $caller,
50 constructor_class_roles =>
51 ['MooseX::StrictConstructor::Role::Meta::Method::Constructor'],
52 );
58370717 53
54=head1 DESCRIPTION
55
fbfaa61f 56This role simply wraps C<_generate_BUILDALL()> (from
57C<Moose::Meta::Method::Constructor>) so that immutable classes have a
58strict constructor.
58370717 59
58370717 60=cut
61