File Coverage

blib/lib/MooseX/UndefTolerant.pm
Criterion Covered Total %
statement 21 21 100.0
branch n/a
condition n/a
subroutine 7 7 100.0
pod n/a
total 28 28 100.0


line stmt bran cond sub pod time code
1             package MooseX::UndefTolerant; # git description: v0.20-6-ga1774a5
2              
3             our $VERSION = '0.21';
4              
5 7     7   1856931 use strict;
  7         13  
  7         187  
6 7     7   26 use warnings;
  7         8  
  7         204  
7              
8 7     7   464 use Moose 0.89 qw();
  7         295054  
  7         134  
9 7     7   28 use Moose::Exporter;
  7         9  
  7         42  
10              
11 7     7   2599 use MooseX::UndefTolerant::Attribute;
  7         16  
  7         217  
12 7     7   2701 use MooseX::UndefTolerant::Class;
  7         13  
  7         192  
13 7     7   2619 use MooseX::UndefTolerant::Constructor;
  7         27  
  7         687  
14              
15              
16             my %metaroles = (
17             class_metaroles => {
18             attribute => [ 'MooseX::UndefTolerant::Attribute' ],
19             }
20             );
21             if ( $Moose::VERSION < 1.9900 ) {
22             $metaroles{class_metaroles}{constructor} = [
23             'MooseX::UndefTolerant::Constructor',
24             ];
25             }
26             else {
27             $metaroles{class_metaroles}{class} = [
28             'MooseX::UndefTolerant::Class',
29             ];
30             $metaroles{role_metaroles} = {
31             applied_attribute => [
32             'MooseX::UndefTolerant::Attribute',
33             ],
34             role => [
35             'MooseX::UndefTolerant::Role',
36             ],
37             application_to_class => [
38             'MooseX::UndefTolerant::ApplicationToClass',
39             ],
40             application_to_role => [
41             'MooseX::UndefTolerant::ApplicationToRole',
42             ],
43             };
44             }
45              
46              
47             Moose::Exporter->setup_import_methods(%metaroles);
48              
49             1;
50              
51             # ABSTRACT: Make your attribute(s) tolerant to undef initialization
52              
53             __END__
54              
55             =pod
56              
57             =encoding UTF-8
58              
59             =head1 NAME
60              
61             MooseX::UndefTolerant - Make your attribute(s) tolerant to undef initialization
62              
63             =head1 VERSION
64              
65             version 0.21
66              
67             =head1 SYNOPSIS
68              
69             package My::Class;
70              
71             use Moose;
72             use MooseX::UndefTolerant;
73              
74             has 'name' => (
75             is => 'ro',
76             isa => 'Str',
77             predicate => 'has_name'
78             );
79              
80             # Meanwhile, under the city...
81              
82             # Doesn't explode
83             my $class = My::Class->new(name => undef);
84             $class->has_name # False!
85              
86             Or, if you only want one attribute to have this behaviour:
87              
88             package My:Class;
89             use Moose;
90              
91             use MooseX::UndefTolerant::Attribute;
92              
93             has 'bar' => (
94             traits => [ qw(MooseX::UndefTolerant::Attribute)],
95             is => 'ro',
96             isa => 'Num',
97             predicate => 'has_bar'
98             );
99              
100             =head1 DESCRIPTION
101              
102             Loading this module in your L<Moose> class makes initialization of your
103             attributes tolerant of undef. If you specify the value of undef to any of
104             the attributes they will not be initialized, effectively behaving as if you
105             had not provided a value at all.
106              
107             You can also apply the 'UndefTolerant' trait to individual attributes. See
108             L<MooseX::UndefTolerant::Attribute> for details.
109              
110             There will be no change in behaviour to any attribute with a type constraint
111             that accepts undef values (for example C<Maybe> types), as it is presumed that
112             since the type is already "undef tolerant", there is no need to avoid
113             initializing the attribute value with C<undef>.
114              
115             As of Moose 1.9900, this module can also be used in a role, in which case all
116             of that role's attributes will be undef-tolerant.
117              
118             =head1 MOTIVATION
119              
120             I often found myself in this quandary:
121              
122             package My:Class;
123             use Moose;
124              
125             has 'foo' => (
126             is => 'ro',
127             isa => 'Str',
128             );
129              
130             # ... then
131              
132             my $foo = ... # get the param from something
133              
134             my $class = My:Class->new(foo => $foo, bar => 123);
135              
136             What if foo is undefined? I didn't want to change my attribute to be
137             Maybe[Str] and I still want my predicate (C<has_foo>) to work. The only
138             real solution was:
139              
140             if(defined($foo)) {
141             $class = My:Class->new(foo => $foo, bar => 123);
142             } else {
143             $class = My:Class->new(bar => 123);
144             }
145              
146             =for stopwords codemulch
147              
148             Or some type of codemulch using ternary conditionals. This module allows you
149             to make your attributes more tolerant of undef so that you can keep the first
150             example: have your cake and eat it too!
151              
152             =head1 PER ATTRIBUTE
153              
154             See L<MooseX::UndefTolerant::Attribute>.
155              
156             =head1 CAVEATS
157              
158             This extension does not currently work in immutable classes when applying the
159             trait to some (but not all) attributes in the class. This is because the
160             inlined constructor initialization code currently lives in
161             L<Moose::Meta::Class>, not L<Moose::Meta::Attribute>. The good news is that
162             this is expected to be changing shortly.
163              
164             =head1 ACKNOWLEDGEMENTS
165              
166             Many thanks to the crew in #moose who talked me through this module:
167              
168             =for stopwords Doran Eldridge Hardison Pearcey diz dylan jshirley
169              
170             =over 4
171              
172             Hans Dieter Pearcey (confound)
173             Jesse Luehrs (doy)
174             Tomas Doran (t0m)
175             Dylan Hardison (dylan)
176             Jay Shirley (jshirley)
177             Mike Eldridge (diz)
178              
179             =back
180              
181             =head1 SUPPORT
182              
183             Bugs may be submitted through L<the RT bug tracker|https://rt.cpan.org/Public/Dist/Display.html?Name=MooseX-UndefTolerant>
184             (or L<bug-MooseX-UndefTolerant@rt.cpan.org|mailto:bug-MooseX-UndefTolerant@rt.cpan.org>).
185              
186             There is also a mailing list available for users of this distribution, at
187             L<http://lists.perl.org/list/moose.html>.
188              
189             There is also an irc channel available for users of this distribution, at
190             L<C<#moose> on C<irc.perl.org>|irc://irc.perl.org/#moose>.
191              
192             =head1 AUTHOR
193              
194             Cory G Watson <gphat at cpan.org>
195              
196             =head1 CONTRIBUTORS
197              
198             =for stopwords Karen Etheridge Dave Rolsky Cory G Watson Jesse Luehrs Chris Andrews
199              
200             =over 4
201              
202             =item *
203              
204             Karen Etheridge <ether@cpan.org>
205              
206             =item *
207              
208             Dave Rolsky <autarch@urth.org>
209              
210             =item *
211              
212             Cory G Watson <gphat@cpan.org>
213              
214             =item *
215              
216             Jesse Luehrs <doy@tozt.net>
217              
218             =item *
219              
220             Chris Andrews <chrisandrews@venda.com>
221              
222             =back
223              
224             =head1 COPYRIGHT AND LICENSE
225              
226             This software is copyright (c) 2011 by Cory G Watson.
227              
228             This is free software; you can redistribute it and/or modify it under
229             the same terms as the Perl 5 programming language system itself.
230              
231             =cut