Commit | Line | Data |
2d1c57bd |
1 | package MooseX::UndefTolerant::Constructor; |
2 | use Moose::Role; |
3 | |
d6ce838b |
4 | if ( $Moose::VERSION < 1.9900 ) { |
2dca5fcf |
5 | around('_generate_slot_initializer', sub { |
6 | my $orig = shift; |
7 | my $self = shift; |
8 | |
9 | # note the key in the params may not match the attr name. |
10 | my $key_name = $self->_attributes->[$_[0]]->init_arg; |
11 | |
12 | # insert a line of code at the start of the initializer, |
13 | # clearing the param if it's undefined. |
14 | |
15 | if (defined $key_name) |
16 | { |
17 | # leave the value unscathed if the attribute's type constraint can |
18 | # handle undef (or doesn't have one, which implicitly means it can) |
19 | my $type_constraint = $self->_attributes->[$_[0]]->type_constraint; |
20 | if ($type_constraint and not $type_constraint->check(undef)) |
21 | { |
22 | my $tolerant_code = |
23 | qq# delete \$params->{'$key_name'} unless # . |
24 | qq# exists \$params->{'$key_name'} && defined \$params->{'$key_name'};\n#; |
25 | |
26 | return $tolerant_code . $self->$orig(@_); |
27 | } |
28 | } |
29 | |
30 | return $self->$orig(@_); |
3991a5d2 |
31 | }); |
2d1c57bd |
32 | |
33 | no Moose::Role; |
2d1c57bd |
34 | 1; |