Jeez, I hate misnaming.
[gitmo/MooseX-UndefTolerant.git] / lib / MooseX / UndefTolerant / Attribute.pm
CommitLineData
5447ee45 1package MooseX::UndefTolerant::Attribute;
2use Moose::Role;
3
4around('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 151;
16
17=head1 NAME
18
19MooseX::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
43Applying this trait to your attribute makes it's initialization tolerant of
44of undef. If you specify the value of undef to any of the attributes they
45will not be initialized. Effectively behaving as if you had not provided a
46value at all.
47
48=head1 AUTHOR
49
50Cory G Watson, C<< <gphat at cpan.org> >>
51
52=head1 COPYRIGHT & LICENSE
53
54Copyright 2009 Cory G Watson.
55
56This program is free software; you can redistribute it and/or modify it
57under the terms of either: the GNU General Public License as published
58by the Free Software Foundation; or the Artistic License.
59
60See http://dev.perl.org/licenses/ for more information.
61
62=cut