Style tweak
[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
bf857f82 32# If the base class role is applied first, and then a superclass is added, we
33# lose the role.
34after superclasses => sub {
1ac1adaa 35 my $self = shift;
7e1ef712 36
37 return unless @_;
38
1ac1adaa 39 Moose::Util::MetaRole::apply_base_class_roles(
bf857f82 40 for => $self->name,
1ac1adaa 41 roles => ['MooseX::StrictConstructor::Role::Object'],
bf857f82 42 );
1ac1adaa 43};
44
c001451a 451;
58370717 46
0639c294 47# ABSTRACT: A role to make immutable constructors strict
48
58370717 49__END__
50
51=pod
52
58370717 53=head1 DESCRIPTION
54
01265e2a 55This role simply wraps C<_inline_BUILDALL()> (from
56C<Moose::Meta::Class>) so that immutable classes have a
fbfaa61f 57strict constructor.
58370717 58
58370717 59=cut