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