working role support!
[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;
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 411;
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 65Simply loading this module makes your constructors "strict". If your
66constructor is called with an attribute init argument that your class
675cf837 67does not declare, then it calls C<Moose->throw_error()>. This is a great way
f2f2a5dc 68to catch small typos.
2ffa7b60 69
44641a61 70As of Moose 1.9900, this module can also be used in a role, in which case the
71constructor of the consuming class will become strict.
72
73
2ffa7b60 74=head2 Subverting Strictness
75
fbfaa61f 76You may find yourself wanting to have your constructor accept a
77parameter which does not correspond to an attribute.
2ffa7b60 78
fbfaa61f 79In that case, you'll probably also be writing a C<BUILD()> or
80C<BUILDARGS()> method to deal with that parameter. In a C<BUILDARGS()>
81method, you can simply make sure that this parameter is not included
82in the hash reference you return. Otherwise, in a C<BUILD()> method,
83you 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 96Please report any bugs or feature requests to
97C<bug-moosex-strictconstructor@rt.cpan.org>, or through the web
98interface at L<http://rt.cpan.org>. I will be notified, and then
99you'll automatically be notified of progress on your bug as I make
100changes.
32726d88 101
32726d88 102=cut