the canonical repository is now https://github.com/moose/MooseX-UndefTolerant
[gitmo/MooseX-UndefTolerant.git] / lib / MooseX / UndefTolerant / Constructor.pm
index e89b609..2bd102a 100644 (file)
@@ -1,18 +1,39 @@
 package MooseX::UndefTolerant::Constructor;
+
+# applied to constructor method metaclass, for Moose < 1.9900
+
 use Moose::Role;
 
-around('_generate_slot_initializer', sub {
-        my $orig = shift;
-        my $self = shift;
-        my $attr = $self->_attributes->[$_[0]]->init_arg;
+use strict;
+use warnings;
 
-        my $tolerant_code = 
-             qq# delete \$params->{'$attr'} unless # . 
-             qq# exists \$params->{'$attr'} && defined \$params->{'$attr'};\n#;
+around _generate_slot_initializer => sub {
+    my $orig = shift;
+    my $self = shift;
 
-        return $tolerant_code . $self->$orig(@_);
-});
+    # note the key in the params may not match the attr name.
+    my $key_name = $self->_attributes->[$_[0]]->init_arg;
 
-no Moose::Role;
+    # insert a line of code at the start of the initializer,
+    # clearing the param if it's undefined.
+
+    if (defined $key_name)
+    {
+        # leave the value unscathed if the attribute's type constraint can
+        # handle undef (or doesn't have one, which implicitly means it can)
+        my $type_constraint = $self->_attributes->[$_[0]]->type_constraint;
+        if ($type_constraint and not $type_constraint->check(undef))
+        {
+            my $tolerant_code =
+                qq# delete \$params->{'$key_name'} unless # .
+                qq# exists \$params->{'$key_name'} && defined \$params->{'$key_name'};\n#;
 
+            return $tolerant_code . $self->$orig(@_);
+        }
+    }
+
+    return $self->$orig(@_);
+};
+
+no Moose::Role;
 1;