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