SysParams/Schema/Result/SysInfo.pm

filter: Statementstmt 86.5% 58 / 67 Branchbran 46.1% 12 / 26 Conditioncond 29.4% 5 / 17 MC/DCmcdc 11.1% 1 / 9 Subroutinesub 77.7% 14 / 18 Podpod 100% 4 / 4 totaltotal 66.6% 94 / 141 CC 20 SCAR 25.4
CC
20
Cov
68%
set_type_from_value
35.0
convert_to
6.4
__ANON__
6.0
CRAP 12.7
↑ Summary SysParams/Schema/ResultSet/SysInfo.pm »
1 package OpusVL::SysParams::Schema::Result::SysInfo;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 4 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use MooseX::NonMoose;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use Data::Munge qw/elem/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use Scalar::Util qw/reftype looks_like_number/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 extends 'DBIx::Class::Core';
13  
14 4 __PACKAGE__->load_components("InflateColumn::DateTime", "TimeStamp");
15  
16  
17 4 __PACKAGE__->table("sys_info");
18  
19  
20 4 __PACKAGE__->add_columns(
21 "name",
22 {
23 data_type => "text",
24 is_nullable => 0,
25 original => { data_type => "varchar" },
26 },
27 "label",
28 {
29 data_type => "text",
30 is_nullable => 1,
31 original => { data_type => "varchar" },
32 },
33 "value",
34 {
35 data_type => "text",
36 is_nullable => 1,
37 original => { data_type => "varchar" },
38 },
39 "comment",
40 {
41 data_type => "text",
42 is_nullable => 1,
43 original => { data_type => "varchar" },
44 },
45 data_type =>
46 {
47 # NOTE: don't set a default value for objects, because the current data_type
48 # restricts what other data_types we can select.
49 data_type => 'enum',
50 is_nullable => 1,
51 extra => {
52 list => [ qw/text textarea object array bool/ ],
53 labels => [ "Text", "Multiline Text", "Object", "List", "Boolean" ],
54 }
55 },
56 );
57 4 __PACKAGE__->set_primary_key("name");
58  
59 sub decoded_value
60 3 ▶{
Subroutine decoded_value: called CC=2 SCAR=7.1
Pod documented
61 3 my $self = shift;
62 3 ▶ return if not defined $self->value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->value 0 3 3
63 3 return JSON->new->allow_nonref->decode($self->value);
64 }
65  
66 2 ▶sub viable_type_conversions {
Subroutine viable_type_conversions: called CC=3 SCAR=11.3
Pod documented
67 2 my $self = shift;
68  
69 return $self->column_info('data_type')->{extra}->{list}
70 2 ▶ if not $self->data_type;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->data_type 0 2 2
71  
72 my $options = +{
73 text => [ qw/textarea array/ ],
74 bool => [ qw/text textarea/ ],
75 array => [ qw/textarea/ ],
76 textarea => [ qw/array/ ],
77 2 ▶ }->{$self->data_type} // [];
Condition50%1/2
{"text", ["textarea", "array"], "bool", ["text", "textarea"], "array", ["textarea"], "textarea", ["array"]}->{$self->data_type} // []
l!l
20
MC/DC0%0/1
{"text", ["textarea", "array"], "bool", ["text", "textarea"], "array", ["textarea"], "textarea", ["array"]}->{$self->data_type} // []
{"text", ["textarea", "array"], "bool", ["text", "textarea"], "array", ["textarea"], "textarea", ["array"]}->{$self->data_type}
Truth table50%1/2
A
A= {"text", ["textarea", "array"], "bool", ["text", "textarea"], "array", ["textarea"], "textarea", ["array"]}->{$self->data_type}
Aresult
11
00
78  
79 2 unshift @$options, $self->data_type;
80 2 return $options;
81 }
82  
83 2 ▶sub convert_to {
Subroutine convert_to: called CC=5 SCAR=18.5
Pod documented
84 2 my $self = shift;
85 2 my ($type) = @_;
86  
87 2 ▶ if(!defined $self->data_type || $self->data_type eq '')
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->data_type or $self->data_type eq "") 1 1 2
Condition66%2/3
not defined $self->data_type or $self->data_type eq ""
l!l&&r!l&&!r
101
MC/DC50%1/2
not defined $self->data_type or $self->data_type eq ""
not defined $self->data_type $self->data_type eq ""
Truth table66%2/3
not(A) or B
A= not defined $self->data_typeB= $self->data_type eq ""
ABresult
1X1
011
000
88 {
89 1 $self->set_type_from_value;
90 }
91 2 ▶ die "Cannot convert " . $self->name . " to $type"
Branch: 1 branch
BranchTrueFalseTotal
unless elem($type, $self->viable_type_conversions) 0 2 2
92 unless elem $type, $self->viable_type_conversions;
93  
94 2 ▶ return $self->decoded_value
Branch: 1 branch
BranchTrueFalseTotal
if $type eq $self->data_type 0 2 2
95 if $type eq $self->data_type;
96  
97 my $conv = {
98 1 ▶ "text textarea" => sub { @_ },
Subroutine __ANON__: called CC=1 SCAR=0.0
99 0 ▶ "text array" => sub { [@_] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
100 0 ▶ "bool text" => sub { $_[0] ? "True" : "False" },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0] ? : 0 0 0
101 0 ▶ "bool textarea" => sub { $_[0] ? "True" : "False" },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0] ? : 0 0 0
102 1 ▶ "array textarea" => sub { join "\n", @{$_[0]} },
Subroutine __ANON__: called CC=1 SCAR=0.0
103 0 ▶ "textarea array" => sub { [ split /\n/, $_[0] ] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
104 2 };
105  
106 2 my $key = join ' ', $self->data_type, $type;
107  
108 2 $conv->{$key}->($self->decoded_value);
109 }
110  
111 6 ▶sub set_type_from_value {
Subroutine set_type_from_value: called CC=11 SCAR=35.6
Pod documented
112 6 my $self = shift;
113 6 ▶ my $value = shift // $self->decoded_value;
Condition66%2/3
shift() // $self->decoded_value
l!l&&r!l&&!r
510
MC/DC0%0/2
shift() // $self->decoded_value
shift() $self->decoded_value
Truth table66%2/3
A // B
A= shift()B= $self->decoded_value
ABresult
1X1
011
000
114  
115 6 ▶ if (ref $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value) { } 2 4 6
116 2 ▶ if (ref $value =~ /Bool/) {
Branch: 4 branches
BranchTrueFalseTotal
if (ref $value =~ /Bool/) { } 0 2 2
elsif (builtin::reftype($value) eq 'HASH') { } 0 2 2
elsif (builtin::reftype($value) eq 'ARRAY') { } 2 0 2
elsif (builtin::reftype($value) eq 'SCALAR' and &looks_like_number($$value) and $$value == 0 || $$value == 1) { } 0 0 0
117 # JSON::Boolean, JSON::PP::Boolean, etc
118 0 $self->data_type('bool')
119 }
120 elsif (reftype $value eq 'HASH') {
121 0 $self->data_type('object');
122 }
123 elsif (reftype $value eq 'ARRAY') {
124 2 $self->data_type('array');
125 }
126 ▶ elsif ( reftype $value eq 'SCALAR'
Condition0%0/9
builtin::reftype($value) eq "SCALAR" and &looks_like_number($$value) and $$value == 0 || $$value == 1
!ll&&!rl&&r
000
builtin::reftype($value) eq "SCALAR" and &looks_like_number($$value)
!ll&&!rl&&r
000
$$value == 0 or $$value == 1
l!l&&r!l&&!r
000
MC/DC0%0/4
builtin::reftype($value) eq "SCALAR" and &looks_like_number($$value) and $$value == 0 || $$value == 1
builtin::reftype($value) eq "SCALAR" &looks_like_number($$value) $$value == 0 $$value == 1
Truth table0%0/5
A and B and C or D
A= builtin::reftype($value) eq "SCALAR"B= &looks_like_number($$value)C= $$value == 0D= $$value == 1
ABCDresult
0XXX0
10XX0
11000
111X1
11011
127 and looks_like_number($$value)
128 and ($$value == 0 or $$value == 1) )
129 {
130 0 $self->data_type('bool');
131 }
132 else {
133 0 warn "Cannot determine type for " . $self->name . " given " . reftype $value . ".";
134 }
135 }
136 else {
137 4 ▶ if ($value =~ /\n/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /\n/) { } 0 4 4
138 0 $self->data_type('textarea');
139 }
140 else {
141 4 $self->data_type('text');
142 }
143 }
144 }
145  
146 4 __PACKAGE__->meta->make_immutable;
147  
148 4 1;
149  
150 __END__
↑ Summary SysParams/Schema/ResultSet/SysInfo.pm »