Style fixes
[gitmo/MooseX-StrictConstructor.git] / lib / MooseX / StrictConstructor / Trait / Class.pm
CommitLineData
1a4f7732 1package MooseX::StrictConstructor::Trait::Class;
c001451a 2
d99e6f32 3use Moose::Role;
4
5use namespace::autoclean;
c001451a 6
79b37c7d 7use B ();
c001451a 8
709eccb9 9around new_object => sub {
eb63f59e 10 my $orig = shift;
11 my $self = shift;
12 my $params = @_ == 1 ? $_[0] : {@_};
709eccb9 13 my $instance = $self->$orig(@_);
14
15 my %attrs = (
16 __INSTANCE__ => 1,
eb63f59e 17 (
18 map { $_ => 1 }
19 grep {defined}
20 map { $_->init_arg() } $self->get_all_attributes()
21 )
709eccb9 22 );
23
24 my @bad = sort grep { !$attrs{$_} } keys %$params;
25
26 if (@bad) {
27 $self->throw_error(
eb63f59e 28 "Found unknown attribute(s) init_arg passed to the constructor: @bad"
29 );
709eccb9 30 }
31
32 return $instance;
33};
34
01265e2a 35around '_inline_BUILDALL' => sub {
64c958ef 36 my $orig = shift;
c001451a 37 my $self = shift;
38
01265e2a 39 my @source = $self->$orig();
c001451a 40
5a0d4921 41 my @attrs = (
df9653e6 42 '__INSTANCE__ => 1,',
79b37c7d 43 map { B::perlstring($_) . ' => 1,' }
5a0d4921 44 grep {defined}
01265e2a 45 map { $_->init_arg() } $self->get_all_attributes()
5a0d4921 46 );
c001451a 47
01265e2a 48 return (
49 @source,
93a34553 50 'my %attrs = (' . ( join ' ', @attrs ) . ');',
01265e2a 51 'my @bad = sort grep { !$attrs{$_} } keys %{ $params };',
52 'if (@bad) {',
714128ef 53 'Moose->throw_error("Found unknown attribute(s) passed to the constructor: @bad");',
01265e2a 54 '}',
55 );
0dc0aea2 56} if $Moose::VERSION >= 1.9900;
c001451a 57
c001451a 581;
58370717 59
0639c294 60# ABSTRACT: A role to make immutable constructors strict
61
58370717 62__END__
63
64=pod
65
58370717 66=head1 DESCRIPTION
67
01265e2a 68This role simply wraps C<_inline_BUILDALL()> (from
69C<Moose::Meta::Class>) so that immutable classes have a
fbfaa61f 70strict constructor.
58370717 71
58370717 72=cut