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