Commit | Line | Data |
32726d88 |
1 | package MooseX::StrictConstructor; |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
343ac26e |
6 | our $VERSION = '0.06_02'; |
c633cb7e |
7 | $VERSION = eval $VERSION; |
32726d88 |
8 | |
0cdff431 |
9 | use Class::MOP (); |
a0a26795 |
10 | use Moose 0.5504 (); |
0cdff431 |
11 | use Moose::Exporter; |
fbfaa61f |
12 | use Moose::Util::MetaRole; |
0cdff431 |
13 | use MooseX::StrictConstructor::Role::Object; |
fbfaa61f |
14 | use MooseX::StrictConstructor::Role::Meta::Method::Constructor; |
32726d88 |
15 | |
f2f2a5dc |
16 | |
17 | Moose::Exporter->setup_import_methods(); |
32726d88 |
18 | |
0cdff431 |
19 | sub init_meta |
32726d88 |
20 | { |
0cdff431 |
21 | shift; |
22 | my %p = @_; |
23 | |
24 | Moose->init_meta(%p); |
25 | |
26 | my $caller = $p{for_class}; |
27 | |
fbfaa61f |
28 | Moose::Util::MetaRole::apply_metaclass_roles |
29 | ( for_class => $caller, |
30 | constructor_class_roles => |
31 | ['MooseX::StrictConstructor::Role::Meta::Method::Constructor'], |
32 | ); |
33 | |
34 | Moose::Util::MetaRole::apply_base_class_roles |
35 | ( for_class => $caller, |
36 | roles => |
37 | [ 'MooseX::StrictConstructor::Role::Object' ], |
38 | ); |
0cdff431 |
39 | |
40 | return $caller->meta(); |
32726d88 |
41 | } |
42 | |
32726d88 |
43 | 1; |
44 | |
45 | __END__ |
46 | |
47 | =pod |
48 | |
49 | =head1 NAME |
50 | |
2ffa7b60 |
51 | MooseX::StrictConstructor - Make your object constructors blow up on unknown attributes |
32726d88 |
52 | |
53 | =head1 SYNOPSIS |
54 | |
2ffa7b60 |
55 | package My::Class; |
32726d88 |
56 | |
f2f2a5dc |
57 | use Moose; |
58 | use MooseX::StrictConstructor; |
32726d88 |
59 | |
2ffa7b60 |
60 | has 'size' => ...; |
32726d88 |
61 | |
2ffa7b60 |
62 | # then later ... |
63 | |
64 | # this blows up because color is not a known attribute |
65 | My::Class->new( size => 5, color => 'blue' ); |
32726d88 |
66 | |
67 | =head1 DESCRIPTION |
68 | |
f2f2a5dc |
69 | Simply loading this module makes your constructors "strict". If your |
70 | constructor is called with an attribute init argument that your class |
71 | does not declare, then it calls "Carp::confess()". This is a great way |
72 | to catch small typos. |
2ffa7b60 |
73 | |
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 | |
94 | =head1 AUTHOR |
95 | |
96 | Dave Rolsky, C<< <autarch@urth.org> >> |
97 | |
98 | =head1 BUGS |
99 | |
2ffa7b60 |
100 | Please report any bugs or feature requests to |
101 | C<bug-moosex-strictconstructor@rt.cpan.org>, or through the web |
102 | interface at L<http://rt.cpan.org>. I will be notified, and then |
103 | you'll automatically be notified of progress on your bug as I make |
104 | changes. |
32726d88 |
105 | |
106 | =head1 COPYRIGHT & LICENSE |
107 | |
fbfaa61f |
108 | Copyright 2007-2008 Dave Rolsky, All Rights Reserved. |
32726d88 |
109 | |
110 | This program is free software; you can redistribute it and/or modify |
111 | it under the same terms as Perl itself. |
112 | |
113 | =cut |