Version bump.
[gitmo/MooseX-UndefTolerant.git] / lib / MooseX / UndefTolerant.pm
1 package MooseX::UndefTolerant;
2
3 use Moose qw();
4 use Moose::Exporter;
5
6 use MooseX::UndefTolerant::Attribute;
7
8 our $VERSION = '0.05';
9
10 Moose::Exporter->setup_import_methods(
11     class_metaroles => { attribute => [ 'MooseX::UndefTolerant::Attribute' ] }
12 );
13
14 1;
15
16 __END__
17
18 =head1 NAME
19
20 MooseX::UndefTolerant - Make your attribute(s) tolerant to undef intitialization
21
22 =head1 SYNOPSIS
23
24   package My::Class;
25
26   use Moose;
27   use MooseX::UndefTolerant;
28
29   has 'name' => (
30     is => 'ro',
31     isa => 'Str',
32     predicate => 'has_name'
33   );
34
35   # Meanwhile, under the city...
36
37   # Doesn't explode
38   my $class = My::Class->new(name => undef);
39   $class->has_name # False!
40
41 Or, if you only want one attribute to have this behaviour:
42
43   package My:Class;
44   use Moose;
45
46   use MooseX::UndefTolerant::Attribute;
47
48   has 'bar' => (
49       traits => [ qw(MooseX::UndefTolerant::Attribute)],
50       is => 'ro',
51       isa => 'Num',
52       predicate => 'has_bar'
53   );
54
55 =head1 DESCRIPTION
56
57 Loading this module in your L<Moose> class makes initialization of your
58 attributes tolerant of undef.  If you specify the value of undef to any of
59 the attributes they will not be initialized.  Effectively behaving as if you
60 had not provided a value at all.
61
62 =head1 MOTIVATION
63
64 I often found myself in this quandry:
65
66   package My:Class;
67   use Moose;
68
69   has 'foo' => (
70     is => 'ro',
71     isa => 'Str',
72   );
73
74   # ... then
75
76   my $foo = ... # get the param from something
77
78   my $class = My:Class->new(foo => $foo, bar => 123);
79
80 What if foo is undefined?  I didn't want to change my attribute to be
81 Maybe[Str] and I still want my predicate (C<has_foo>) to work.  The only
82 real solution was:
83
84   if(defined($foo)) {
85     $class = My:CLass->new(foo => $foo, bar => 123);
86   } else {
87     $class = My:CLass->new(bar => 123);
88   }
89
90 Or some type of codemulch using ternarys.  This module allows you to make
91 your attributes more tolerant of undef so that you can keep the first
92 example: have your cake and eat it too!
93
94 =head1 PER ATTRIBUTE
95
96 =head1 AUTHOR
97
98 Cory G Watson, C<< <gphat at cpan.org> >>
99
100 =head1 ACKNOWLEDGEMENTS
101
102 Many thanks to the crew in #moose who talked me through this module:
103
104 Hans Dieter Pearcey (confound)
105
106 Jesse Luehrs (doy)
107
108 Tomas Doran (t0m)
109
110 Dylan Hardison (dylan)
111
112 Jay Shirley (jshirley)
113
114 Mike Eldridge (diz)
115
116 =head1 COPYRIGHT & LICENSE
117
118 Copyright 2009 Cory G Watson.
119
120 This program is free software; you can redistribute it and/or modify it
121 under the terms of either: the GNU General Public License as published
122 by the Free Software Foundation; or the Artistic License.
123
124 See http://dev.perl.org/licenses/ for more information.
125
126 =cut