blib/lib/Script/Toolbox/Util/Menues.pm

filter: Statementstmt 5.6% 10 / 177 Branchbran 0.0% 0 / 78 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 6.9% 2 / 29 Podpod 0.0% 0 / 19 totaltotal 3.9% 12 / 308 SCAR 76.5
CC
48
Cov
4%
run
56.0
_invalidParam
56.0
getMatching
56.0
CRAP 2094.3
« blib/lib/Script/Toolbox/Util.pm ↑ Summary blib/lib/Script/Toolbox/Util/Formatter.pm »
1 package Script::Toolbox::Util::Menues;
2  
3 21 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 21 ▶use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 21 require Exporter;
7  
8 21 @ISA = qw(Exporter);
9 # Items to export into callers namespace by default. Note: do not export
10 # names by default without a very good reason. Use EXPORT_OK instead.
11 # Do not simply export all your public functions/methods/constants.
12 21 @EXPORT = qw(
13
14 );
15 #$VERSION = '0.03';
16  
17  
18 # Preloaded methods go here.
19  
20 #-----------------------------------------------------------------------------
21 # {'menueName>' =>[{label=>,value=>,jump=>,argv=>},...]}
22 #-----------------------------------------------------------------------------
23 sub new
24 {
25 0 ▶ my $classname = shift;
Subroutine new: not called CC=1 SCAR=6.9
Pod undocumented
26 0 my $self = {};
27 0 bless( $self, $classname );
28 0 $self->_init( @_ );
29 0 return $self;
30 }
31  
32 #-----------------------------------------------------------------------------
33 # {'<menueName>' =>[{label=>,value=>,jump=>,argv=>},...]}
34 #-----------------------------------------------------------------------------
35 sub _init
36 {
37 0 ▶ my ($self, $newDef) = @_;
Subroutine _init: not called CC=2 SCAR=17.9
38  
39 0 $self->{'def'} = {};
40 0 ▶ return if( ref $newDef ne 'HASH' );
Branch: 1 branch
BranchTrueFalseTotal
if ref $newDef ne "HASH" 0 0 0
41 0 $self->addMenue($newDef);
42 }
43  
44 #------------------------------------------------------------------------------
45 #------------------------------------------------------------------------------
46 sub _getHead($){
47 0 ▶ my ($self,$def) = @_;
Subroutine _getHead: not called CC=4 SCAR=30.0
48 0 my $s = '';
49 0 foreach my $k ( @{$def} ) {
50 0 ▶ next if( ! defined $k->{'header'} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $k->{"header"} 0 0 0
51 0 $s .= sprintf "%s", $k->{'header'};
52 }
53 0 ▶ return $s ne '' ? $s : undef;
Branch: 1 branch
BranchTrueFalseTotal
$s ne '' ? : 0 0 0
54 }
55 #------------------------------------------------------------------------------
56 #------------------------------------------------------------------------------
57 sub _getFoot($){
58 0 ▶ my ($self,$def) = @_;
Subroutine _getFoot: not called CC=4 SCAR=30.0
59 0 my $s = '';
60 0 foreach my $k ( @{$def} ) {
61 0 ▶ next if( ! defined $k->{'footer'} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $k->{"footer"} 0 0 0
62 0 $s .= sprintf "%s", $k->{'footer'};
63 }
64 0 ▶ return $s ne '' ? $s : undef;
Branch: 1 branch
BranchTrueFalseTotal
$s ne '' ? : 0 0 0
65 }
66  
67 #------------------------------------------------------------------------------
68 # ...
69 # {'label'=>'Call the submenue 1','jump'=>'SubMenue1'}
70 # SubMenue1: is the name of a previous defined menue in the same menues container
71 #------------------------------------------------------------------------------
72 sub _resolveSubmenue($){
73 0 ▶ my ($self,$opt) = @_;
Subroutine _resolveSubmenue: not called CC=3 SCAR=24.8
74  
75 0 ▶ return if( !defined $opt->{'jump'} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opt->{"jump"} 0 0 0
76 0 ▶ return if( ref \$opt->{'jump'} ne 'SCALAR' );
Branch: 1 branch
BranchTrueFalseTotal
if ref \$opt->{"jump"} ne "SCALAR" 0 0 0
77  
78 0 my $subName = $opt->{'jump'};
79 0 $opt->{'jump'} = \&Script::Toolbox::Util::Menues::run;
80 0 $opt->{'argv'} = [$self,$subName];
81 0 return;
82 }
83  
84 #------------------------------------------------------------------------------
85 # [{label=>,value=>,jump=>,argv=>},...]}
86 #------------------------------------------------------------------------------
87 sub _getOpts($){
88 0 ▶ my ($self,$def) = @_;
Subroutine _getOpts: not called CC=3 SCAR=24.8
89 0 my @s;
90 0 foreach my $k ( @{$def} ) {
91 0 ▶ next if( ! defined $k->{'label'} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $k->{"label"} 0 0 0
92 0 $self->_resolveSubmenue($k);
93 0 push @s, $k;
94 }
95 0 return \@s;
96 }
97  
98 #------------------------------------------------------------------------------
99 # <menueName>, [{label=>,value=>,jump=>,argv=>},...]}
100 #------------------------------------------------------------------------------
101 sub addMenue($){
102 0 ▶ my ($self,$newDef) = @_;
Subroutine addMenue: not called CC=3 SCAR=24.8
Pod undocumented
103  
104 0 ▶ return if( ref $newDef ne 'HASH' );
Branch: 1 branch
BranchTrueFalseTotal
if ref $newDef ne "HASH" 0 0 0
105  
106 0 foreach my $name ( keys %{$newDef} ){
107 0 my $def = $newDef->{$name};
108 0 $self->{'def'}{$name}{'head'} = $self->_getHead($def);
109 0 $self->{'def'}{$name}{'foot'} = $self->_getFoot($def);
110 0 $self->{'def'}{$name}{'opts'} = $self->_getOpts($def);
111 }
112 0 return;
113 }
114  
115 #------------------------------------------------------------------------------
116 # <menueName>, <HeaderString>
117 #------------------------------------------------------------------------------
118 sub setHeader($$){
119 0 ▶ my ($self,$name,$head) = @_;
Subroutine setHeader: not called CC=1 SCAR=6.9
Pod undocumented
120  
121 0 $self->{'def'}{$name}{'head'} = $head;
122 0 return;
123 }
124  
125  
126 #------------------------------------------------------------------------------
127 # <menueName>, <HeaderString>
128 #------------------------------------------------------------------------------
129 sub setAutoHeader($){
130 0 ▶ my ($self,$name) = @_;
Subroutine setAutoHeader: not called CC=3 SCAR=24.8
Pod undocumented
131
132 0 ▶ if( defined $name) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $name) 0 0 0
133 0 $self->{'def'}{$name}{'autohead'} = 1;
134 0 return;
135 }
136 0 foreach my $n (keys %{$self->{'def'}} ){
137 0 $self->{'def'}{$n}{'autohead'} = 1;
138 }
139 }
140  
141 #------------------------------------------------------------------------------
142 #------------------------------------------------------------------------------
143 sub _delAutoHead($){
144 0 ▶ my ($ah) = @_;
Subroutine _delAutoHead: not called CC=2 SCAR=17.9
145 0 ▶ delete $ah->{'autohead'} if( defined $ah->{'autohead'} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $ah->{"autohead"} 0 0 0
146 }
147  
148 #------------------------------------------------------------------------------
149 # <menueName>, <HeaderString>
150 #------------------------------------------------------------------------------
151 sub delAutoHeader($){
152 0 ▶ my ($self,$name) = @_;
Subroutine delAutoHeader: not called CC=3 SCAR=24.8
Pod undocumented
153  
154 0 ▶ if( defined $name) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $name) 0 0 0
155 0 _delAutoHead( $self->{'def'}{$name} );
156 0 return;
157 }
158 0 foreach my $n (keys %{$self->{'def'}} ){
159 0 _delAutoHead( $self->{'def'}{$n} );
160 }
161 }
162  
163 #------------------------------------------------------------------------------
164 # <menueName>, <HeaderString>
165 #------------------------------------------------------------------------------
166 sub getHeader($){
167 0 ▶ my ($self,$name) = @_;
Subroutine getHeader: not called CC=4 SCAR=30.0
Pod undocumented
168  
169 0 my $autoHead = $self->{'def'}{$name}{'autohead'};
170 0 my $H;
171 0 my $h = $self->{'def'}{$name}{'head'};
172 0 ▶ $h = "Menue: $name" if(!defined $h && defined $autoHead);
Branch: 1 branch
BranchTrueFalseTotal
if not defined $h and defined $autoHead 0 0 0
Condition0%0/3
not defined $h and defined $autoHead
!ll&&!rl&&r
000
MC/DC0%0/2
not defined $h and defined $autoHead
not defined $h defined $autoHead
Truth table0%0/3
not(A) and B
A= not defined $hB= defined $autoHead
ABresult
0X0
100
111
173 0 ▶ $H = {'header' => $h} if( defined $h );
Branch: 1 branch
BranchTrueFalseTotal
if defined $h 0 0 0
174  
175 0 return $H;
176 }
177  
178 #------------------------------------------------------------------------------
179 # <menueName>, <FooterString>
180 #------------------------------------------------------------------------------
181 sub setFooter($$){
182 0 ▶ my ($self,$name,$foot) = @_;
Subroutine setFooter: not called CC=1 SCAR=6.9
Pod undocumented
183  
184 0 $self->{'def'}{$name}{'foot'} = $foot;
185 0 return;
186 }
187  
188 #------------------------------------------------------------------------------
189 # <menueName>, <FooterString>
190 #------------------------------------------------------------------------------
191 sub getFooter($){
192 0 ▶ my ($self,$name) = @_;
Subroutine getFooter: not called CC=2 SCAR=17.9
Pod undocumented
193  
194 0 my $foot = $self->{'def'}{$name}{'foot'} ;
195 0 ▶ return {'footer'=> $foot} if(defined $foot);
Branch: 1 branch
BranchTrueFalseTotal
if defined $foot 0 0 0
196 0 return undef;
197 }
198  
199 #------------------------------------------------------------------------------
200 # <menueName>, {label=>,value=>,jump=>,argv=>}
201 #------------------------------------------------------------------------------
202 sub addOption($$){
203 0 ▶ my ($self,$name,$opt) = @_;
Subroutine addOption: not called CC=2 SCAR=17.9
Pod undocumented
204  
205 0 ▶ $self->{'def'}{$name}{'opts'} = [] if( ! defined $self->{'def'}{$name}{'opts'} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"def"}{$name}{"opts"} 0 0 0
206  
207 0 $self->_resolveSubmenue($opt);
208 0 push @{$self->{'def'}{$name}{'opts'}}, $opt;
209 0 return;
210 }
211  
212 #------------------------------------------------------------------------------
213 #------------------------------------------------------------------------------
214 sub _getParams($){
215 0 ▶ my ($self,$name) = @_;
Subroutine _getParams: not called CC=3 SCAR=24.8
216  
217 0 my @p;
218 0 ▶ my $s = $self->getHeader($name); push @p, $s if( defined $s );
Branch: 1 branch
BranchTrueFalseTotal
if defined $s 0 0 0
219 0 ▶ $s = $self->getFooter($name); push @p, $s if( defined $s );
Branch: 1 branch
BranchTrueFalseTotal
if defined $s 0 0 0
220 0 push @p, {'label'=>'RETURN'};
221 map {
222 0 push @p, $_;
223 0 } @{$self->{'def'}{$name}{'opts'}};
224  
225 0 return \@p;
226 }
227  
228 #------------------------------------------------------------------------------
229 # Validate parameters and rearrange parameters in case of internal menue call
230 # ( submenue call by name).
231 # Return 0 if parameters invalid.
232 #------------------------------------------------------------------------------
233 sub validateParams($$){
234 0 ▶ my ($self,$name) = @_;
Subroutine validateParams: not called CC=5 SCAR=34.0
Pod undocumented
235 0 ▶ return 0 if( ! defined $$self );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $$self 0 0 0
236 0 ▶ if( ref $$self eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $$self eq "ARRAY") 0 0 0
237 0 ▶ return 0 if( ref $$self->[0] ne 'Script::Toolbox::Util::Menues' );
Branch: 1 branch
BranchTrueFalseTotal
if ref $$self->[0] ne "Script::Toolbox::Util::Menues" 0 0 0
238 0 $$name = $$self->[1];
239 0 $$self = $$self->[0];
240 }
241 0 ▶ return 1 if( defined $$self->{'def'}{$$name} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $$self->{"def"}{$$name} 0 0 0
242 0 Script::Toolbox::Util::Log("\nWARNING: Submenue $$name is not defined!");
243 0 sleep 5;
244 0 return 0;
245 }
246  
247 #------------------------------------------------------------------------------
248 # Run the named menue as long as $cnt is true. $cnt will be decremented by each
249 # loop. That means if $cnt starts with 0 we have an endless loop.
250 # Return the number of the last selected option.
251 # The option 'RETURN' will be created automaticly and has option number 0 by
252 # default.
253 #------------------------------------------------------------------------------
254 sub run($$){
255 0 ▶ my ($self,$name,$cnt) = @_;
Subroutine run: not called CC=7 SCAR=40.3
Pod undocumented
256  
257 0 ▶ return if( ! validateParams(\$self,\$name) );
Branch: 1 branch
BranchTrueFalseTotal
unless &validateParams(\$self, \$name) 0 0 0
258 0 ▶ $cnt = 1 if( ! defined $cnt);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cnt 0 0 0
259 0 ▶ $cnt = 1 if( $cnt !~ /^[-]?\d+$/ );
Branch: 1 branch
BranchTrueFalseTotal
unless $cnt =~ /^[-]?\d+$/ 0 0 0
260 0 ▶ $cnt =-1 if( $cnt == 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $cnt == 0 0 0 0
261 0 my $o; my $m;
262 0 ▶ while($cnt--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cnt--) 0 0 0
263 0 my $p = $self->_getParams($name);
264 0 ($o,$m) = Script::Toolbox::Util::Menue($p);
265 0 $self->{'def'}{$name}{'selected'}{'num'} = $o;
266 0 $self->{'def'}{$name}{'selected'}{'opt'} = $m->[$o];
267 0 ▶ return $o if( $o == 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $o == 0 0 0 0
268 }
269 0 return $o;
270 }
271  
272 #------------------------------------------------------------------------------
273 # Return current nmber of selected option.
274 #------------------------------------------------------------------------------
275 sub currNumber($){
276 0 ▶ my ($self,$name) = @_;
Subroutine currNumber: not called CC=1 SCAR=6.9
Pod undocumented
277 0 return $self->{'def'}{$name}{'selected'}{'num'};
278 }
279  
280 #------------------------------------------------------------------------------
281 # Return current label of selected option.
282 #------------------------------------------------------------------------------
283 sub currLabel($){
284 0 ▶ my ($self,$name) = @_;
Subroutine currLabel: not called CC=1 SCAR=6.9
Pod undocumented
285 0 return $self->{'def'}{$name}{'selected'}{'opt'}{'label'};
286 }
287  
288 #------------------------------------------------------------------------------
289 # Return current value of selected option.
290 #------------------------------------------------------------------------------
291 sub currValue($){
292 0 ▶ my ($self,$name) = @_;
Subroutine currValue: not called CC=1 SCAR=6.9
Pod undocumented
293 0 return $self->{'def'}{$name}{'selected'}{'opt'}{'value'};
294 }
295  
296 #------------------------------------------------------------------------------
297 # Return the callback address and argv address of selected option.
298 #------------------------------------------------------------------------------
299 sub currJump($){
300 0 ▶ my ($self,$name) = @_;
Subroutine currJump: not called CC=1 SCAR=6.9
Pod undocumented
301  
302 0 my $call = $self->{'def'}{$name}{'selected'}{'opt'}{'jump'};
303 0 my $args = $self->{'def'}{$name}{'selected'}{'opt'}{'argv'};
304
305 0 return $call,$args;
306 }
307  
308 #------------------------------------------------------------------------------
309 # Set a new label for current selected option. Return old label.
310 #------------------------------------------------------------------------------
311 sub setCurrLabel($$){
312 0 ▶ my ($self,$name,$newLabel) = @_;
Subroutine setCurrLabel: not called CC=1 SCAR=6.9
Pod undocumented
313  
314 0 my $cn = $self->currNumber($name) -1;
315 0 my $ol = $self->{'def'}{$name}{'opts'}[$cn]{'label'};
316 0 $self->{'def'}{$name}{'opts'}[$cn]{'label'} = $newLabel;
317 0 return $ol;
318 }
319  
320 #------------------------------------------------------------------------------
321 # Set a new value for current selected option. Return old value.
322 #------------------------------------------------------------------------------
323 sub setCurrValue($$){
324 0 ▶ my ($self,$name,$newValue) = @_;
Subroutine setCurrValue: not called CC=1 SCAR=6.9
Pod undocumented
325  
326 0 my $cn = $self->currNumber($name) -1;
327 0 my $ov = $self->{'def'}{$name}{'opts'}[$cn]{'value'};
328 0 $self->{'def'}{$name}{'opts'}[$cn]{'value'} = $newValue;
329 0 return $ov;
330 }
331  
332 #------------------------------------------------------------------------------
333 # Set new callback address und argv for the current selected option.
334 #------------------------------------------------------------------------------
335 sub setCurrJump($$$){
336 0 ▶ my ($self,$name,$callBack,$argv) = @_;
Subroutine setCurrJump: not called CC=1 SCAR=6.9
Pod undocumented
337  
338 0 my $cn = $self->currNumber($name) -1;
339 0 $self->{'def'}{$name}{'opts'}[$cn]{'jump'} = $callBack;
340 0 $self->{'def'}{$name}{'opts'}[$cn]{'argv'} = $argv;
341 0 return $callBack,$argv;
342 }
343  
344 #------------------------------------------------------------------------------
345 #------------------------------------------------------------------------------
346 sub _invalidParam($$$$$){
347 0 ▶ my ($self,$name,$pattern,$search,$return) = @_;
Subroutine _invalidParam: not called CC=7 SCAR=40.3
348 0 ▶ return 1 if( !defined $pattern );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pattern 0 0 0
349 0 ▶ return 1 if( !defined $search );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $search 0 0 0
350 0 ▶ return 1 if( !defined $return );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $return 0 0 0
351 0 ▶ return 1 if( !defined $self->{'def'}{$name}{'opts'});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"def"}{$name}{"opts"} 0 0 0
352 0 ▶ return 1 if( $search !~ /(number|value|label)/ );
Branch: 1 branch
BranchTrueFalseTotal
unless $search =~ /(number|value|label)/ 0 0 0
353 0 ▶ return 1 if( $return !~ /(number|value|label)/ );
Branch: 1 branch
BranchTrueFalseTotal
unless $return =~ /(number|value|label)/ 0 0 0
354 0 return 0;
355 }
356  
357 #------------------------------------------------------------------------------
358 # Search the labels array for $pattern matching in $search. If matching return
359 # value of type return.
360 # $pattern='[Mm]ax' $search='value' $return='label'
361 # => returns all labels where value column matching Max or max.
362 # search: /(label,number,value)/
363 # return: /(label,number,value)/
364 #------------------------------------------------------------------------------
365 sub getMatching($$$$){
366 0 ▶ my ($self,$name,$pattern,$search,$return) = @_;
Subroutine getMatching: not called CC=7 SCAR=40.3
Pod undocumented
367 0 ▶ return '' if( _invalidParam($self,$name,$pattern,$search,$return));
Branch: 1 branch
BranchTrueFalseTotal
if &_invalidParam($self, $name, $pattern, $search, $return) 0 0 0
368  
369 0 my $L = $self->{'def'}{$name}{'opts'};
370 0 my @R;
371 0 my $i=1;
372 0 foreach my $l ( @{$L} ){
373 0 ▶ if( $search eq 'number' ){
Branch: 1 branch
BranchTrueFalseTotal
if ($search eq 'number') { } 0 0 0
374 0 ▶ push @R, $l->{$return} if( $i =~ /$pattern/);
Branch: 1 branch
BranchTrueFalseTotal
if $i =~ /$pattern/ 0 0 0
375 0 $i++;
376 }else{
377 0 ▶ next if( !defined $l->{$search} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $l->{$search} 0 0 0
378 0 ▶ next if( $l->{$search} !~ /$pattern/ );
Branch: 1 branch
BranchTrueFalseTotal
unless $l->{$search} =~ /$pattern/ 0 0 0
379 0 push @R, $l->{$return};
380 }
381 }
382 0 return \@R;
383 }
384  
385  
386 21 1;
387 __END__
« blib/lib/Script/Toolbox/Util.pm ↑ Summary blib/lib/Script/Toolbox/Util/Formatter.pm »