File Coverage

blib/lib/MooseX/StrictConstructor/Trait/Method/Constructor.pm
Criterion Covered Total %
statement 15 15 100.0
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 20 20 100.0


line stmt bran cond sub pod time code
1             package MooseX::StrictConstructor::Trait::Method::Constructor;
2              
3 3     3   13 use strict;
  3         3  
  3         76  
4 3     3   9 use warnings;
  3         3  
  3         57  
5 3     3   9 use namespace::autoclean;
  3         4  
  3         12  
6              
7             our $VERSION = '0.21';
8              
9 3     3   155 use Moose::Role;
  3         4  
  3         12  
10              
11 3     3   9380 use B ();
  3         4  
  3         460  
12              
13             around _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";
26             my \%attrs = (@attrs);
27              
28             my \@bad = sort grep { ! \$attrs{\$_} } keys \%{ \$params };
29              
30             if (\@bad) {
31             Moose->throw_error("Found unknown attribute(s) passed to the constructor: \@bad");
32             }
33             EOF
34              
35             return $source;
36             }
37             if $Moose::VERSION < 1.9900;
38              
39             1;
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              
51             MooseX::StrictConstructor::Trait::Method::Constructor - A role to make immutable constructors strict
52              
53             =head1 VERSION
54              
55             version 0.21
56              
57             =head1 DESCRIPTION
58              
59             This role simply wraps C<_generate_BUILDALL()> (from
60             C<Moose::Meta::Method::Constructor>) so that immutable classes have a
61             strict constructor.
62              
63             =head1 SUPPORT
64              
65             Bugs may be submitted at L<https://github.com/moose/MooseX-StrictConstructor/issues>.
66              
67             I am also usually active on IRC as 'autarch' on C<irc://irc.perl.org>.
68              
69             =head1 SOURCE
70              
71             The source code repository for MooseX-StrictConstructor can be found at L<https://github.com/moose/MooseX-StrictConstructor>.
72              
73             =head1 AUTHOR
74              
75             Dave Rolsky <autarch@urth.org>
76              
77             =head1 COPYRIGHT AND LICENSE
78              
79             This software is Copyright (c) 2007 - 2017 by Dave Rolsky.
80              
81             This is free software, licensed under:
82              
83             The Artistic License 2.0 (GPL Compatible)
84              
85             The full text of the license can be found in the
86             F<LICENSE> file included with this distribution.
87              
88             =cut