lib/CallBackery/GuiPlugin/AbstractForm.pm

filter: Statementstmt 50.4% 63 / 125 Branchbran 24.1% 14 / 58 Conditioncond 7.4% 2 / 27 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 63.6% 14 / 22 Podpod 100% 8 / 8 totaltotal 39.4% 101 / 256 CC 45 SCAR 34.7
CC
45
Cov
38%
validateData
90.0
processData
72.0
formPhase
56.0
CRAP 32.1
« blib/lib/CallBackery/Model/ConfigJsonSchema.pm ↑ Summary lib/CallBackery/Controller/RpcService.pm »
1 package CallBackery::GuiPlugin::AbstractForm;
2 14 ▶use Carp qw(carp croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
3 14 ▶use CallBackery::Translate qw(trm);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 14 ▶use CallBackery::Exception qw(mkerror);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 14 ▶use Mojo::Promise;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 14 ▶use Mojo::Util qw(dumper);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 14 ▶use Time::HiRes;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =head1 NAME
10  
11 CallBackery::GuiPlugin::AbstractForm - form base class
12  
13 =head1 SYNOPSIS
14  
15 use Mojo::Base 'CallBackery::GuiPlugin::AbstractForm', -signatures;
16  
17 =head1 DESCRIPTION
18  
19 The base class for gui forms.
20  
21 =cut
22  
23 14 ▶use Mojo::Base 'CallBackery::GuiPlugin::AbstractAction', -signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 =head1 ATTRIBUTES
26  
27 The attributes of the L<CallBackery::GuiPlugin::Abstract> class plus:
28  
29 =head2 screenCfg
30  
31 Returns a configuration structure for the form. The output from this
32 method is fed to the callbackery.ui.form.Auto object to build the
33 Qooxdoo form.
34  
35 =cut
36  
37 0 ▶has screenCfg => sub ($self) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
38 0 my $cfg = $self->SUPER::screenCfg;
39 0 $cfg->{type} = 'form';
40 0 $cfg->{form} = $self->formCfg;
41 0 return $cfg;
42 14 };
43  
44 =head2 formCfg
45  
46 Returns the content of the form.
47  
48 =cut
49  
50 0 ▶has formCfg => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
51 0 [];
52 14 };
53  
54 =head2 formCfg
55  
56 TODOC
57  
58 =cut
59  
60 3 ▶has formCfgMap => sub ($self) {
Subroutine __ANON__: called CC=3 SCAR=11.0
61 3 my %map;
62 3 for my $row (@{$self->formCfg}){
63 6 ▶ next unless $row->{key};
Branch: 1 branch
BranchTrueFalseTotal
unless $row->{"key"} 0 6 6
64 6 $map{$row->{key}} = $row;
65 }
66 3 return \%map;
67 14 };
68  
69  
70 =head2 formData ()
71  
72 Return the form data independently from the form phase.
73  
74 =cut
75  
76 0 ▶sub formData ($self) {
Subroutine formData: not called CC=5 SCAR=34.0
Pod documented
77 0 ▶ my $args = $self->args || {};
Condition0%0/2
$self->args || {}
l!l
00
MC/DC0%0/1
$self->args || {}
$self->args
Truth table0%0/2
A
A= $self->args
Aresult
11
00
78 0 ▶ return {} if ref $args->{location} eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $args->{"location"} eq "HASH" 0 0 0
79 0 ▶ return $args->{currentFormData} || $args->{formData} || $args;
Condition0%0/6
$args->{'currentFormData'} || $args->{'formData'} || $args
l!l&&r!l&&!r
000
$args->{"currentFormData"} or $args->{"formData"}
l!l&&r!l&&!r
000
MC/DC0%0/3
$args->{'currentFormData'} || $args->{'formData'} || $args
$args->{"currentFormData"} $args->{"formData"} $args
Truth table0%0/4
A or B || C
A= $args->{"currentFormData"}B= $args->{"formData"}C= $args
ABCresult
1XX1
01X1
0011
0000
80 };
81  
82  
83 =head2 formPhase ()
84  
85 Return the form phase.
86  
87 =cut
88  
89 0 ▶sub formPhase ($self) {
Subroutine formPhase: not called CC=7 SCAR=40.3
Pod documented
90 0 my $args = $self->args;
91  
92  
93 # data
94 0 ▶ if ($args->{currentFormData}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"currentFormData"}) 0 0 0
95 0 ▶ return 'reconfigure' if $args->{triggerField};
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"triggerField"} 0 0 0
96 0 return 'initialize';
97 }
98  
99 # actions
100 0 ▶ if ($args->{formData}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"formData"}) 0 0 0
101 0 ▶ return "action:$args->{key}" if $args->{key};
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"key"} 0 0 0
102 }
103  
104 # if called from CardList or Table plugins
105 0 ▶ if (ref $args eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args eq "HASH") 0 0 0
106 0 ▶ return 'pluginConfig' if not keys $args->%*;
Branch: 1 branch
BranchTrueFalseTotal
unless keys %$args 0 0 0
107 0 return 'instanciate';
108 }
109  
110 # catch all, if ever reached
111 0 $self->log->warn('Unknown form phase, args=', dumper $args);
112 0 return 'unknown';
113 };
114  
115  
116 =head1 METHODS
117  
118 All the methods of L<CallBackery::GuiPlugin::Abstract> plus:
119  
120 =cut
121  
122 =head2 validateData(fieldName,formData)
123  
124 If the given value is valid for the field, return undef else return
125 an error message.
126  
127 =cut
128  
129 0 ▶sub validateData {
Subroutine validateData: not called CC=9 SCAR=45.0
Pod documented
130 0 my $self = shift;
131 0 my $fieldName = shift;
132 0 ▶ my $formData = shift || {};
Condition0%0/2
shift() || {}
l!l
00
MC/DC0%0/1
shift() || {}
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
133 0 $self->args($formData);
134 0 my $entry = $self->formCfgMap->{$fieldName};
135 0 ▶ if (not ref $entry){
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $entry) 0 0 0
136 0 die mkerror(4095,trm("sorry, don't know the field you are talking about"));
137 }
138 0 ▶ my $fieldIsEmpty = (not defined $formData->{$fieldName} or length($formData->{$fieldName}) == 0);
Condition0%0/3
!defined($formData->{$fieldName}) || length $formData->{$fieldName} == 0
l!l&&r!l&&!r
000
MC/DC0%0/2
!defined($formData->{$fieldName}) || length $formData->{$fieldName} == 0
!defined($formData->{$fieldName}) length $formData->{$fieldName} == 0
Truth table0%0/3
not(A) || B
A= !defined($formData->{$fieldName})B= length $formData->{$fieldName} == 0
ABresult
1X1
011
000
139 0 ▶ return if not $entry->{set}{required} and $fieldIsEmpty;
Branch: 1 branch
BranchTrueFalseTotal
if not $entry->{"set"}{"required"} and $fieldIsEmpty 0 0 0
Condition0%0/3
not $entry->{"set"}{"required"} and $fieldIsEmpty
!ll&&!rl&&r
000
MC/DC0%0/2
not $entry->{"set"}{"required"} and $fieldIsEmpty
not $entry->{"set"}{"required"} $fieldIsEmpty
Truth table0%0/3
not(A) and B
A= not $entry->{"set"}{"required"}B= $fieldIsEmpty
ABresult
0X0
100
111
140 0 ▶ if ($entry->{validator}){
Branch: 2 branches
BranchTrueFalseTotal
if ($entry->{'validator'}) { } 0 0 0
elsif ($entry->{'set'}{'required'} and $fieldIsEmpty) { } 0 0 0
141 0 my $start = time;
142 0 my $data = $entry->{validator}->($formData->{$fieldName},$fieldName,$formData);
143 0 $self->log->debug(sprintf("validator %s: %0.2fs",$fieldName,time-$start));
144 0 return $data;
145 }
146 # if there is no validator but the field is required, complain
147 # if the content is empty
148 ▶ elsif ($entry->{set}{required} and $fieldIsEmpty){
Condition0%0/3
$entry->{"set"}{"required"} and $fieldIsEmpty
!ll&&!rl&&r
000
MC/DC0%0/2
$entry->{"set"}{"required"} and $fieldIsEmpty
$entry->{"set"}{"required"} $fieldIsEmpty
Truth table0%0/3
A and B
A= $entry->{"set"}{"required"}B= $fieldIsEmpty
ABresult
0X0
100
111
149 0 return trm('The %1 field is required',$fieldName);
150 }
151 0 return;
152 }
153  
154 =head2 processData($args)
155  
156 The default behavior of the method is to validate all the form fields
157 and then store the data into the config database.
158  
159 =cut
160  
161 0 ▶sub processData ($self, $args, $extraArgs=undef) {
Subroutine processData: not called CC=8 SCAR=42.8
Pod documented
162 0 ▶ $self->args($args) if $args;
Branch: 1 branch
BranchTrueFalseTotal
if $args 0 0 0
163 0 my $form = $self->formCfgMap;
164 0 my $formData = $args->{formData};
165 # this is only to be sure ... data should be pre-validated
166 0 for my $key (keys %$form){
167 0 ▶ if (my $error = $self->validateData($key,$formData)){
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $self->validateData($key, $formData)) 0 0 0
168 0 die mkerror(7492,$error);
169 }
170 }
171 0 ▶ if ($args->{key}){
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"key"}) 0 0 0
172 0 my $handler = $self->actionCfgMap->{$args->{key}}{actionHandler};
173 0 ▶ if (ref $handler eq 'CODE'){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $handler eq "CODE") 0 0 0
174 0 return $handler->($self,$formData);
175 }
176 0 $handler = $self->actionCfgMap->{$args->{key}}{handler};
177 0 ▶ if (ref $handler eq 'CODE'){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $handler eq "CODE") 0 0 0
178 0 $self->log->warn("Using handler properties in actionCfg is deprecated. Use actionHandler instead.");
179 0 return $handler->($formData);
180 }
181 0 $self->log->error('Plugin instance '.$self->name." action $args->{key} has a broken handler");
182 0 die mkerror(7623,'Plugin instance '.$self->name." action $args->{key} has a broken handler");
183 }
184 }
185  
186  
187 =head2 saveFormDataToConfig(data)
188  
189 Save all the form fields for which is available to the config
190 database. Keys will be prefixed by the plugin instance name
191 (C<PluginInstance::keyName>).
192  
193 =cut
194  
195 0 ▶sub saveFormDataToConfig ($self, $formData) {
Subroutine saveFormDataToConfig: not called CC=3 SCAR=24.8
Pod documented
196 0 my $form = $self->formCfgMap;
197 0 for my $key (keys %$form){
198 0 ▶ next if not exists $formData->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $formData->{$key} 0 0 0
199 0 $self->setConfigValue($self->name.'::'.$key,$formData->{$key});
200 }
201 }
202  
203 =head2 getFieldValue(field)
204  
205 Fetch the current value of the field. This will either use the getter
206 method supplied in the form config or try to fetch the value from the
207 config database.
208  
209 =cut
210  
211 6 ▶sub getFieldValue ($self, $field) {
Subroutine getFieldValue: called CC=7 SCAR=20.8
Pod documented
212 6 my $entry = $self->formCfgMap->{$field};
213 6 my $log = $self->log;
214 6 ▶ return undef unless ref $entry eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $entry eq "HASH" 0 6 6
215 6 ▶ if ($entry->{getter}){
Branch: 1 branch
BranchTrueFalseTotal
if ($entry->{"getter"}) 6 0 6
216 6 ▶ if (ref $entry->{getter} eq 'CODE'){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $entry->{'getter'} eq 'CODE') { } 6 0 6
217 6 my $start = time;
218 6 my $data = $entry->{getter}->($self);
219 6 ▶ if (eval { blessed $data && $data->isa('Mojo::Promise')}){
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { $data->blessed and $data->isa('Mojo::Promise') } }) { } 3 3 6
Condition33%1/3
$data->blessed && $data->isa('Mojo::Promise')
!ll&&!rl&&r
003
MC/DC0%0/2
$data->blessed && $data->isa('Mojo::Promise')
$data->blessed $data->isa('Mojo::Promise')
Truth table33%1/3
A && B
A= $data->blessedB= $data->isa('Mojo::Promise')
ABresult
0X0
100
111
220 3 ▶ $data = $data->then(sub ($value) {
Subroutine __ANON__: called CC=2 SCAR=7.1
221 3 my $delay = time-$start;
222 3 ▶ $log->debug(sprintf("slow async getter %s: %0.2fs",$field,$delay)) if $delay > 2;
Branch: 1 branch
BranchTrueFalseTotal
if $delay > 2 0 3 3
223 3 return $value;
224 3 });
225 }
226 else {
227 3 my $delay = time-$start;
228 3 ▶ $log->debug(sprintf("slow getter %s: %0.2fs",$field,$delay)) if $delay > 2;
Branch: 1 branch
BranchTrueFalseTotal
if $delay > 2 0 3 3
229 }
230 6 return $data;
231 }
232 else {
233 0 $log->warn('Plugin instance'.$self->name." field $field has a broken getter\n");
234 }
235 }
236 0 return $self->getConfigValue($self->name.'::'.$field);
237 }
238  
239 =head2 getAllFieldValues
240  
241 Return all field values of the form.
242  
243 =cut
244  
245 3 ▶sub getAllFieldValues ($self, $parentForm, $currentForm, $args) {
Subroutine getAllFieldValues: called CC=6 SCAR=18.7
Pod documented
246 3 my %map;
247 my @promises;
248 3 ▶ $self->args($currentForm) if $currentForm;
Branch: 1 branch
BranchTrueFalseTotal
if $currentForm 3 0 3
249 3 for my $key (keys %{$self->formCfgMap}){
250 6 my $value = $self->getFieldValue($key);
251 6 ▶ if (eval { blessed $value && $value->isa('Mojo::Promise')}){
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { $value->blessed and $value->isa('Mojo::Promise') } }) { } 3 3 6
Condition33%1/3
$value->blessed && $value->isa('Mojo::Promise')
!ll&&!rl&&r
003
MC/DC0%0/2
$value->blessed && $value->isa('Mojo::Promise')
$value->blessed $value->isa('Mojo::Promise')
Truth table33%1/3
A && B
A= $value->blessedB= $value->isa('Mojo::Promise')
ABresult
0X0
100
111
252 3 push @promises, $value;
253 $value->then(
254 3 ▶ sub{
Subroutine __ANON__: called CC=1 SCAR=0.0
255 3 $map{$key} = shift;
256 },
257 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
258 0 die shift;
259 }
260 3 );
261 }
262 else {
263 3 $map{$key} = $value;
264 }
265 }
266 3 ▶ if (@promises){
Branch: 1 branch
BranchTrueFalseTotal
if (@promises) 3 0 3
267 3 ▶ return Mojo::Promise->new->all(@promises)->then(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
268 3 return \%map;
269 3 });
270 }
271 0 return \%map;
272 }
273  
274  
275 =head2 getData (type,field)
276  
277 Return the value of the given field. If no field name is specified
278 return a hash with all the current data known to the plugin.
279  
280 =cut
281  
282 1 ▶sub getData ($self, $type, @args) {
Subroutine getData: called CC=4 SCAR=20.1
Pod documented
283 1 ▶ if ($type eq 'field'){
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'field') { } 0 1 1
elsif ($type eq 'allFields') { } 1 0 1
284 0 return $self->getFieldValue(@args);
285 }
286 elsif ($type eq 'allFields') {
287 1 return $self->getAllFieldValues(@args);
288 }
289 else {
290 0 ▶ die mkerror(38334, 'Requested unknown data type ' . ($type // 'unknown'));
Condition0%0/2
$type // "unknown"
l!l
00
MC/DC0%0/1
$type // "unknown"
$type
Truth table0%0/2
A
A= $type
Aresult
11
00
291 }
292 }
293  
294 14 1;
295  
296 __END__
« blib/lib/CallBackery/Model/ConfigJsonSchema.pm ↑ Summary lib/CallBackery/Controller/RpcService.pm »