Commit | Line | Data |
1a4f7732 |
1 | package MooseX::StrictConstructor::Trait::Class; |
c001451a |
2 | |
d6932ec6 |
3 | # applied as class_metaroles => { class => [ __PACKAGE__ ] }, for Moose 1.99x and later |
4 | |
d99e6f32 |
5 | use Moose::Role; |
6 | |
7 | use namespace::autoclean; |
c001451a |
8 | |
79b37c7d |
9 | use B (); |
c001451a |
10 | |
01265e2a |
11 | around '_inline_BUILDALL' => sub { |
64c958ef |
12 | my $orig = shift; |
c001451a |
13 | my $self = shift; |
14 | |
01265e2a |
15 | my @source = $self->$orig(); |
c001451a |
16 | |
5a0d4921 |
17 | my @attrs = ( |
df9653e6 |
18 | '__INSTANCE__ => 1,', |
79b37c7d |
19 | map { B::perlstring($_) . ' => 1,' } |
5a0d4921 |
20 | grep {defined} |
01265e2a |
21 | map { $_->init_arg() } $self->get_all_attributes() |
5a0d4921 |
22 | ); |
c001451a |
23 | |
01265e2a |
24 | return ( |
25 | @source, |
93a34553 |
26 | 'my %attrs = (' . ( join ' ', @attrs ) . ');', |
01265e2a |
27 | 'my @bad = sort grep { !$attrs{$_} } keys %{ $params };', |
28 | 'if (@bad) {', |
675cf837 |
29 | 'Moose->throw_error("Found unknown attribute(s) passed to the constructor: @bad");', |
01265e2a |
30 | '}', |
31 | ); |
c001451a |
32 | }; |
33 | |
c001451a |
34 | 1; |
58370717 |
35 | |
0639c294 |
36 | # ABSTRACT: A role to make immutable constructors strict |
37 | |
58370717 |
38 | __END__ |
39 | |
40 | =pod |
41 | |
58370717 |
42 | =head1 DESCRIPTION |
43 | |
01265e2a |
44 | This role simply wraps C<_inline_BUILDALL()> (from |
45 | C<Moose::Meta::Class>) so that immutable classes have a |
fbfaa61f |
46 | strict constructor. |
58370717 |
47 | |
58370717 |
48 | =cut |