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