require Moose 0.55_04
[gitmo/MooseX-StrictConstructor.git] / lib / MooseX / StrictConstructor.pm
CommitLineData
32726d88 1package MooseX::StrictConstructor;
2
3use strict;
4use warnings;
5
2a1fb9c7 6our $VERSION = '0.06_01';
c633cb7e 7$VERSION = eval $VERSION;
32726d88 8
0cdff431 9use Class::MOP ();
a0a26795 10use Moose 0.5504 ();
0cdff431 11use Moose::Exporter;
fbfaa61f 12use Moose::Util::MetaRole;
0cdff431 13use MooseX::StrictConstructor::Role::Object;
fbfaa61f 14use MooseX::StrictConstructor::Role::Meta::Method::Constructor;
32726d88 15
0cdff431 16Moose::Exporter->setup_import_methods( also => 'Moose' );
32726d88 17
0cdff431 18sub 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 421;
43
44__END__
45
46=pod
47
48=head1 NAME
49
2ffa7b60 50MooseX::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 67Using this class to load Moose instead of just loading using Moose
68itself makes your constructors "strict". If your constructor is called
a83dec43 69with an attribute init argument that your class does not declare, then
70it calls "Carp::confess()". This is a great way to catch small typos.
2ffa7b60 71
72=head2 Subverting Strictness
73
fbfaa61f 74You may find yourself wanting to have your constructor accept a
75parameter which does not correspond to an attribute.
2ffa7b60 76
fbfaa61f 77In that case, you'll probably also be writing a C<BUILD()> or
78C<BUILDARGS()> method to deal with that parameter. In a C<BUILDARGS()>
79method, you can simply make sure that this parameter is not included
80in the hash reference you return. Otherwise, in a C<BUILD()> method,
81you 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
94Dave Rolsky, C<< <autarch@urth.org> >>
95
96=head1 BUGS
97
2ffa7b60 98Please report any bugs or feature requests to
99C<bug-moosex-strictconstructor@rt.cpan.org>, or through the web
100interface at L<http://rt.cpan.org>. I will be notified, and then
101you'll automatically be notified of progress on your bug as I make
102changes.
32726d88 103
104=head1 COPYRIGHT & LICENSE
105
fbfaa61f 106Copyright 2007-2008 Dave Rolsky, All Rights Reserved.
32726d88 107
108This program is free software; you can redistribute it and/or modify
109it under the same terms as Perl itself.
110
111=cut