do nothing at construction time for attributes with undef init_arg - fixes undefined...
[gitmo/MooseX-UndefTolerant.git] / lib / MooseX / UndefTolerant / Attribute.pm
index 15d35dd..644611e 100644 (file)
@@ -4,14 +4,23 @@ use Moose::Role;
 around('initialize_instance_slot', sub {
     my $orig = shift;
     my $self = shift;
-
-    my $ia = $self->init_arg;
-
-    # $_[2] is the hashref of options passed to the constructor. If our
-    # parameter passed in was undef, pop it off the args...
-    pop unless (defined $ia && defined($_[2]->{$ia}));
-
-    # Invoke the real init, as the above line cleared the unef
+    my ($meta_instance, $instance, $params) = @_;
+
+    my $key_name = $self->init_arg;
+
+    # If our parameter passed in was undef, remove it from the parameter list...
+    # but leave the value unscathed if the attribute's type constraint can
+    # handle undef (or doesn't have one, which implicitly means it can)
+    if (defined $key_name and not defined($params->{$key_name}))
+    {
+        my $type_constraint = $self->type_constraint;
+        if ($type_constraint and not $type_constraint->check(undef))
+        {
+            delete $params->{$key_name};
+        }
+    }
+
+    # Invoke the real init, as the above line cleared the undef param value
     $self->$orig(@_)
 });