do not use Undef-Tolerant behaviour on attributes that are capable of handling undef
[gitmo/MooseX-UndefTolerant.git] / lib / MooseX / UndefTolerant / Attribute.pm
1 package MooseX::UndefTolerant::Attribute;
2 use Moose::Role;
3
4 around('initialize_instance_slot', sub {
5     my $orig = shift;
6     my $self = shift;
7
8     my $key_name = $self->init_arg;
9
10     # $_[2] is the hashref of options passed to the constructor.
11     # If our parameter passed in was undef, pop it off the args...
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)
14     if (not defined $key_name or not defined($_[2]->{$key_name}))
15     {
16         my $type_constraint = $self->type_constraint;
17         if ($type_constraint and not $type_constraint->check(undef))
18         {
19             pop;
20         }
21     }
22
23     # Invoke the real init, as the above line cleared the undef
24     $self->$orig(@_)
25 });
26
27 1;
28
29 # ABSTRACT: Make your attribute(s) tolerant to undef intitialization
30
31 __END__
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
55 Applying this trait to your attribute makes it's initialization tolerant of
56 of undef.  If you specify the value of undef to any of the attributes they
57 will not be initialized (or will be set to the default, if applicable).
58 Effectively behaving as if you had not provided a value at all.
59
60 =cut