Commit | Line | Data |
5447ee45 |
1 | package MooseX::UndefTolerant::Attribute; |
2 | use Moose::Role; |
3 | |
efcfddbd |
4 | around('initialize_instance_slot', sub { |
5447ee45 |
5 | my $orig = shift; |
6 | my $self = shift; |
7 | |
efcfddbd |
8 | my $ia = $self->init_arg; |
9 | |
10 | # $_[2] is the hashref of options passed to the constructor. If our |
779ca481 |
11 | # parameter passed in was undef, pop it off the args... |
81d4422f |
12 | pop unless (defined $ia && defined($_[2]->{$ia})); |
5447ee45 |
13 | |
779ca481 |
14 | # Invoke the real init, as the above line cleared the unef |
5447ee45 |
15 | $self->$orig(@_) |
16 | }); |
17 | |
19258058 |
18 | 1; |
19 | |
b2c5b43c |
20 | # ABSTRACT: Make your attribute(s) tolerant to undef intitialization |
19258058 |
21 | |
b2c5b43c |
22 | __END__ |
19258058 |
23 | |
24 | =head1 SYNOPSIS |
25 | |
26 | package My:Class; |
27 | use Moose; |
28 | |
29 | use MooseX::UndefTolerant::Attribute; |
30 | |
31 | has 'bar' => ( |
32 | traits => [ qw(MooseX::UndefTolerant::Attribute)], |
33 | is => 'ro', |
34 | isa => 'Num', |
35 | predicate => 'has_bar' |
36 | ); |
37 | |
38 | # Meanwhile, under the city... |
39 | |
40 | # Doesn't explode |
41 | my $class = My::Class->new(bar => undef); |
42 | $class->has_bar # False! |
43 | |
44 | =head1 DESCRIPTION |
45 | |
46 | Applying this trait to your attribute makes it's initialization tolerant of |
47 | of undef. If you specify the value of undef to any of the attributes they |
b2c5b43c |
48 | will not be initialized (or will be set to the default, if applicable). |
779ca481 |
49 | Effectively behaving as if you had not provided a value at all. |
19258058 |
50 | |
19258058 |
51 | =cut |