the canonical repository is now https://github.com/moose/MooseX-UndefTolerant
[gitmo/MooseX-UndefTolerant.git] / lib / MooseX / UndefTolerant / Attribute.pm
CommitLineData
5447ee45 1package MooseX::UndefTolerant::Attribute;
2use Moose::Role;
3
efcfddbd 4around('initialize_instance_slot', sub {
5447ee45 5 my $orig = shift;
6 my $self = shift;
95f91970 7 my ($meta_instance, $instance, $params) = @_;
5447ee45 8
3991a5d2 9 my $key_name = $self->init_arg;
10
95f91970 11 # If our parameter passed in was undef, remove it from the parameter list...
3991a5d2 12 # but leave the value unscathed if the attribute's type constraint can
13 # handle undef (or doesn't have one, which implicitly means it can)
0601096c 14 if (defined $key_name and not defined($params->{$key_name}))
3991a5d2 15 {
16 my $type_constraint = $self->type_constraint;
17 if ($type_constraint and not $type_constraint->check(undef))
18 {
95f91970 19 delete $params->{$key_name};
3991a5d2 20 }
21 }
22
0601096c 23 # Invoke the real init, as the above line cleared the undef param value
5447ee45 24 $self->$orig(@_)
25});
26
19258058 271;
28
b2c5b43c 29# ABSTRACT: Make your attribute(s) tolerant to undef intitialization
19258058 30
b2c5b43c 31__END__
19258058 32
33=head1 SYNOPSIS
34
35 package My:Class;
36 use Moose;
37
38 use MooseX::UndefTolerant::Attribute;
39
40 has 'bar' => (
41 traits => [ qw(MooseX::UndefTolerant::Attribute)],
42 is => 'ro',
43 isa => 'Num',
44 predicate => 'has_bar'
45 );
46
47 # Meanwhile, under the city...
48
49 # Doesn't explode
50 my $class = My::Class->new(bar => undef);
51 $class->has_bar # False!
52
53=head1 DESCRIPTION
54
55Applying this trait to your attribute makes it's initialization tolerant of
56of undef. If you specify the value of undef to any of the attributes they
b2c5b43c 57will not be initialized (or will be set to the default, if applicable).
779ca481 58Effectively behaving as if you had not provided a value at all.
19258058 59
19258058 60=cut