1package MooseX::StrictConstructor::Trait::Method::Constructor;
2
3use strict;
4use warnings;
5use namespace::autoclean;
6
7our $VERSION = '0.21';
8
9use Moose::Role;
10
11use B ();
12
13around _generate_BUILDALL => sub {
14    my $orig = shift;
15    my $self = shift;
16
17    my $source = $self->$orig();
18    $source .= ";\n" if $source;
19
20    my @attrs = ( '__INSTANCE__ => 1,', '__no_BUILD__ => 1,' );
21    push @attrs, map { B::perlstring($_) . ' => 1,' }
22        grep {defined}
23        map  { $_->init_arg() } @{ $self->_attributes() };
24
25    $source .= <<"EOF";
26my \%attrs = (@attrs);
27
28my \@bad = sort grep { ! \$attrs{\$_} }  keys \%{ \$params };
29
30if (\@bad) {
31    Moose->throw_error("Found unknown attribute(s) passed to the constructor: \@bad");
32}
33EOF
34
35    return $source;
36    }
37    if $Moose::VERSION < 1.9900;
38
391;
40
41# ABSTRACT: A role to make immutable constructors strict
42
43__END__
44
45=pod
46
47=encoding UTF-8
48
49=head1 NAME
50
51MooseX::StrictConstructor::Trait::Method::Constructor - A role to make immutable constructors strict
52
53=head1 VERSION
54
55version 0.21
56
57=head1 DESCRIPTION
58
59This role simply wraps C<_generate_BUILDALL()> (from
60C<Moose::Meta::Method::Constructor>) so that immutable classes have a
61strict constructor.
62
63=head1 SUPPORT
64
65Bugs may be submitted at L<https://github.com/moose/MooseX-StrictConstructor/issues>.
66
67I am also usually active on IRC as 'autarch' on C<irc://irc.perl.org>.
68
69=head1 SOURCE
70
71The source code repository for MooseX-StrictConstructor can be found at L<https://github.com/moose/MooseX-StrictConstructor>.
72
73=head1 AUTHOR
74
75Dave Rolsky <autarch@urth.org>
76
77=head1 COPYRIGHT AND LICENSE
78
79This software is Copyright (c) 2007 - 2017 by Dave Rolsky.
80
81This is free software, licensed under:
82
83  The Artistic License 2.0 (GPL Compatible)
84
85The full text of the license can be found in the
86F<LICENSE> file included with this distribution.
87
88=cut
89