PodTests became PodCoverageTests + PodSyntaxTests.
[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 use Carp ();
9
10 around '_inline_BUILDALL' => sub {
11     my $orig = shift;
12     my $self = shift;
13
14     my @source = $self->$orig();
15
16     my @attrs = (
17         '__INSTANCE__ => 1,',
18         map { B::perlstring($_) . ' => 1,' }
19         grep {defined}
20         map  { $_->init_arg() } $self->get_all_attributes()
21     );
22
23     return (
24         @source,
25         'my %attrs = (' . ( join ' ', @attrs ) . ');',
26         'my @bad = sort grep { !$attrs{$_} } keys %{ $params };',
27         'if (@bad) {',
28             'Carp::confess "Found unknown attribute(s) passed to the constructor: @bad";',
29         '}',
30     );
31 };
32
33 1;
34
35 # ABSTRACT: A role to make immutable constructors strict
36
37 __END__
38
39 =pod
40
41 =head1 DESCRIPTION
42
43 This role simply wraps C<_inline_BUILDALL()> (from
44 C<Moose::Meta::Class>) so that immutable classes have a
45 strict constructor.
46
47 =cut