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