bump version
[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';
7eval $VERSION = $VERSION;
32726d88 8
0cdff431 9use Class::MOP ();
10use Moose ();
11use Moose::Exporter;
12use MooseX::StrictConstructor::Role::Object;
13use MooseX::StrictConstructor::Role::Metaclass;
32726d88 14
0cdff431 15Moose::Exporter->setup_import_methods( also => 'Moose' );
32726d88 16
0cdff431 17sub init_meta
32726d88 18{
0cdff431 19 shift;
20 my %p = @_;
21
22 Moose->init_meta(%p);
23
24 my $caller = $p{for_class};
25
26 my $metameta = $caller->meta()->meta();
27 unless ( $metameta->can('does_role')
28 && $metameta->does_role( 'MooseX::StrictConstructor::Role::Metaclass' ) )
29 {
30 my $new_meta =
31 Moose::Meta::Class->create_anon_class
32 ( superclasses => [ ref $caller->meta() ],
33 roles => [ 'MooseX::StrictConstructor::Role::Metaclass' ],
34 cache => 1,
35 );
36
37 Class::MOP::remove_metaclass_by_name($caller);
38
39 $new_meta->name()->initialize($caller);
40 }
41
42 unless ( $caller->meta()->does_role('MooseX::StrictConstructor::Role::Object') )
43 {
44 my $new_base =
45 Moose::Meta::Class->create_anon_class
46 ( superclasses => [ $caller->meta()->superclasses() ],
47 roles => [ 'MooseX::StrictConstructor::Role::Object' ],
48 cache => 1,
49 );
50
51 $caller->meta()->superclasses( $new_base->name() );
52 }
53
54 return $caller->meta();
32726d88 55}
56
32726d88 571;
58
59__END__
60
61=pod
62
63=head1 NAME
64
2ffa7b60 65MooseX::StrictConstructor - Make your object constructors blow up on unknown attributes
32726d88 66
67=head1 SYNOPSIS
68
2ffa7b60 69 package My::Class;
32726d88 70
2ffa7b60 71 use MooseX::StrictConstructor; # instead of use Moose
32726d88 72
2ffa7b60 73 has 'size' => ...;
32726d88 74
2ffa7b60 75 # then later ...
76
77 # this blows up because color is not a known attribute
78 My::Class->new( size => 5, color => 'blue' );
32726d88 79
80=head1 DESCRIPTION
81
2ffa7b60 82Using this class to load Moose instead of just loading using Moose
83itself makes your constructors "strict". If your constructor is called
a83dec43 84with an attribute init argument that your class does not declare, then
85it calls "Carp::confess()". This is a great way to catch small typos.
2ffa7b60 86
87=head2 Subverting Strictness
88
89You may find yourself wanting to accept a parameter to the constructor
90that is not the name of an attribute.
91
92In that case, you'll probably be writing a C<BUILD()> method to deal
93with it. Your C<BUILD()> method will receive two parameters, the new
94object, and a hash reference of parameters passed to the constructor.
95
96If you delete keys from this hash reference, then they will not be
97seen when this class does its checking.
98
99 sub BUILD {
100 my $self = shift;
101 my $params = shift;
32726d88 102
2ffa7b60 103 if ( delete $params->{do_something} ) {
104 ...
105 }
106 }
32726d88 107
c001451a 108=head2 Caveats
109
110Using this class replaces the default Moose meta class,
111C<Moose::Meta::Class>, with its own,
112C<MooseX::StrictConstructor::Meta::Class>. If you have your own meta
113class, this distro will probably not work for you.
114
32726d88 115=head1 AUTHOR
116
117Dave Rolsky, C<< <autarch@urth.org> >>
118
119=head1 BUGS
120
2ffa7b60 121Please report any bugs or feature requests to
122C<bug-moosex-strictconstructor@rt.cpan.org>, or through the web
123interface at L<http://rt.cpan.org>. I will be notified, and then
124you'll automatically be notified of progress on your bug as I make
125changes.
32726d88 126
127=head1 COPYRIGHT & LICENSE
128
129Copyright 2007 Dave Rolsky, All Rights Reserved.
130
131This program is free software; you can redistribute it and/or modify
132it under the same terms as Perl itself.
133
134=cut