Ignore __INSTANCE__ as constructor arg
[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 = (
d78f8e74 18 "__INSTANCE__ => 1",
5a0d4921 19 map {"$_ => 1,"}
20 grep {defined}
21 map { $_->init_arg() } @{ $self->_attributes() }
22 );
c001451a 23
5c3f24ed 24 $source .= <<"EOF";
25my \%attrs = (@attrs);
c001451a 26
5c40fb22 27my \@bad = sort grep { ! \$attrs{\$_} } keys \%{ \$params };
5c3f24ed 28
29if (\@bad) {
30 Carp::confess "Found unknown attribute(s) passed to the constructor: \@bad";
31}
c001451a 32EOF
33
5c3f24ed 34 return $source;
c001451a 35};
36
64c958ef 37no Moose::Role;
c001451a 38
391;
58370717 40
0639c294 41# ABSTRACT: A role to make immutable constructors strict
42
58370717 43__END__
44
45=pod
46
58370717 47=head1 SYNOPSIS
48
f83de654 49 Moose::Util::MetaRole::apply_metaclass_roles
50 ( for_class => $caller,
51 constructor_class_roles =>
52 ['MooseX::StrictConstructor::Role::Meta::Method::Constructor'],
53 );
58370717 54
55=head1 DESCRIPTION
56
fbfaa61f 57This role simply wraps C<_generate_BUILDALL()> (from
58C<Moose::Meta::Method::Constructor>) so that immutable classes have a
59strict constructor.
58370717 60
58370717 61=cut
62