47d81283c857615c8a5846794279d4b4bc094e85
[gitmo/MooseX-UndefTolerant.git] / lib / MooseX / UndefTolerant / Constructor.pm
1 package MooseX::UndefTolerant::Constructor;
2 use Moose::Role;
3
4 around('_generate_slot_initializer', sub {
5         my $orig = shift;
6         my $self = shift;
7         my $attr = $self->_attributes->[$_[0]]->init_arg;
8
9         # insert a line of code at the start of the initializer,
10         # clearing the param if it's undefined.
11
12         if (defined $attr) {
13                 my $tolerant_code = 
14                      qq# delete \$params->{'$attr'} unless # . 
15                      qq# exists \$params->{'$attr'} && defined \$params->{'$attr'};\n#;
16
17                 return $tolerant_code . $self->$orig(@_);
18         }
19         else {
20                 return $self->$orig(@_);
21         }
22 });
23
24 no Moose::Role;
25
26 1;