File Coverage

blib/lib/MooseX/UndefTolerant/Constructor.pm
Criterion Covered Total %
statement 12 12 100.0
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 16 16 100.0


line stmt bran cond sub pod time code
1             package MooseX::UndefTolerant::Constructor;
2              
3             our $VERSION = '0.21';
4              
5             # applied to constructor method metaclass, for Moose < 1.9900
6              
7 7     7   28 use Moose::Role;
  7         9  
  7         50  
8              
9 7     7   23915 use strict;
  7         10  
  7         127  
10 7     7   24 use warnings;
  7         7  
  7         1086  
11              
12             around _generate_slot_initializer => sub {
13             my $orig = shift;
14             my $self = shift;
15              
16             # note the key in the params may not match the attr name.
17             my $key_name = $self->_attributes->[$_[0]]->init_arg;
18              
19             # insert a line of code at the start of the initializer,
20             # clearing the param if it's undefined.
21              
22             if (defined $key_name)
23             {
24             # leave the value unscathed if the attribute's type constraint can
25             # handle undef (or doesn't have one, which implicitly means it can)
26             my $type_constraint = $self->_attributes->[$_[0]]->type_constraint;
27             if ($type_constraint and not $type_constraint->check(undef))
28             {
29             my $tolerant_code =
30             qq# delete \$params->{'$key_name'} unless # .
31             qq# exists \$params->{'$key_name'} && defined \$params->{'$key_name'};\n#;
32              
33             return $tolerant_code . $self->$orig(@_);
34             }
35             }
36              
37             return $self->$orig(@_);
38             };
39              
40 7     7   30 no Moose::Role;
  7         8  
  7         24  
41             1;