clean undef-tolerant and undefined args in BUILDARGS
[gitmo/MooseX-UndefTolerant.git] / lib / MooseX / UndefTolerant / Object.pm
1 package MooseX::UndefTolerant::Object;
2
3 # applied to class.
4
5 use strict;
6 use warnings;
7
8 use Moose::Role;
9
10 around BUILDARGS => sub {
11     my ($orig, $class, @args) = @_;
12
13     my $args = $class->$orig(@args);
14
15     my @delete_keys = grep
16     {
17         defined $_->init_arg
18         and exists $args->{$_->init_arg}
19         and not defined $args->{$_->init_arg}
20         and do {
21             my $type_constraint = $_->type_constraint;
22             $type_constraint and not $type_constraint->check(undef)
23         }
24     } Moose::Util::find_meta($class)->get_all_attributes();
25
26     delete @{$args}{@delete_keys} if @delete_keys;
27     return $args;
28 };
29
30 1;