File Coverage

blib/lib/MooseX/StrictConstructor.pm
Criterion Covered Total %
statement 21 21 100.0
branch n/a
condition n/a
subroutine 7 7 100.0
pod n/a
total 28 28 100.0


line stmt bran cond sub pod time code
1             ## no critic (Moose::RequireMakeImmutable)
2             package MooseX::StrictConstructor;
3              
4 3     3   1074393 use strict;
  3         6  
  3         75  
5 3     3   11 use warnings;
  3         4  
  3         113  
6              
7             our $VERSION = '0.21';
8              
9 3     3   11 use Moose 0.94 ();
  3         76  
  3         44  
10 3     3   12 use Moose::Exporter;
  3         2  
  3         18  
11 3     3   88 use Moose::Util::MetaRole;
  3         3  
  3         52  
12              
13 3     3   1045 use MooseX::StrictConstructor::Trait::Class;
  3         7  
  3         88  
14 3     3   1200 use MooseX::StrictConstructor::Trait::Method::Constructor;
  3         5  
  3         195  
15              
16             my %metaroles = (
17             class => ['MooseX::StrictConstructor::Trait::Class'],
18             );
19              
20             $metaroles{constructor}
21             = ['MooseX::StrictConstructor::Trait::Method::Constructor']
22             if $Moose::VERSION <= 1.9900;
23              
24             Moose::Exporter->setup_import_methods( class_metaroles => \%metaroles );
25              
26             1;
27              
28             # ABSTRACT: Make your object constructors blow up on unknown attributes
29              
30             __END__
31              
32             =pod
33              
34             =encoding UTF-8
35              
36             =head1 NAME
37              
38             MooseX::StrictConstructor - Make your object constructors blow up on unknown attributes
39              
40             =head1 VERSION
41              
42             version 0.21
43              
44             =head1 SYNOPSIS
45              
46             package My::Class;
47              
48             use Moose;
49             use MooseX::StrictConstructor;
50              
51             has 'size' => ( is => 'ro' );
52              
53             # then later ...
54              
55             # this blows up because color is not a known attribute
56             My::Class->new( size => 5, color => 'blue' );
57              
58             =head1 DESCRIPTION
59              
60             Simply loading this module makes your constructors "strict". If your
61             constructor is called with an attribute init argument that your class does not
62             declare, then it calls C<< Moose->throw_error() >>. This is a great way to
63             catch small typos.
64              
65             =head2 Subverting Strictness
66              
67             You may find yourself wanting to have your constructor accept a
68             parameter which does not correspond to an attribute.
69              
70             In that case, you'll probably also be writing a C<BUILD()> or
71             C<BUILDARGS()> method to deal with that parameter. In a C<BUILDARGS()>
72             method, you can simply make sure that this parameter is not included
73             in the hash reference you return. Otherwise, in a C<BUILD()> method,
74             you can delete it from the hash reference of parameters.
75              
76             sub BUILD {
77             my $self = shift;
78             my $params = shift;
79              
80             if ( delete $params->{do_something} ) {
81             ...
82             }
83             }
84              
85             =head1 BUGS
86              
87             Please report any bugs or feature requests to
88             C<bug-moosex-strictconstructor@rt.cpan.org>, or through the web
89             interface at L<http://rt.cpan.org>. I will be notified, and then
90             you'll automatically be notified of progress on your bug as I make
91             changes.
92              
93             Bugs may be submitted at L<https://github.com/moose/MooseX-StrictConstructor/issues>.
94              
95             I am also usually active on IRC as 'autarch' on C<irc://irc.perl.org>.
96              
97             =head1 SOURCE
98              
99             The source code repository for MooseX-StrictConstructor can be found at L<https://github.com/moose/MooseX-StrictConstructor>.
100              
101             =head1 DONATIONS
102              
103             If you'd like to thank me for the work I've done on this module, please
104             consider making a "donation" to me via PayPal. I spend a lot of free time
105             creating free software, and would appreciate any support you'd care to offer.
106              
107             Please note that B<I am not suggesting that you must do this> in order for me
108             to continue working on this particular software. I will continue to do so,
109             inasmuch as I have in the past, for as long as it interests me.
110              
111             Similarly, a donation made in this way will probably not make me work on this
112             software much more, unless I get so many donations that I can consider working
113             on free software full time (let's all have a chuckle at that together).
114              
115             To donate, log into PayPal and send money to autarch@urth.org, or use the
116             button at L<http://www.urth.org/~autarch/fs-donation.html>.
117              
118             =head1 AUTHOR
119              
120             Dave Rolsky <autarch@urth.org>
121              
122             =head1 CONTRIBUTORS
123              
124             =for stopwords Jesse Luehrs Karen Etheridge Ricardo Signes
125              
126             =over 4
127              
128             =item *
129              
130             Jesse Luehrs <doy@tozt.net>
131              
132             =item *
133              
134             Karen Etheridge <ether@cpan.org>
135              
136             =item *
137              
138             Ricardo Signes <rjbs@cpan.org>
139              
140             =back
141              
142             =head1 COPYRIGHT AND LICENSE
143              
144             This software is Copyright (c) 2007 - 2017 by Dave Rolsky.
145              
146             This is free software, licensed under:
147              
148             The Artistic License 2.0 (GPL Compatible)
149              
150             The full text of the license can be found in the
151             F<LICENSE> file included with this distribution.
152              
153             =cut