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