Commit | Line | Data |
5447ee45 |
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 | # If the parameter passed in was undef, quietly do nothing but return |
9 | return unless defined($_->[2]); |
10 | |
11 | # If it was defined, call the real init slot method |
12 | $self->$orig(@_) |
13 | }); |
14 | |
19258058 |
15 | 1; |
16 | |
17 | =head1 NAME |
18 | |
19 | MooseX::UndefTolerant::Attribute - Make your attribute tolerant to undef intitialization |
20 | |
21 | =head1 SYNOPSIS |
22 | |
23 | package My:Class; |
24 | use Moose; |
25 | |
26 | use MooseX::UndefTolerant::Attribute; |
27 | |
28 | has 'bar' => ( |
29 | traits => [ qw(MooseX::UndefTolerant::Attribute)], |
30 | is => 'ro', |
31 | isa => 'Num', |
32 | predicate => 'has_bar' |
33 | ); |
34 | |
35 | # Meanwhile, under the city... |
36 | |
37 | # Doesn't explode |
38 | my $class = My::Class->new(bar => undef); |
39 | $class->has_bar # False! |
40 | |
41 | =head1 DESCRIPTION |
42 | |
43 | Applying this trait to your attribute makes it's initialization tolerant of |
44 | of undef. If you specify the value of undef to any of the attributes they |
45 | will not be initialized. Effectively behaving as if you had not provided a |
46 | value at all. |
47 | |
48 | =head1 AUTHOR |
49 | |
50 | Cory G Watson, C<< <gphat at cpan.org> >> |
51 | |
52 | =head1 COPYRIGHT & LICENSE |
53 | |
54 | Copyright 2009 Cory G Watson. |
55 | |
56 | This program is free software; you can redistribute it and/or modify it |
57 | under the terms of either: the GNU General Public License as published |
58 | by the Free Software Foundation; or the Artistic License. |
59 | |
60 | See http://dev.perl.org/licenses/ for more information. |
61 | |
62 | =cut |