Dep twiddling
[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;
7
efcfddbd 8 my $ia = $self->init_arg;
9
10 # $_[2] is the hashref of options passed to the constructor. If our
11 # parameter passed in was undef, quietly do nothing but return.
12 return unless exists($_[2]->{$ia}) && defined($_[2]->{$ia});
5447ee45 13
14 # If it was defined, call the real init slot method
15 $self->$orig(@_)
16});
17
19258058 181;
19
20=head1 NAME
21
22MooseX::UndefTolerant::Attribute - Make your attribute tolerant to undef intitialization
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
46Applying this trait to your attribute makes it's initialization tolerant of
47of undef. If you specify the value of undef to any of the attributes they
48will not be initialized. Effectively behaving as if you had not provided a
49value at all.
50
51=head1 AUTHOR
52
53Cory G Watson, C<< <gphat at cpan.org> >>
54
55=head1 COPYRIGHT & LICENSE
56
57Copyright 2009 Cory G Watson.
58
59This program is free software; you can redistribute it and/or modify it
60under the terms of either: the GNU General Public License as published
61by the Free Software Foundation; or the Artistic License.
62
63See http://dev.perl.org/licenses/ for more information.
64
65=cut