Redid this as (mostly) roles which are applied at runtime to the meta
[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';
7
8 use Class::MOP ();
9 use Moose ();
10 use Moose::Exporter;
11 use MooseX::StrictConstructor::Role::Object;
12 use MooseX::StrictConstructor::Role::Metaclass;
13
14 Moose::Exporter->setup_import_methods( also => 'Moose' );
15
16 sub init_meta
17 {
18     shift;
19     my %p = @_;
20
21     Moose->init_meta(%p);
22
23     my $caller = $p{for_class};
24
25     my $metameta = $caller->meta()->meta();
26     unless ( $metameta->can('does_role')
27              && $metameta->does_role( 'MooseX::StrictConstructor::Role::Metaclass' ) )
28     {
29         my $new_meta =
30             Moose::Meta::Class->create_anon_class
31                 ( superclasses => [ ref $caller->meta() ],
32                   roles        => [ 'MooseX::StrictConstructor::Role::Metaclass' ],
33                   cache        => 1,
34                 );
35
36         Class::MOP::remove_metaclass_by_name($caller);
37
38         $new_meta->name()->initialize($caller);
39     }
40
41     unless ( $caller->meta()->does_role('MooseX::StrictConstructor::Role::Object') )
42     {
43         my $new_base =
44             Moose::Meta::Class->create_anon_class
45                 ( superclasses => [ $caller->meta()->superclasses() ],
46                   roles        => [ 'MooseX::StrictConstructor::Role::Object' ],
47                   cache        => 1,
48                 );
49
50         $caller->meta()->superclasses( $new_base->name() );
51     }
52
53     return $caller->meta();
54 }
55
56 1;
57
58 __END__
59
60 =pod
61
62 =head1 NAME
63
64 MooseX::StrictConstructor - Make your object constructors blow up on unknown attributes
65
66 =head1 SYNOPSIS
67
68     package My::Class;
69
70     use MooseX::StrictConstructor; # instead of use Moose
71
72     has 'size' => ...;
73
74     # then later ...
75
76     # this blows up because color is not a known attribute
77     My::Class->new( size => 5, color => 'blue' );
78
79 =head1 DESCRIPTION
80
81 Using this class to load Moose instead of just loading using Moose
82 itself makes your constructors "strict". If your constructor is called
83 with an attribute init argument that your class does not declare, then
84 it calls "Carp::confess()". This is a great way to catch small typos.
85
86 =head2 Subverting Strictness
87
88 You may find yourself wanting to accept a parameter to the constructor
89 that is not the name of an attribute.
90
91 In that case, you'll probably be writing a C<BUILD()> method to deal
92 with it. Your C<BUILD()> method will receive two parameters, the new
93 object, and a hash reference of parameters passed to the constructor.
94
95 If you delete keys from this hash reference, then they will not be
96 seen when this class does its checking.
97
98   sub BUILD {
99       my $self   = shift;
100       my $params = shift;
101
102       if ( delete $params->{do_something} ) {
103           ...
104       }
105   }
106
107 =head2 Caveats
108
109 Using this class replaces the default Moose meta class,
110 C<Moose::Meta::Class>, with its own,
111 C<MooseX::StrictConstructor::Meta::Class>. If you have your own meta
112 class, this distro will probably not work for you.
113
114 =head1 AUTHOR
115
116 Dave Rolsky, C<< <autarch@urth.org> >>
117
118 =head1 BUGS
119
120 Please report any bugs or feature requests to
121 C<bug-moosex-strictconstructor@rt.cpan.org>, or through the web
122 interface at L<http://rt.cpan.org>.  I will be notified, and then
123 you'll automatically be notified of progress on your bug as I make
124 changes.
125
126 =head1 COPYRIGHT & LICENSE
127
128 Copyright 2007 Dave Rolsky, All Rights Reserved.
129
130 This program is free software; you can redistribute it and/or modify
131 it under the same terms as Perl itself.
132
133 =cut