SysParams/Schema/Result/SysInfo.pm

filter: Statementstmt 87.8% 58 / 66 Branchbran 50.0% 12 / 24 Conditioncond 62.5% 5 / 8 MC/DCmcdc 20.0% 1 / 5 Subroutinesub 77.7% 14 / 18 Podpod 100% 4 / 4 totaltotal 75.2% 94 / 125 CC 16 SCAR 15.2
CC
16
Cov
77%
set_type_from_value
9.6
convert_to
6.4
__ANON__
6.0
CRAP 4.6
↑ 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/;
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 {
61 3 ▶ my $self = shift;
Subroutine decoded_value: called CC=2 SCAR=7.1
Pod documented
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 sub viable_type_conversions {
67 2 ▶ my $self = shift;
Subroutine viable_type_conversions: called CC=3 SCAR=11.3
Pod documented
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 sub convert_to {
84 2 ▶ my $self = shift;
Subroutine convert_to: called CC=5 SCAR=18.5
Pod documented
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 sub set_type_from_value {
112 6 ▶ my $self = shift;
Subroutine set_type_from_value: called CC=7 SCAR=22.6
Pod documented
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: 3 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
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 else {
127 0 warn "Cannot determine type for " . $self->name . " given " . reftype $value . ".";
128 }
129 }
130 else {
131 4 ▶ if ($value =~ /\n/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /\n/) { } 0 4 4
132 0 $self->data_type('textarea');
133 }
134 else {
135 4 $self->data_type('text');
136 }
137 }
138 }
139  
140 4 __PACKAGE__->meta->make_immutable;
141  
142 4 1;
143  
144 __END__
145  
146 =pod
147  
148 =encoding UTF-8
149  
150 =head1 NAME
151  
152 OpusVL::SysParams::Schema::Result::SysInfo
153  
154 =head1 VERSION
155  
156 version 0.18
157  
158 =head1 ACCESSORS
159  
160 =head2 name
161  
162 data_type: 'text'
163 is_nullable: 0
164 original: {data_type => "varchar"}
165  
166 =head2 value
167  
168 data_type: 'text'
169 is_nullable: 1
170 original: {data_type => "varchar"}
171  
172 =head2 comment
173  
174 data_type: 'text'
175 is_nullable: 1
176 original: {data_type => "varchar"}
177  
178 =head2 decoded_value
179  
180 Returns the value that the get method returns.
181 This may be any arbitrary data (simple) type.
182  
183 =head2 viable_type_conversions
184  
185 Returns an arrayref of the types we can probably convert this value to. Also
186 returns the current type.
187  
188 For a new row, this simply returns the whole set, because we haven't specified
189 the type yet.
190  
191 =head2 METHODS
192  
193 =head2 convert_to
194  
195 =over
196  
197 =item $data_type
198  
199 =back
200  
201 Converts the value to the provided data type (see C<viable_type_conversions>),
202 if necessary. Returns the decoded value, i.e. a Perl data structure.
203  
204 Expected types are,
205  
206 =over
207  
208 =item * text
209  
210 =item * array
211  
212 =item * textarea
213  
214 =back
215  
216 =head2 set_type_from_value
217  
218 =over
219  
220 =item $value
221  
222 =back
223  
224 Attempts to guess the data type of the provided value, which defaults to the
225 row's value if not provided. Sets the C<data_type> property on the field, but
226 doesn't save it.
227  
228 =head1 AUTHOR
229  
230 OpusVL - www.opusvl.com
231  
232 =head1 COPYRIGHT AND LICENSE
233  
234 This software is copyright (c) 2011 - 2016 by OpusVL - www.opusvl.com.
235  
236 This is free software; you can redistribute it and/or modify it under
237 the same terms as the Perl 5 programming language system itself.
238  
239 =cut
↑ Summary SysParams/Schema/ResultSet/SysInfo.pm »