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