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
index e89b609..841811d 100644 (file)
@@ -4,13 +4,23 @@ use Moose::Role;
 around('_generate_slot_initializer', sub {
         my $orig = shift;
         my $self = shift;
-        my $attr = $self->_attributes->[$_[0]]->init_arg;
 
-        my $tolerant_code = 
-             qq# delete \$params->{'$attr'} unless # . 
-             qq# exists \$params->{'$attr'} && defined \$params->{'$attr'};\n#;
+        # note the key in the params may not match the attr name.
+        my $key_name = $self->_attributes->[$_[0]]->init_arg;
 
-        return $tolerant_code . $self->$orig(@_);
+        # insert a line of code at the start of the initializer,
+        # clearing the param if it's undefined.
+
+        if (defined $key_name) {
+                my $tolerant_code = 
+                     qq# delete \$params->{'$key_name'} unless # . 
+                     qq# exists \$params->{'$key_name'} && defined \$params->{'$key_name'};\n#;
+
+                return $tolerant_code . $self->$orig(@_);
+        }
+        else {
+                return $self->$orig(@_);
+        }
 });
 
 no Moose::Role;