make this work with old and new moose
[gitmo/MooseX-StrictConstructor.git] / lib / MooseX / StrictConstructor.pm
1 package MooseX::StrictConstructor;
2
3 use strict;
4 use warnings;
5
6 use Moose 0.94 ();
7 use Moose::Exporter;
8 use Moose::Util::MetaRole;
9 use MooseX::StrictConstructor::Role::Object;
10 use MooseX::StrictConstructor::Role::Meta::Class;
11 use MooseX::StrictConstructor::Role::Meta::Method::Constructor;
12
13 Moose::Exporter->setup_import_methods(
14     class_metaroles => {
15         ($Moose::VERSION >= 1.9900
16             ? (class => ['MooseX::StrictConstructor::Role::Meta::Class'])
17             : (constructor => ['MooseX::StrictConstructor::Role::Meta::Method::Constructor'])),
18     },
19     base_class_roles => ['MooseX::StrictConstructor::Role::Object'],
20 );
21
22 1;
23
24 # ABSTRACT: Make your object constructors blow up on unknown attributes
25
26 __END__
27
28 =pod
29
30 =head1 SYNOPSIS
31
32     package My::Class;
33
34     use Moose;
35     use MooseX::StrictConstructor;
36
37     has 'size' => ...;
38
39     # then later ...
40
41     # this blows up because color is not a known attribute
42     My::Class->new( size => 5, color => 'blue' );
43
44 =head1 DESCRIPTION
45
46 Simply loading this module makes your constructors "strict". If your
47 constructor is called with an attribute init argument that your class
48 does not declare, then it calls "Carp::confess()". This is a great way
49 to catch small typos.
50
51 =head2 Subverting Strictness
52
53 You may find yourself wanting to have your constructor accept a
54 parameter which does not correspond to an attribute.
55
56 In that case, you'll probably also be writing a C<BUILD()> or
57 C<BUILDARGS()> method to deal with that parameter. In a C<BUILDARGS()>
58 method, you can simply make sure that this parameter is not included
59 in the hash reference you return. Otherwise, in a C<BUILD()> method,
60 you can delete it from the hash reference of parameters.
61
62   sub BUILD {
63       my $self   = shift;
64       my $params = shift;
65
66       if ( delete $params->{do_something} ) {
67           ...
68       }
69   }
70
71 =head1 BUGS
72
73 Please report any bugs or feature requests to
74 C<bug-moosex-strictconstructor@rt.cpan.org>, or through the web
75 interface at L<http://rt.cpan.org>.  I will be notified, and then
76 you'll automatically be notified of progress on your bug as I make
77 changes.
78
79 =cut