4b5a7651323b64699c5c81bb159ce75c1ed5fa42
[gitmo/MooseX-StrictConstructor.git] / lib / MooseX / StrictConstructor / Role / Object.pm
1 package MooseX::StrictConstructor::Role::Object;
2
3 use Moose::Role;
4
5 use namespace::autoclean;
6
7 after 'BUILDALL' => sub {
8     my $self   = shift;
9     my $params = shift;
10
11     my %attrs = (
12         __INSTANCE__ => 1,
13         map { $_ => 1 }
14         grep {defined}
15         map  { $_->init_arg() } $self->meta()->get_all_attributes()
16     );
17
18     my @bad = sort grep { !$attrs{$_} } keys %{$params};
19
20     if (@bad) {
21         Moose->throw_error(
22             "Found unknown attribute(s) init_arg passed to the constructor: @bad");
23     }
24
25     return;
26 };
27
28 1;
29
30 # ABSTRACT: A role which implements a strict constructor for Moose::Object
31
32 __END__
33
34 =pod
35
36 =head1 SYNOPSIS
37
38   Moose::Util::MetaRole::apply_base_class_roles(
39       for_class => $caller,
40       roles =>
41           ['MooseX::StrictConstructor::Role::Object'],
42   );
43
44 =head1 DESCRIPTION
45
46 When you use C<MooseX::StrictConstructor>, your objects will have this
47 role applied to them. It provides a method modifier for C<BUILDALL()>
48 from C<Moose::Object> that implements strict argument checking for
49 your class.
50
51 =cut