use newer MetaRole API and 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
2c33a849 9use Moose 0.94 ();
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
2c33a849 25 Moose::Util::MetaRole::apply_metaroles(
26 for => $caller,
27 class_metaroles => {
28 constructor => [
29 'MooseX::StrictConstructor::Role::Meta::Method::Constructor']
30 },
5a0d4921 31 );
32
33 Moose::Util::MetaRole::apply_base_class_roles(
2c33a849 34 for => $caller,
5a0d4921 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
f2f2a5dc 56 use Moose;
57 use MooseX::StrictConstructor;
32726d88 58
2ffa7b60 59 has 'size' => ...;
32726d88 60
2ffa7b60 61 # then later ...
62
63 # this blows up because color is not a known attribute
64 My::Class->new( size => 5, color => 'blue' );
32726d88 65
66=head1 DESCRIPTION
67
f2f2a5dc 68Simply loading this module makes your constructors "strict". If your
69constructor is called with an attribute init argument that your class
70does not declare, then it calls "Carp::confess()". This is a great way
71to catch small typos.
2ffa7b60 72
73=head2 Subverting Strictness
74
fbfaa61f 75You may find yourself wanting to have your constructor accept a
76parameter which does not correspond to an attribute.
2ffa7b60 77
fbfaa61f 78In that case, you'll probably also be writing a C<BUILD()> or
79C<BUILDARGS()> method to deal with that parameter. In a C<BUILDARGS()>
80method, you can simply make sure that this parameter is not included
81in the hash reference you return. Otherwise, in a C<BUILD()> method,
82you can delete it from the hash reference of parameters.
2ffa7b60 83
84 sub BUILD {
85 my $self = shift;
86 my $params = shift;
32726d88 87
2ffa7b60 88 if ( delete $params->{do_something} ) {
89 ...
90 }
91 }
32726d88 92
93=head1 AUTHOR
94
95Dave Rolsky, C<< <autarch@urth.org> >>
96
97=head1 BUGS
98
2ffa7b60 99Please report any bugs or feature requests to
100C<bug-moosex-strictconstructor@rt.cpan.org>, or through the web
101interface at L<http://rt.cpan.org>. I will be notified, and then
102you'll automatically be notified of progress on your bug as I make
103changes.
32726d88 104
105=head1 COPYRIGHT & LICENSE
106
fbfaa61f 107Copyright 2007-2008 Dave Rolsky, All Rights Reserved.
32726d88 108
109This program is free software; you can redistribute it and/or modify
110it under the same terms as Perl itself.
111
112=cut