work with and require Moose 0.73_01+
[gitmo/MooseX-StrictConstructor.git] / lib / MooseX / StrictConstructor / Role / Meta / Method / Constructor.pm
1 package MooseX::StrictConstructor::Role::Meta::Method::Constructor;
2
3 use strict;
4 use warnings;
5
6 use Carp ();
7
8 use Moose::Role;
9
10 around '_generate_BUILDALL' => sub
11 {
12     my $orig = shift;
13     my $self = shift;
14
15     my $source = $self->$orig();
16     $source .= ";\n" if $source;
17
18     my @attrs =
19         ( map { "$_ => 1," }
20           grep { defined }
21           map { $_->init_arg() }
22           @{ $self->_attributes() }
23         );
24
25     $source .= <<"EOF";
26 my \%attrs = (@attrs);
27
28 my \@bad = sort grep { ! \$attrs{\$_} }  keys \%{ \$params };
29
30 if (\@bad) {
31     Carp::confess "Found unknown attribute(s) passed to the constructor: \@bad";
32 }
33 EOF
34
35     return $source;
36 };
37
38 no Moose::Role;
39
40 1;
41
42 __END__
43
44 =pod
45
46 =head1 NAME
47
48 MooseX::StrictConstructor::Role::Meta::Method::Constructor - A role to make immutable constructors strict
49
50 =head1 SYNOPSIS
51
52   Moose::Util::MetaRole::apply_metaclass_roles
53       ( for_class => $caller,
54         constructor_class_roles =>
55         ['MooseX::StrictConstructor::Role::Meta::Method::Constructor'],
56       );
57
58 =head1 DESCRIPTION
59
60 This role simply wraps C<_generate_BUILDALL()> (from
61 C<Moose::Meta::Method::Constructor>) so that immutable classes have a
62 strict constructor.
63
64 =head1 AUTHOR
65
66 Dave Rolsky, C<< <autarch@urth.org> >>
67
68 =head1 COPYRIGHT & LICENSE
69
70 Copyright 2007-2008 Dave Rolsky, All Rights Reserved.
71
72 This program is free software; you can redistribute it and/or modify
73 it under the same terms as Perl itself.
74
75 =cut
76