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
d6932ec6 3# applied as class_metaroles => { class => [ __PACKAGE__ ] }, for Moose 1.99x and later
4
d99e6f32 5use Moose::Role;
6
7use namespace::autoclean;
c001451a 8
79b37c7d 9use B ();
c001451a 10
01265e2a 11around '_inline_BUILDALL' => sub {
64c958ef 12 my $orig = shift;
c001451a 13 my $self = shift;
14
01265e2a 15 my @source = $self->$orig();
c001451a 16
5a0d4921 17 my @attrs = (
df9653e6 18 '__INSTANCE__ => 1,',
79b37c7d 19 map { B::perlstring($_) . ' => 1,' }
5a0d4921 20 grep {defined}
01265e2a 21 map { $_->init_arg() } $self->get_all_attributes()
5a0d4921 22 );
c001451a 23
01265e2a 24 return (
25 @source,
93a34553 26 'my %attrs = (' . ( join ' ', @attrs ) . ');',
01265e2a 27 'my @bad = sort grep { !$attrs{$_} } keys %{ $params };',
28 'if (@bad) {',
675cf837 29 'Moose->throw_error("Found unknown attribute(s) passed to the constructor: @bad");',
01265e2a 30 '}',
31 );
c001451a 32};
33
82b1f9fb 34# if the Object role is applied first, and then a superclass added, we just
35# lost our BUILDALL modification.
36after superclasses => sub
37{
38 my $self = shift;
39 return if not @_;
40 Moose::Util::MetaRole::apply_base_class_roles(
41 for => $self->name,
42 roles => ['MooseX::StrictConstructor::Role::Object'],
43 )
44};
45
c001451a 461;
58370717 47
0639c294 48# ABSTRACT: A role to make immutable constructors strict
49
58370717 50__END__
51
52=pod
53
58370717 54=head1 DESCRIPTION
55
01265e2a 56This role simply wraps C<_inline_BUILDALL()> (from
57C<Moose::Meta::Class>) so that immutable classes have a
fbfaa61f 58strict constructor.
58370717 59
58370717 60=cut