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