Commit | Line | Data |
32726d88 |
1 | package MooseX::StrictConstructor; |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
2c33a849 |
6 | use Moose 0.94 (); |
0cdff431 |
7 | use Moose::Exporter; |
fbfaa61f |
8 | use Moose::Util::MetaRole; |
0cdff431 |
9 | use MooseX::StrictConstructor::Role::Object; |
7815dbf4 |
10 | |
11 | { |
12 | my %class_meta; |
44641a61 |
13 | my %role_meta; |
7815dbf4 |
14 | |
15 | if ( $Moose::VERSION < 1.9900 ) { |
1a4f7732 |
16 | require MooseX::StrictConstructor::Trait::Method::Constructor; |
7815dbf4 |
17 | %class_meta = ( |
18 | constructor => [ |
1a4f7732 |
19 | 'MooseX::StrictConstructor::Trait::Method::Constructor'] |
7815dbf4 |
20 | ); |
21 | } |
22 | else { |
1a4f7732 |
23 | require MooseX::StrictConstructor::Trait::Class; |
7815dbf4 |
24 | %class_meta |
1a4f7732 |
25 | = ( class => ['MooseX::StrictConstructor::Trait::Class'] ); |
44641a61 |
26 | %role_meta |
27 | = ( |
28 | role => ['MooseX::StrictConstructor::Trait::Role'], |
29 | application_to_class => ['MooseX::StrictConstructor::Trait::ApplicationToClass'], |
30 | application_to_role =>['MooseX::StrictConstructor::Trait::ApplicationToRole'], |
31 | ); |
7815dbf4 |
32 | } |
33 | |
34 | Moose::Exporter->setup_import_methods( |
35 | class_metaroles => \%class_meta, |
44641a61 |
36 | role_metaroles => \%role_meta, |
7815dbf4 |
37 | base_class_roles => ['MooseX::StrictConstructor::Role::Object'], |
38 | ); |
39 | } |
32726d88 |
40 | |
32726d88 |
41 | 1; |
42 | |
5b66b6d4 |
43 | # ABSTRACT: Make your object constructors blow up on unknown attributes |
44 | |
32726d88 |
45 | __END__ |
46 | |
47 | =pod |
48 | |
32726d88 |
49 | =head1 SYNOPSIS |
50 | |
2ffa7b60 |
51 | package My::Class; |
32726d88 |
52 | |
f2f2a5dc |
53 | use Moose; |
54 | use MooseX::StrictConstructor; |
32726d88 |
55 | |
2ffa7b60 |
56 | has 'size' => ...; |
32726d88 |
57 | |
2ffa7b60 |
58 | # then later ... |
59 | |
60 | # this blows up because color is not a known attribute |
61 | My::Class->new( size => 5, color => 'blue' ); |
32726d88 |
62 | |
63 | =head1 DESCRIPTION |
64 | |
f2f2a5dc |
65 | Simply loading this module makes your constructors "strict". If your |
66 | constructor is called with an attribute init argument that your class |
675cf837 |
67 | does not declare, then it calls C<Moose->throw_error()>. This is a great way |
f2f2a5dc |
68 | to catch small typos. |
2ffa7b60 |
69 | |
44641a61 |
70 | As of Moose 1.9900, this module can also be used in a role, in which case the |
71 | constructor of the consuming class will become strict. |
72 | |
73 | |
2ffa7b60 |
74 | =head2 Subverting Strictness |
75 | |
fbfaa61f |
76 | You may find yourself wanting to have your constructor accept a |
77 | parameter which does not correspond to an attribute. |
2ffa7b60 |
78 | |
fbfaa61f |
79 | In that case, you'll probably also be writing a C<BUILD()> or |
80 | C<BUILDARGS()> method to deal with that parameter. In a C<BUILDARGS()> |
81 | method, you can simply make sure that this parameter is not included |
82 | in the hash reference you return. Otherwise, in a C<BUILD()> method, |
83 | you can delete it from the hash reference of parameters. |
2ffa7b60 |
84 | |
85 | sub BUILD { |
86 | my $self = shift; |
87 | my $params = shift; |
32726d88 |
88 | |
2ffa7b60 |
89 | if ( delete $params->{do_something} ) { |
90 | ... |
91 | } |
92 | } |
32726d88 |
93 | |
32726d88 |
94 | =head1 BUGS |
95 | |
2ffa7b60 |
96 | Please report any bugs or feature requests to |
97 | C<bug-moosex-strictconstructor@rt.cpan.org>, or through the web |
98 | interface at L<http://rt.cpan.org>. I will be notified, and then |
99 | you'll automatically be notified of progress on your bug as I make |
100 | changes. |
32726d88 |
101 | |
32726d88 |
102 | =cut |