package MooseX::UndefTolerant::Object;

# applied to class.

use strict;
use warnings;

use Moose::Role;

around BUILDARGS => sub {
    my ($orig, $class, @args) = @_;

    my $args = $class->$orig(@args);

    my @delete_keys = grep
    {
        defined $_->init_arg
        and exists $args->{$_->init_arg}
        and not defined $args->{$_->init_arg}
        and do {
            my $type_constraint = $_->type_constraint;
            $type_constraint and not $type_constraint->check(undef)
        }
    } Moose::Util::find_meta($class)->get_all_attributes();

    delete @{$args}{@delete_keys} if @delete_keys;
    return $args;
};

1;
