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