rename a variable, to make it more clear that we are not working with the Moose:...
[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
8         # note the key in the params may not match the attr name.
9         my $key_name = $self->_attributes->[$_[0]]->init_arg;
10
11         # insert a line of code at the start of the initializer,
12         # clearing the param if it's undefined.
13
14         if (defined $key_name) {
15                 my $tolerant_code = 
16                      qq# delete \$params->{'$key_name'} unless # . 
17                      qq# exists \$params->{'$key_name'} && defined \$params->{'$key_name'};\n#;
18
19                 return $tolerant_code . $self->$orig(@_);
20         }
21         else {
22                 return $self->$orig(@_);
23         }
24 });
25
26 no Moose::Role;
27
28 1;