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 | |
5447ee45 |
10 | |
d6ce838b |
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 | |
e92df8d4 |
20 | Moose::Exporter->setup_import_methods( |
d6ce838b |
21 | class_metaroles => \%metaroles, |
e92df8d4 |
22 | ); |
5447ee45 |
23 | |
24 | 1; |
25 | |
b2c5b43c |
26 | # ABSTRACT: Make your attribute(s) tolerant to undef initialization |
5447ee45 |
27 | |
b2c5b43c |
28 | __END__ |
5447ee45 |
29 | |
30 | =head1 SYNOPSIS |
31 | |
19258058 |
32 | package My::Class; |
5447ee45 |
33 | |
19258058 |
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 |
94f9d198 |
67 | the attributes they will not be initialized, effectively behaving as if you |
19258058 |
68 | had not provided a value at all. |
69 | |
1686337d |
70 | You can also apply the 'UndefTolerant' trait to individual attributes. See |
793ea91a |
71 | L<MooseX::UndefTolerant::Attribute> for details. |
72 | |
73 | There will be no change in behaviour to any attribute with a type constraint |
74 | that accepts undef values (for example C<Maybe> types), as it is presumed that |
75 | since the type is already "undef tolerant", there is no need to avoid |
76 | initializing the attribute value with C<undef>. |
77 | |
19258058 |
78 | =head1 MOTIVATION |
79 | |
80 | I often found myself in this quandry: |
81 | |
82 | package My:Class; |
83 | use Moose; |
84 | |
85 | has 'foo' => ( |
86 | is => 'ro', |
87 | isa => 'Str', |
88 | ); |
89 | |
90 | # ... then |
91 | |
92 | my $foo = ... # get the param from something |
93 | |
94 | my $class = My:Class->new(foo => $foo, bar => 123); |
95 | |
96 | What if foo is undefined? I didn't want to change my attribute to be |
97 | Maybe[Str] and I still want my predicate (C<has_foo>) to work. The only |
98 | real solution was: |
99 | |
100 | if(defined($foo)) { |
1cbb963b |
101 | $class = My:Class->new(foo => $foo, bar => 123); |
19258058 |
102 | } else { |
1cbb963b |
103 | $class = My:Class->new(bar => 123); |
19258058 |
104 | } |
105 | |
106 | Or some type of codemulch using ternarys. This module allows you to make |
107 | your attributes more tolerant of undef so that you can keep the first |
108 | example: have your cake and eat it too! |
109 | |
110 | =head1 PER ATTRIBUTE |
5447ee45 |
111 | |
1cbb963b |
112 | See L<MooseX::UndefTolerant::Attribute>. |
113 | |
94f9d198 |
114 | =head1 CAVEATS |
115 | |
116 | This extension does not currently work in immutable classes when applying the |
117 | trait to some (but not all) attributes in the class. This is because the |
118 | inlined constructor initialization code currently lives in |
119 | L<Moose::Meta::Method::Constructor>, not L<Moose::Meta::Attribute>. The good |
120 | news is that this is expected to be changing shortly. |
121 | |
5447ee45 |
122 | =head1 ACKNOWLEDGEMENTS |
123 | |
19258058 |
124 | Many thanks to the crew in #moose who talked me through this module: |
125 | |
5447ee45 |
126 | Hans Dieter Pearcey (confound) |
127 | |
128 | Jesse Luehrs (doy) |
129 | |
130 | Tomas Doran (t0m) |
131 | |
132 | Dylan Hardison (dylan) |
133 | |
134 | Jay Shirley (jshirley) |
135 | |
136 | Mike Eldridge (diz) |
137 | |
5447ee45 |
138 | =cut |