small code formatting tweaks
[gitmo/MooseX-StrictConstructor.git] / lib / MooseX / StrictConstructor / Trait / Method / Constructor.pm
CommitLineData
1a4f7732 1package MooseX::StrictConstructor::Trait::Method::Constructor;
c8a39fc4 2
d99e6f32 3use Moose::Role;
4
5use namespace::autoclean;
c8a39fc4 6
7use B ();
c8a39fc4 8
0b60c40b 9around _generate_BUILDALL => sub {
c8a39fc4 10 my $orig = shift;
11 my $self = shift;
12
13 my $source = $self->$orig();
14 $source .= ";\n" if $source;
15
0b60c40b 16 my @attrs = '__INSTANCE__ => 1,';
17 push @attrs, map { B::perlstring($_) . ' => 1,' }
18 grep { defined }
19 map { $_->init_arg() } @{ $self->_attributes() };
20
c8a39fc4 21
22 $source .= <<"EOF";
23my \%attrs = (@attrs);
24
25my \@bad = sort grep { ! \$attrs{\$_} } keys \%{ \$params };
26
27if (\@bad) {
714128ef 28 Moose->throw_error("Found unknown attribute(s) passed to the constructor: \@bad");
c8a39fc4 29}
30EOF
31
32 return $source;
bb64c045 33} if $Moose::VERSION < 1.9900;
34
35around _eval_environment => sub {
36 my $orig = shift;
37 my $self = shift;
38
39 my $env = $self->$orig();
40
41 my %attrs = map { $_ => 1 }
42 grep { defined }
43 map { $_->init_arg() }
44 $self->associated_metaclass()->get_all_attributes();
45
46 $attrs{__INSTANCE__} = 1;
47
48 $env->{'%allowed_attrs'} = \%attrs;
49
50 return $env;
51} if $Moose::VERSION >= 1.9900;
c8a39fc4 52
c8a39fc4 531;
54
55# ABSTRACT: A role to make immutable constructors strict
56
57__END__
58
59=pod
60
c8a39fc4 61=head1 DESCRIPTION
62
63This role simply wraps C<_generate_BUILDALL()> (from
64C<Moose::Meta::Method::Constructor>) so that immutable classes have a
65strict constructor.
66
67=cut
68