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