use Moose->throw_error rather than confess
[gitmo/MooseX-StrictConstructor.git] / lib / MooseX / StrictConstructor / Trait / Class.pm
1 package MooseX::StrictConstructor::Trait::Class;
2
3 use Moose::Role;
4
5 use namespace::autoclean;
6
7 use B ();
8
9 around '_inline_BUILDALL' => sub {
10     my $orig = shift;
11     my $self = shift;
12
13     my @source = $self->$orig();
14
15     my @attrs = (
16         '__INSTANCE__ => 1,',
17         map { B::perlstring($_) . ' => 1,' }
18         grep {defined}
19         map  { $_->init_arg() } $self->get_all_attributes()
20     );
21
22     return (
23         @source,
24         'my %attrs = (' . ( join ' ', @attrs ) . ');',
25         'my @bad = sort grep { !$attrs{$_} } keys %{ $params };',
26         'if (@bad) {',
27             'Moose->throw_error("Found unknown attribute(s) passed to the constructor: @bad");',
28         '}',
29     );
30 };
31
32 1;
33
34 # ABSTRACT: A role to make immutable constructors strict
35
36 __END__
37
38 =pod
39
40 =head1 DESCRIPTION
41
42 This role simply wraps C<_inline_BUILDALL()> (from
43 C<Moose::Meta::Class>) so that immutable classes have a
44 strict constructor.
45
46 =cut