feda0ea3a1cee469758978982683c38044fd7ae8
[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::Class;
8 use MooseX::UndefTolerant::Constructor;
9
10
11 my %metaroles = ( attribute => [ 'MooseX::UndefTolerant::Attribute' ] );
12 if ( $Moose::VERSION < 1.9900 ) {
13         $metaroles{constructor} = [ 'MooseX::UndefTolerant::Constructor' ];
14 }
15 else {
16         $metaroles{class} = [ 'MooseX::UndefTolerant::Class' ];
17 }
18
19
20 Moose::Exporter->setup_import_methods(
21     class_metaroles => \%metaroles,
22 );
23
24 1;
25
26 # ABSTRACT: Make your attribute(s) tolerant to undef initialization
27
28 __END__
29
30 =head1 SYNOPSIS
31
32   package My::Class;
33
34   use Moose;
35   use MooseX::UndefTolerant;
36
37   has 'name' => (
38     is => 'ro',
39     isa => 'Str',
40     predicate => 'has_name'
41   );
42
43   # Meanwhile, under the city...
44
45   # Doesn't explode
46   my $class = My::Class->new(name => undef);
47   $class->has_name # False!
48
49 Or, if you only want one attribute to have this behaviour:
50
51   package My:Class;
52   use Moose;
53
54   use MooseX::UndefTolerant::Attribute;
55
56   has 'bar' => (
57       traits => [ qw(MooseX::UndefTolerant::Attribute)],
58       is => 'ro',
59       isa => 'Num',
60       predicate => 'has_bar'
61   );
62
63 =head1 DESCRIPTION
64
65 Loading this module in your L<Moose> class makes initialization of your
66 attributes tolerant of undef.  If you specify the value of undef to any of
67 the attributes they will not be initialized, effectively behaving as if you
68 had not provided a value at all.
69
70 =head1 MOTIVATION
71
72 I often found myself in this quandry:
73
74   package My:Class;
75   use Moose;
76
77   has 'foo' => (
78     is => 'ro',
79     isa => 'Str',
80   );
81
82   # ... then
83
84   my $foo = ... # get the param from something
85
86   my $class = My:Class->new(foo => $foo, bar => 123);
87
88 What if foo is undefined?  I didn't want to change my attribute to be
89 Maybe[Str] and I still want my predicate (C<has_foo>) to work.  The only
90 real solution was:
91
92   if(defined($foo)) {
93     $class = My:Class->new(foo => $foo, bar => 123);
94   } else {
95     $class = My:Class->new(bar => 123);
96   }
97
98 Or some type of codemulch using ternarys.  This module allows you to make
99 your attributes more tolerant of undef so that you can keep the first
100 example: have your cake and eat it too!
101
102 =head1 PER ATTRIBUTE
103
104 See L<MooseX::UndefTolerant::Attribute>.
105
106 =head1 CAVEATS
107
108 This extension does not currently work in immutable classes when applying the
109 trait to some (but not all) attributes in the class. This is because the
110 inlined constructor initialization code currently lives in
111 L<Moose::Meta::Method::Constructor>, not L<Moose::Meta::Attribute>. The good
112 news is that this is expected to be changing shortly.
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 =cut