s/Role::Meta/Trait/g
[gitmo/MooseX-StrictConstructor.git] / lib / MooseX / StrictConstructor.pm
1 package MooseX::StrictConstructor;
2
3 use strict;
4 use warnings;
5
6 use Moose 0.94 ();
7 use Moose::Exporter;
8 use Moose::Util::MetaRole;
9 use MooseX::StrictConstructor::Role::Object;
10
11 {
12     my %class_meta;
13
14     if ( $Moose::VERSION < 1.9900 ) {
15         require MooseX::StrictConstructor::Trait::Method::Constructor;
16         %class_meta = (
17             constructor => [
18                 'MooseX::StrictConstructor::Trait::Method::Constructor']
19         );
20     }
21     else {
22         require MooseX::StrictConstructor::Trait::Class;
23         %class_meta
24             = ( class => ['MooseX::StrictConstructor::Trait::Class'] );
25     }
26
27     Moose::Exporter->setup_import_methods(
28             class_metaroles  => \%class_meta,
29             base_class_roles => ['MooseX::StrictConstructor::Role::Object'],
30     );
31 }
32
33 1;
34
35 # ABSTRACT: Make your object constructors blow up on unknown attributes
36
37 __END__
38
39 =pod
40
41 =head1 SYNOPSIS
42
43     package My::Class;
44
45     use Moose;
46     use MooseX::StrictConstructor;
47
48     has 'size' => ...;
49
50     # then later ...
51
52     # this blows up because color is not a known attribute
53     My::Class->new( size => 5, color => 'blue' );
54
55 =head1 DESCRIPTION
56
57 Simply loading this module makes your constructors "strict". If your
58 constructor is called with an attribute init argument that your class
59 does not declare, then it calls "Carp::confess()". This is a great way
60 to catch small typos.
61
62 =head2 Subverting Strictness
63
64 You may find yourself wanting to have your constructor accept a
65 parameter which does not correspond to an attribute.
66
67 In that case, you'll probably also be writing a C<BUILD()> or
68 C<BUILDARGS()> method to deal with that parameter. In a C<BUILDARGS()>
69 method, you can simply make sure that this parameter is not included
70 in the hash reference you return. Otherwise, in a C<BUILD()> method,
71 you can delete it from the hash reference of parameters.
72
73   sub BUILD {
74       my $self   = shift;
75       my $params = shift;
76
77       if ( delete $params->{do_something} ) {
78           ...
79       }
80   }
81
82 =head1 BUGS
83
84 Please report any bugs or feature requests to
85 C<bug-moosex-strictconstructor@rt.cpan.org>, or through the web
86 interface at L<http://rt.cpan.org>.  I will be notified, and then
87 you'll automatically be notified of progress on your bug as I make
88 changes.
89
90 =cut