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