DBI/Filter/Boolean.pm

filter: Statementstmt 89.8% 44 / 49 Branchbran 85.0% 17 / 20 Conditioncond 75.0% 6 / 8 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 100% 10 / 10 Podpod 100% 2 / 2 totaltotal 87.1% 81 / 93 CC 16 SCAR 15.6
CC
16
Cov
87%
encode
8.9
decode
4.2
_is_false
4.0
CRAP 4.8
« DBI/Column.pm ↑ Summary DBI/Record/Plugin.pm »
1 package Jifty::DBI::Filter::Boolean;
2  
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use base 'Jifty::DBI::Filter';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use constant TRUE_VALUES => qw(1 t true y yes TRUE);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use constant FALSE_VALUES => ('', qw(0 f false n no FALSE));
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 sub _is_true {
12 151 ▶ my $self = shift;
Subroutine _is_true: called CC=3 SCAR=11.0
13 151 my $value = shift;
14  
15 1 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 151 for ($self->TRUE_VALUES, map { "'$_'" } $self->TRUE_VALUES) {
18 1072 ▶ return 1 if $value eq $_;
Branch: 1 branch
BranchTrueFalseTotal
if $value eq $_ 74 998 1072
19 }
20  
21 77 return 0;
22 }
23  
24 sub _is_false {
25 77 ▶ my $self = shift;
Subroutine _is_false: called CC=4 SCAR=13.9
26 77 my $value = shift;
27  
28 77 ▶ return 1 if not defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 1 76 77
29  
30 76 for ($self->FALSE_VALUES, map { "'$_'" } $self->FALSE_VALUES) {
31 244 ▶ return 1 if $value eq $_;
Branch: 1 branch
BranchTrueFalseTotal
if $value eq $_ 76 168 244
32 }
33  
34 0 return 0;
35 }
36  
37 =head1 NAME
38  
39 Jifty::DBI::Filter::Boolean - Encodes booleans
40  
41 =head1 DESCRIPTION
42  
43 =head2 decode
44  
45 Transform the value into 1 or 0 so Perl's concept of the value agrees
46 with the database's concept of the value. (For example, 't' and 'f'
47 might be used in the database, but 'f' is true in Perl)
48  
49 =cut
50  
51 sub decode {
52 56 ▶ my $self = shift;
Subroutine decode: called CC=4 SCAR=14.2
Pod documented
53 56 my $value_ref = $self->value_ref;
54  
55 56 ▶ return unless defined $$value_ref;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $$value_ref 13 43 56
56  
57 43 ▶ if ($self->_is_true($$value_ref)) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->_is_true($$value_ref)) { } 21 22 43
elsif ($self->_is_false($$value_ref)) { } 22 0 22
58 21 $$value_ref = 1;
59 }
60 elsif ($self->_is_false($$value_ref)) {
61 22 $$value_ref = 0;
62 }
63 else {
64 0 $self->handle->log("The value '$$value_ref' does not look like a boolean. Defaulting to false.");
65 0 $$value_ref = 0;
66 }
67 }
68  
69 =head2 encode
70  
71 Transform the value to the canonical true or false value as expected by the
72 database.
73  
74 =cut
75  
76 sub encode {
77 122 ▶ my $self = shift;
Subroutine encode: called CC=8 SCAR=21.8
Pod documented
78 122 my $value_ref = $self->value_ref;
79  
80 122 ▶ return unless defined($$value_ref) or $self->column->mandatory;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $$value_ref or $self->column->mandatory 14 108 122
Condition100%3/3
defined $$value_ref or $self->column->mandatory
l!l&&r!l&&!r
107114
MC/DC100%2/2
defined $$value_ref or $self->column->mandatory
defined $$value_ref $self->column->mandatory
Truth table100%3/3
A or B
A= defined $$value_refB= $self->column->mandatory
ABresult
1X1
011
000
81 108 ▶ return if uc($$value_ref||'') eq "NULL" and not $self->column->mandatory;
Branch: 1 branch
BranchTrueFalseTotal
if uc($$value_ref || "") eq "NULL" and not $self->column->mandatory 0 108 108
Condition60%3/5
uc($$value_ref || "") eq "NULL" and not $self->column->mandatory
!ll&&!rl&&r
10800
$$value_ref || ''
l!l
8919
MC/DC0%0/2
uc($$value_ref || "") eq "NULL" and not $self->column->mandatory
$$value_ref not $self->column->mandatory
Truth table33%1/3
A and not(B)
A= $$value_refB= not $self->column->mandatory
ABresult
0X0
100
111
82  
83 108 ▶ if ($self->_is_true($$value_ref)) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->_is_true($$value_ref)) { } 53 55 108
elsif ($self->_is_false($$value_ref)) { } 55 0 55
84 53 $$value_ref = $self->handle->canonical_true;
85 }
86 elsif ($self->_is_false($$value_ref)) {
87 55 $$value_ref = $self->handle->canonical_false;
88 }
89 else {
90 0 $self->handle->log("The value '$$value_ref' does not look like a boolean. Defaulting to false.");
91 0 $$value_ref = $self->handle->canonical_false;
92 }
93 }
94  
95 =head1 SEE ALSO
96  
97 L<Jifty::DBI::Filter>
98  
99 =cut
100  
101 1 1;
« DBI/Column.pm ↑ Summary DBI/Record/Plugin.pm »