blib/lib/DBIx/Class/InflateColumn/Boolean.pm

filter: Statementstmt 64.7% 44 / 68 Branchbran 61.5% 16 / 26 Conditioncond 66.6% 6 / 9 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 61.9% 13 / 21 Podpod 100% 1 / 1 totaltotal 62.0% 80 / 129 CC 17 SCAR 21.0
CC
17
Cov
64%
register_column
20.0
__ANON__
12.0
__ANON__
6.0
CRAP 8.2
↑ Summary
1 package DBIx::Class::InflateColumn::Boolean;
2  
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 ▶use base qw/DBIx::Class/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 ▶use Contextual::Return;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 DBIx::Class::InflateColumn::Boolean - Auto-create boolean objects from columns.
13  
14 =head1 VERSION
15  
16 Version 0.001001
17  
18 =cut
19  
20 3 our $VERSION = '0.002000';
21  
22 =head1 SYNOPSIS
23  
24 Load this component and declare columns as boolean values.
25  
26 package Table;
27 __PACKAGE__->load_components(qw/InflateColumn::Boolean Core/);
28 __PACKAGE__->table('table');
29 __PACKAGE__->true_is('Y');
30 __PACKAGE__->add_columns(
31 foo => {
32 data_type => 'varchar',
33 is_boolean => 1,
34 },
35 bar => {
36 data_type => 'varchar',
37 is_boolean => 1,
38 true_is => qr/^(?:yes|ja|oui|si)$/i,
39 },
40 baz => {
41 data_type => 'int',
42 is_boolean => 1,
43 false_is => ['0', '-1'],
44 },
45 );
46  
47 Then you can treat the specified column as a boolean:
48  
49 print 'table.foo is ', $table->foo ? 'true' : 'false', "\n";
50 print 'table.bar is ', $table->bar ? 'true' : 'false', "\n";
51  
52 The boolean object still stringifies to the actual field value:
53  
54 print $table->foo; # prints "Y" if it is true
55  
56 =head1 DESCRIPTION
57  
58 Perl does not have a native boolean data type by itself, it takes
59 certain several scalar values as C<false> (like '', 0, 0.0) as well as
60 empty lists and C<undef>, and everything else is C<true>. It is also
61 possible to set the boolean value of an object instance.
62  
63 As in most program code you have boolean data in nearly every database.
64 But for a database it is up to the designer to decide what is C<true>
65 and what is C<false>.
66  
67 This module maps such "database booleans" into "Perl booleans" and back
68 by inflating designated columns into some sort of boolean objects, that
69 happen to be instances of L<Contextual::Return::Value|Contextual::Return>.
70 Objects of this class can store the original value and a boolean meaning
71 at the same time. Therefore - if C<Yes> in the database means C<true>
72 and C<No> means C<false> in the application the following two lines can
73 virtually mean the same:
74  
75 if ($table->field eq "No") { ... }
76 if (not $table->field) { ... }
77  
78 That means that C<< $table->field >> has the scalar value C<"No">, but
79 is taken as C<false> in a boolean context, whereas without the little
80 magic from L<Contextual::Return|Contextual::Return> Perl would regard
81 the string C<"No"> as C<true>.
82  
83 When writing to the database, of course C<< $table->field >> would be
84 deflated to the original value C<"No"> and not some Perlish form of a
85 boolean.
86  
87 =head2 Important Notice
88  
89 It is strongly discouraged to assign a
90 L<Contextual::Return|Contextual::Return> object to a boolean field when
91 creating a fresh row, because:
92  
93 =over 4
94  
95 =item KISS (http://en.wikipedia.org/wiki/KISS_principle)
96  
97 Just say "No" when you mean it. It does not buy you anything to say
98 C<SCALAR {"No"} BOOL { 0 }>.
99  
100 =item Don't rely on the current boolean class
101  
102 Take the underlying boolean class as a black box. It might be replaced by
103 something other in future versions of this module.
104  
105 =back
106  
107 Simply assign the appropriate scalars to boolean fields ("Yes" or "No"
108 for the above example).
109  
110 =head2 Another Important Notice
111  
112 A database C<NULL> value is mapped to Perl's C<undef> and is never
113 inflated. Therefore C<NULL> is C<false> and this can not be altered.
114  
115 =head1 METHODS
116  
117 =head2 true_is
118  
119 __PACKAGE__->true_is('Y');
120 __PACKAGE__->true_is(['Y', 'y']);
121 __PACKAGE__->true_is(qr/^(y|yes|true|1)$/i);
122  
123 Gets/sets the possible values for C<true> data in this table.
124 Can be either a scalar, a reference to an array of scalars or a
125 regular expression (C<qr/.../>).
126  
127 The last line in the above example shows this package's default
128 for what is C<true> when neither C<true_is> nor L</false_is> are set.
129  
130 =head2 false_is
131  
132 __PACKAGE__->false_is('N');
133 __PACKAGE__->false_is(['N', 'n']);
134 __PACKAGE__->false_is(qr/^(n|no|false|0)$/i);
135  
136 Gets/sets the possible values for C<false> data in this table.
137 Can be either a scalar, a reference to an array of scalars or a
138 regular expression (C<qr/.../>).
139  
140 =cut
141  
142 3 __PACKAGE__->mk_group_accessors(inherited => qw/true_is false_is/);
143  
144 =head2 register_column
145  
146 Chains with L<DBIx::Class::Row/register_column>, and sets up boolean
147 columns appropriately. This would not normally be called directly by end
148 users.
149  
150 =cut
151  
152 sub register_column {
153 4 ▶ my ($self, $column, $info, @rest) = @_;
Subroutine register_column: called CC=11 SCAR=30.0
Pod documented
154 4 $self->next::method($column, $info, @rest);
155  
156 4 ▶ return unless defined $info->{'is_boolean'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $info->{"is_boolean"} 1 3 4
157  
158 3 my ($true_is, $false_is);
159 defined($true_is = $info->{true_is})
160 or defined($false_is = $info->{false_is})
161 3 ▶ or defined($true_is = $self->true_is)
Branch: 1 branch
BranchTrueFalseTotal
defined($true_is = $info->{"true_is"}) or defined($false_is = $info->{"false_is"}) or defined($true_is = $self->true_is) or defined($false_is = $self->false_is) or $true_is = qr/^(y|yes|true|1)$/i 0 3 3
Condition66%6/9
defined($true_is = $info->{"true_is"}) or defined($false_is = $info->{"false_is"}) or defined($true_is = $self->true_is) or defined($false_is = $self->false_is)
l!l&&r!l&&!r
300
defined($true_is = $info->{"true_is"}) or defined($false_is = $info->{"false_is"}) or defined($true_is = $self->true_is)
l!l&&r!l&&!r
210
defined($true_is = $info->{"true_is"}) or defined($false_is = $info->{"false_is"})
l!l&&r!l&&!r
111
MC/DC0%0/4
defined($true_is = $info->{"true_is"}) or defined($false_is = $info->{"false_is"}) or defined($true_is = $self->true_is) or defined($false_is = $self->false_is)
defined($true_is = $info->{"true_is"}) defined($false_is = $info->{"false_is"}) defined($true_is = $self->true_is) defined($false_is = $self->false_is)
Truth table60%3/5
A or B or C or D
A= defined($true_is = $info->{"true_is"})B= defined($false_is = $info->{"false_is"})C= defined($true_is = $self->true_is)D= defined($false_is = $self->false_is)
ABCDresult
1XXX1
01XX1
001X1
00011
00000
162 or defined($false_is = $self->false_is)
163 or $true_is = qr/^(y|yes|true|1)$/i;
164  
165 3 my $ref;
166 3 ▶ if (defined $false_is) { # column is false-specific
Branch: 1 branch
BranchTrueFalseTotal
if (defined $false_is) { } 1 2 3
167 1 $ref = ref $false_is;
168 $self->inflate_column(
169 $column => {
170 inflate =>
171 $ref eq '' ?
172 sub {
173 0 ▶ my $x = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
174 0 ▶ BLESSED { 'Contextual::Return::Value' } SCALAR { $x } BOOL { $x eq $false_is ? 0 : 1 };
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$x eq $false_is ? : 0 0 0
175 } :
176 $ref eq 'ARRAY' ?
177 sub {
178 2 ▶ my $x = shift;
Subroutine __ANON__: called CC=3 SCAR=11.1
179 2 for (@$false_is) {
180 1 ▶ return BLESSED { 'Contextual::Return::Value' } SCALAR { $x } BOOL { 0 }
Subroutine __ANON__: called CC=1 SCAR=0.4
181 4 ▶ if $x eq $_;
Branch: 1 branch
BranchTrueFalseTotal
if $x eq $_ 1 3 4
182 }
183 1 ▶ BLESSED { 'Contextual::Return::Value' } SCALAR { $x } BOOL { 1 };
Subroutine __ANON__: called CC=1 SCAR=0.2
184 } :
185 # $ref eq 'Regexp'
186 sub {
187 0 ▶ my $x = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
188 0 ▶ BLESSED { 'Contextual::Return::Value' } SCALAR { $x } BOOL { $x =~ $false_is ? 0 : 1 };
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$x =~ /$false_is/ ? : 0 0 0
189 },
190 0 ▶ deflate => sub { shift },
Subroutine __ANON__: not called CC=1 SCAR=6.9
191 }
192 1 ▶ );
Branch: 2 branches
BranchTrueFalseTotal
$ref eq '' ? : 0 1 1
$ref eq 'ARRAY' ? : 1 0 1
193 }
194 else { # column is true-specific
195 2 $ref = ref $true_is;
196 $self->inflate_column(
197 $column => {
198 inflate =>
199 $ref eq '' ?
200 sub {
201 2 ▶ my $x = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
202 2 ▶ BLESSED { 'Contextual::Return::Value' } SCALAR { $x } BOOL { $x eq $true_is ? 1 : 0 };
Subroutine __ANON__: called CC=2 SCAR=7.0
Branch: 1 branch
BranchTrueFalseTotal
$x eq $true_is ? : 1 1 2
203 } :
204 $ref eq 'ARRAY' ?
205 sub {
206 0 ▶ my $x = shift;
Subroutine __ANON__: not called CC=3 SCAR=24.8
207 0 for (@$true_is) {
208 0 ▶ return BLESSED { 'Contextual::Return::Value' } SCALAR { $x } BOOL { 1 }
Subroutine __ANON__: not called CC=1 SCAR=6.9
209 0 ▶ if $x eq $_;
Branch: 1 branch
BranchTrueFalseTotal
if $x eq $_ 0 0 0
210 }
211 0 ▶ BLESSED { 'Contextual::Return::Value' } SCALAR { $x } BOOL { 0 };
Subroutine __ANON__: not called CC=1 SCAR=6.9
212 } :
213 # $ref eq 'Regexp'
214 sub {
215 3 ▶ my $x = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
216 3 ▶ BLESSED { 'Contextual::Return::Value' } SCALAR { $x } BOOL { $x =~ $true_is ? 1 : 0 };
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
$x =~ /$true_is/ ? : 2 1 3
217 },
218 1 ▶ deflate => sub { shift },
Subroutine __ANON__: called CC=1 SCAR=0.0
219 }
220 2 ▶ );
Branch: 2 branches
BranchTrueFalseTotal
$ref eq '' ? : 1 1 2
$ref eq 'ARRAY' ? : 0 1 1
221 }
222 }
223  
224 3 1;
225  
226 __END__
↑ Summary