also test adding strictness in a subclass -- and fix mutable case (thanks doy!) :)
[gitmo/MooseX-StrictConstructor.git] / lib / MooseX / StrictConstructor / Trait / Class.pm
CommitLineData
1a4f7732 1package MooseX::StrictConstructor::Trait::Class;
c001451a 2
d99e6f32 3use Moose::Role;
4
5use namespace::autoclean;
c001451a 6
79b37c7d 7use B ();
c001451a 8
01265e2a 9around '_inline_BUILDALL' => sub {
64c958ef 10 my $orig = shift;
c001451a 11 my $self = shift;
12
01265e2a 13 my @source = $self->$orig();
c001451a 14
5a0d4921 15 my @attrs = (
df9653e6 16 '__INSTANCE__ => 1,',
79b37c7d 17 map { B::perlstring($_) . ' => 1,' }
5a0d4921 18 grep {defined}
01265e2a 19 map { $_->init_arg() } $self->get_all_attributes()
5a0d4921 20 );
c001451a 21
01265e2a 22 return (
23 @source,
93a34553 24 'my %attrs = (' . ( join ' ', @attrs ) . ');',
01265e2a 25 'my @bad = sort grep { !$attrs{$_} } keys %{ $params };',
26 'if (@bad) {',
714128ef 27 'Moose->throw_error("Found unknown attribute(s) passed to the constructor: @bad");',
01265e2a 28 '}',
29 );
c001451a 30};
31
1ac1adaa 32# if the Object role is applied first, and then a superclass added, we just
33# lost our BUILDALL modification.
34after superclasses => sub
35{
36 my $self = shift;
37 return if not @_;
38 Moose::Util::MetaRole::apply_base_class_roles(
39 for => $self->name,
40 roles => ['MooseX::StrictConstructor::Role::Object'],
41 )
42};
43
c001451a 441;
58370717 45
0639c294 46# ABSTRACT: A role to make immutable constructors strict
47
58370717 48__END__
49
50=pod
51
58370717 52=head1 DESCRIPTION
53
01265e2a 54This role simply wraps C<_inline_BUILDALL()> (from
55C<Moose::Meta::Class>) so that immutable classes have a
fbfaa61f 56strict constructor.
58370717 57
58370717 58=cut