Commit | Line | Data |
---|---|---|
fbfaa61f | 1 | package MooseX::StrictConstructor::Role::Meta::Method::Constructor; |
c001451a | 2 | |
3 | use strict; | |
4 | use warnings; | |
5 | ||
5c3f24ed | 6 | use Carp (); |
c001451a | 7 | |
64c958ef | 8 | use Moose::Role; |
c001451a | 9 | |
5a0d4921 | 10 | around '_generate_BUILDALL' => sub { |
64c958ef | 11 | my $orig = shift; |
c001451a | 12 | my $self = shift; |
13 | ||
64c958ef | 14 | my $source = $self->$orig(); |
5c3f24ed | 15 | $source .= ";\n" if $source; |
c001451a | 16 | |
5a0d4921 | 17 | my @attrs = ( |
18 | map {"$_ => 1,"} | |
19 | grep {defined} | |
20 | map { $_->init_arg() } @{ $self->_attributes() } | |
21 | ); | |
c001451a | 22 | |
5c3f24ed | 23 | $source .= <<"EOF"; |
24 | my \%attrs = (@attrs); | |
c001451a | 25 | |
5c40fb22 | 26 | my \@bad = sort grep { ! \$attrs{\$_} } keys \%{ \$params }; |
5c3f24ed | 27 | |
28 | if (\@bad) { | |
29 | Carp::confess "Found unknown attribute(s) passed to the constructor: \@bad"; | |
30 | } | |
c001451a | 31 | EOF |
32 | ||
5c3f24ed | 33 | return $source; |
c001451a | 34 | }; |
35 | ||
64c958ef | 36 | no Moose::Role; |
c001451a | 37 | |
38 | 1; | |
58370717 | 39 | |
40 | __END__ | |
41 | ||
42 | =pod | |
43 | ||
44 | =head1 NAME | |
45 | ||
fbfaa61f | 46 | MooseX::StrictConstructor::Role::Meta::Method::Constructor - A role to make immutable constructors strict |
58370717 | 47 | |
48 | =head1 SYNOPSIS | |
49 | ||
f83de654 | 50 | Moose::Util::MetaRole::apply_metaclass_roles |
51 | ( for_class => $caller, | |
52 | constructor_class_roles => | |
53 | ['MooseX::StrictConstructor::Role::Meta::Method::Constructor'], | |
54 | ); | |
58370717 | 55 | |
56 | =head1 DESCRIPTION | |
57 | ||
fbfaa61f | 58 | This role simply wraps C<_generate_BUILDALL()> (from |
59 | C<Moose::Meta::Method::Constructor>) so that immutable classes have a | |
60 | strict constructor. | |
58370717 | 61 | |
62 | =head1 AUTHOR | |
63 | ||
64 | Dave Rolsky, C<< <autarch@urth.org> >> | |
65 | ||
66 | =head1 COPYRIGHT & LICENSE | |
67 | ||
fbfaa61f | 68 | Copyright 2007-2008 Dave Rolsky, All Rights Reserved. |
58370717 | 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 |