| 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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||
| 30 | 76 | for ($self->FALSE_VALUES, map { "'$_'" } $self->FALSE_VALUES) { | |||||||||||||||||||||||||||||||
| 31 | 244 | ▶ | return 1 if $value eq $_; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||
| 57 | 43 | ▶ | if ($self->_is_true($$value_ref)) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
defined $$value_ref or $self->column->mandatory
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
| |||||||||||||||||||||||||||||||||
| 81 | 108 | ▶ | return if uc($$value_ref||'') eq "NULL" and not $self->column->mandatory; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition60%3/5
uc($$value_ref || "") eq "NULL" and not $self->column->mandatory
$$value_ref || ''
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
| |||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||
| 83 | 108 | ▶ | if ($self->_is_true($$value_ref)) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 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; | |||||||||||||||||||||||||||||||