Commit | Line | Data |
32726d88 |
1 | package MooseX::StrictConstructor; |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
2a1fb9c7 |
6 | our $VERSION = '0.06_01'; |
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 | |
0cdff431 |
16 | Moose::Exporter->setup_import_methods( also => 'Moose' ); |
32726d88 |
17 | |
0cdff431 |
18 | sub init_meta |
32726d88 |
19 | { |
0cdff431 |
20 | shift; |
21 | my %p = @_; |
22 | |
23 | Moose->init_meta(%p); |
24 | |
25 | my $caller = $p{for_class}; |
26 | |
fbfaa61f |
27 | Moose::Util::MetaRole::apply_metaclass_roles |
28 | ( for_class => $caller, |
29 | constructor_class_roles => |
30 | ['MooseX::StrictConstructor::Role::Meta::Method::Constructor'], |
31 | ); |
32 | |
33 | Moose::Util::MetaRole::apply_base_class_roles |
34 | ( for_class => $caller, |
35 | roles => |
36 | [ 'MooseX::StrictConstructor::Role::Object' ], |
37 | ); |
0cdff431 |
38 | |
39 | return $caller->meta(); |
32726d88 |
40 | } |
41 | |
32726d88 |
42 | 1; |
43 | |
44 | __END__ |
45 | |
46 | =pod |
47 | |
48 | =head1 NAME |
49 | |
2ffa7b60 |
50 | MooseX::StrictConstructor - Make your object constructors blow up on unknown attributes |
32726d88 |
51 | |
52 | =head1 SYNOPSIS |
53 | |
2ffa7b60 |
54 | package My::Class; |
32726d88 |
55 | |
2ffa7b60 |
56 | use MooseX::StrictConstructor; # instead of use Moose |
32726d88 |
57 | |
2ffa7b60 |
58 | has 'size' => ...; |
32726d88 |
59 | |
2ffa7b60 |
60 | # then later ... |
61 | |
62 | # this blows up because color is not a known attribute |
63 | My::Class->new( size => 5, color => 'blue' ); |
32726d88 |
64 | |
65 | =head1 DESCRIPTION |
66 | |
2ffa7b60 |
67 | Using this class to load Moose instead of just loading using Moose |
68 | itself makes your constructors "strict". If your constructor is called |
a83dec43 |
69 | with an attribute init argument that your class does not declare, then |
70 | it calls "Carp::confess()". This is a great way to catch small typos. |
2ffa7b60 |
71 | |
72 | =head2 Subverting Strictness |
73 | |
fbfaa61f |
74 | You may find yourself wanting to have your constructor accept a |
75 | parameter which does not correspond to an attribute. |
2ffa7b60 |
76 | |
fbfaa61f |
77 | In that case, you'll probably also be writing a C<BUILD()> or |
78 | C<BUILDARGS()> method to deal with that parameter. In a C<BUILDARGS()> |
79 | method, you can simply make sure that this parameter is not included |
80 | in the hash reference you return. Otherwise, in a C<BUILD()> method, |
81 | you can delete it from the hash reference of parameters. |
2ffa7b60 |
82 | |
83 | sub BUILD { |
84 | my $self = shift; |
85 | my $params = shift; |
32726d88 |
86 | |
2ffa7b60 |
87 | if ( delete $params->{do_something} ) { |
88 | ... |
89 | } |
90 | } |
32726d88 |
91 | |
92 | =head1 AUTHOR |
93 | |
94 | Dave Rolsky, C<< <autarch@urth.org> >> |
95 | |
96 | =head1 BUGS |
97 | |
2ffa7b60 |
98 | Please report any bugs or feature requests to |
99 | C<bug-moosex-strictconstructor@rt.cpan.org>, or through the web |
100 | interface at L<http://rt.cpan.org>. I will be notified, and then |
101 | you'll automatically be notified of progress on your bug as I make |
102 | changes. |
32726d88 |
103 | |
104 | =head1 COPYRIGHT & LICENSE |
105 | |
fbfaa61f |
106 | Copyright 2007-2008 Dave Rolsky, All Rights Reserved. |
32726d88 |
107 | |
108 | This program is free software; you can redistribute it and/or modify |
109 | it under the same terms as Perl itself. |
110 | |
111 | =cut |