Commit | Line | Data |
1a4f7732 |
1 | package MooseX::StrictConstructor::Trait::Class; |
c001451a |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
79b37c7d |
6 | use B (); |
5c3f24ed |
7 | use Carp (); |
c001451a |
8 | |
64c958ef |
9 | use Moose::Role; |
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, |
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 | ); |
c001451a |
32 | }; |
33 | |
64c958ef |
34 | no Moose::Role; |
c001451a |
35 | |
36 | 1; |
58370717 |
37 | |
0639c294 |
38 | # ABSTRACT: A role to make immutable constructors strict |
39 | |
58370717 |
40 | __END__ |
41 | |
42 | =pod |
43 | |
58370717 |
44 | =head1 SYNOPSIS |
45 | |
48ad7621 |
46 | Moose::Util::MetaRole::apply_metaroles( |
47 | for_class => $caller, |
48 | class => { |
49 | constructor => |
1a4f7732 |
50 | ['MooseX::StrictConstructor::Trait::Method::Constructor'], |
48ad7621 |
51 | }, |
52 | ); |
58370717 |
53 | |
54 | =head1 DESCRIPTION |
55 | |
01265e2a |
56 | This role simply wraps C<_inline_BUILDALL()> (from |
57 | C<Moose::Meta::Class>) so that immutable classes have a |
fbfaa61f |
58 | strict constructor. |
58370717 |
59 | |
58370717 |
60 | =cut |