Listman/dictionary.pm

filter: Statementstmt 10.5% 10 / 95 Branchbran 0.0% 0 / 46 Conditioncond 0.0% 0 / 32 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 20.0% 3 / 15 Podpod 100% 11 / 11 totaltotal 11.0% 24 / 217 CC 38 SCAR 45.6
CC
38
Cov
6%
reposition_term
240.0
save
72.0
_load
72.0
CRAP 95.4
↑ Summary Listman.pm »
1 # dictionary.pm - this file is part of the CGI::Listman distribution
2 #
3 # CGI::Listman is Copyright (C) 2002 iScream multimédia <info@iScream.ca>
4 #
5 # This package is free software; you can redistribute it and/or
6 # modify it under the same terms as Perl itself.
7 #
8 # Author: Wolfgang Sourdeau <Wolfgang@Contre.COM>
9  
10 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 package CGI::Listman::dictionary;
13  
14 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 ▶use CGI::Listman::dictionary::term;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 =pod
19  
20 =head1 NAME
21  
22 CGI::Listman::dictionary - list of and informations about CGI form parameters
23  
24 =head1 SYNOPSIS
25  
26 use CGI::Listman::dictionary;
27  
28 =head1 DESCRIPTION
29  
30 A dictionary mainly serves two purposes: first, it gives the list manager
31 a reference for what database rows are in use; second, it makes the
32 connection between your web representation of those rows (definition) and
33 checks whether some fields are mandatory or not.
34  
35 This helps you build web pages where each mentionned field can have a
36 readable name. For example, let's say you have a form with a field
37 internally identified with "user_name". If the user forgets or refuses to
38 fill in this field, I<CGI::Listman> will return you a list containing it
39 among other similar fields (see the I<check_params> method of the
40 I<CGI::Listman> class). Furthermore, your error page could contain a more
41 explicit field name such as "User Identification" instead of the silly
42 "user_name" identifier.
43  
44 This functionality is somewhat limited at this stage since
45 I<CGI::Listman> only checks the presence of mandatory fields, yet it
46 considers strings of whitespaces as empty strings. Otherwise, it does not
47 enforce semantics over those field. Until this is implemented, I
48 recommend you have a look at Mark Stosberg's L<Data::FormValidator> or
49 Francis J. Lacoste's L<HTML::FormValidator> modules which seem to do a
50 pretty job.
51  
52 =head1 API
53  
54 =head2 new
55  
56 Constructor with a an argument specifying the filename where to store
57 the dictionary informations.
58  
59 =over
60  
61 =item Parameters
62  
63 The parameter "filename" is optional with this method.
64  
65 =over
66  
67 =item filename
68  
69 A string representing the path to your dictionary's storage file.
70  
71 =back
72  
73 =item Return values
74  
75 A reference to a blessed instance of I<CGI::Listman::dictionary>.
76  
77 =back
78  
79 =cut
80  
81 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
82 0 my $class = shift;
83  
84 0 my $self = {};
85 0 $self->{'filename'} = shift;
86  
87 0 $self->{'_terms'} = undef;
88 0 $self->{'_loading'} = 0;
89  
90 0 bless $self, $class;
91 }
92  
93 =pod
94  
95 =head2 add_term
96  
97 Append an instance of I<CGI::Listman::dictionary::term> to your
98 dictionary's list of terms.
99  
100 =over
101  
102 =item Parameters
103  
104 =over
105  
106 =item term
107  
108 A reference to the instance of I<CGI::Listman::dictionary::term> you want
109 to add to your dictionary.
110  
111 =back
112  
113 =item Return values
114  
115 This method returns nothing.
116  
117 =back
118  
119 =cut
120  
121 0 ▶sub add_term {
Subroutine add_term: not called CC=1 SCAR=6.9
Pod documented
122 0 my ($self, $term) = @_;
123  
124 0 my $terms_ref = $self->terms ();
125 0 push @$terms_ref, $term;
126 }
127  
128 =pod
129  
130 =head2 get_term
131  
132 This method returns the I<CGI::Listman::dictionary::term> corresponding
133 to the key given as parameter.
134  
135 =over
136  
137 =item Parameters
138  
139 =over
140  
141 =item key
142  
143 The key of the term you wish to be returned.
144  
145 =back
146  
147 =item Return values
148  
149 A reference to an instance of I<CGI::Listman::dictionary::term> or
150 I<undef> if the key was not found.
151  
152 =back
153  
154 =cut
155  
156 0 ▶sub get_term {
Subroutine get_term: not called CC=4 SCAR=30.0
Pod documented
157 0 my ($self, $key) = @_;
158  
159 0 my $terms_ref = $self->terms ();
160  
161 0 my $term_object = undef;
162  
163 0 ▶ if (defined $terms_ref) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $terms_ref) 0 0 0
164 0 foreach my $term (@$terms_ref) {
165 0 ▶ next if ($term->{'key'} ne $key);
Branch: 1 branch
BranchTrueFalseTotal
if $term->{"key"} ne $key 0 0 0
166 0 $term_object = $term;
167 }
168 }
169  
170 0 return $term_object;
171 }
172  
173 =pod
174  
175 =head2 terms
176  
177 Returns the list of terms.
178  
179 =over
180  
181 =item Parameters
182  
183 This method takes no parameter.
184  
185 =item Return values
186  
187 A reference to the ARRAY containing the list of terms.
188  
189 =back
190  
191 =cut
192  
193 0 ▶sub terms {
Subroutine terms: not called CC=2 SCAR=17.9
Pod documented
194 0 my $self = shift;
195  
196 0 ▶ $self->_load () unless (defined $self->{'_terms'});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"_terms"} 0 0 0
197 0 my $terms_ref = $self->{'_terms'};
198  
199 0 return $terms_ref;
200 }
201  
202 =pod
203  
204 =head2 term_pos_in_list
205  
206 =over
207  
208 =item Parameters
209  
210 =over
211  
212 =item term
213  
214 An instance of I<CGI::Listman::dictionary::term> you wish to know the
215 position of in the list.
216  
217 =back
218  
219 =item Return values
220  
221 This method returns an integer corresponding to that term in the list's
222 order or -1 if the term was not found.
223  
224 =back
225  
226 =cut
227  
228 0 ▶sub term_pos_in_list {
Subroutine term_pos_in_list: not called CC=4 SCAR=30.0
Pod documented
229 0 my ($self, $term) = @_;
230  
231 0 my $number = 0;
232 0 my $terms_ref = $self->terms ();
233 0 foreach my $comp_term (@$terms_ref) {
234 0 ▶ last if ($comp_term == $term);
Branch: 1 branch
BranchTrueFalseTotal
if $comp_term == $term 0 0 0
235 0 $number++;
236 }
237  
238 0 ▶ $number = -1 if ($number == scalar (@$terms_ref));
Branch: 1 branch
BranchTrueFalseTotal
if $number == scalar @$terms_ref 0 0 0
239  
240 0 return $number;
241 }
242  
243 =pod
244  
245 =head2 reposition_term
246  
247 This functions serves the purpose of reposition a term relatively to its
248 siblings. Giving a value that is to high or low will silently cancel the
249 operation. This function is generally called by the I<increase_...> and
250 I<decrease_...> functions.
251  
252 =over
253  
254 =item Parameters
255  
256 =over
257  
258 =item term
259  
260 An instance of I<CGI::Listman::dictionary::term> you wish to move in the
261 dictionary's internal list.
262  
263 =item delta
264  
265 An positive or negative integer giving the number of steps the term has
266 to be moved from.
267  
268 =back
269  
270 =item Return values
271  
272 This method returns nothing.
273  
274 =back
275  
276 =cut
277  
278 0 ▶sub reposition_term {
Subroutine reposition_term: not called CC=15 SCAR=54.8
Pod documented
279 0 my ($self, $term, $delta) = @_;
280  
281 0 my $curr_pos = $self->term_pos_in_list ($term);
282 0 my $new_pos = $curr_pos + $delta;
283 0 my $terms_ref = $self->{'_terms'};
284  
285 0 ▶ unless ($new_pos > scalar (@$terms_ref)
Branch: 1 branch
BranchTrueFalseTotal
unless ($new_pos > scalar @$terms_ref or $new_pos < 0 or $delta == 0) 0 0 0
Condition0%0/6
$new_pos > scalar @$terms_ref or $new_pos < 0 or $delta == 0
l!l&&r!l&&!r
000
$new_pos > scalar @$terms_ref or $new_pos < 0
l!l&&r!l&&!r
000
MC/DC0%0/3
$new_pos > scalar @$terms_ref or $new_pos < 0 or $delta == 0
$new_pos > scalar @$terms_ref $new_pos < 0 $delta == 0
Truth table0%0/4
A or B or C
A= $new_pos > scalar @$terms_refB= $new_pos < 0C= $delta == 0
ABCresult
1XX1
01X1
0011
0000
286 || $new_pos < 0
287 || $delta == 0) {
288 0 my @new_terms_list;
289  
290 0 ▶ for (my $count = 0; $count < @$terms_ref; $count++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count < @$terms_ref) 0 0 0
291 0 ▶ if ($delta > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($delta > 0) { } 0 0 0
292 0 ▶ push @new_terms_list, $terms_ref->[$count + 1]
Branch: 1 branch
BranchTrueFalseTotal
if $count < $new_pos and $count >= $curr_pos 0 0 0
Condition0%0/3
$count < $new_pos and $count >= $curr_pos
!ll&&!rl&&r
000
MC/DC0%0/2
$count < $new_pos and $count >= $curr_pos
$count < $new_pos $count >= $curr_pos
Truth table0%0/3
A and B
A= $count < $new_posB= $count >= $curr_pos
ABresult
0X0
100
111
293 if ($count < $new_pos && $count >= $curr_pos);
294 } else {
295 0 ▶ push @new_terms_list, $terms_ref->[$count - 1]
Branch: 1 branch
BranchTrueFalseTotal
if $count > $new_pos and $count <= $curr_pos 0 0 0
Condition0%0/3
$count > $new_pos and $count <= $curr_pos
!ll&&!rl&&r
000
MC/DC0%0/2
$count > $new_pos and $count <= $curr_pos
$count > $new_pos $count <= $curr_pos
Truth table0%0/3
A and B
A= $count > $new_posB= $count <= $curr_pos
ABresult
0X0
100
111
296 if ($count > $new_pos && $count <= $curr_pos);
297 }
298 0 ▶ push @new_terms_list, $terms_ref->[$count]
Branch: 1 branch
BranchTrueFalseTotal
if $count < $new_pos and $count < $curr_pos or $count > $new_pos and $count > $curr_pos 0 0 0
Condition0%0/9
$count < $new_pos and $count < $curr_pos or $count > $new_pos and $count > $curr_pos
l!l&&r!l&&!r
000
$count < $new_pos and $count < $curr_pos
!ll&&!rl&&r
000
$count > $new_pos and $count > $curr_pos
!ll&&!rl&&r
000
MC/DC0%0/4
$count < $new_pos and $count < $curr_pos or $count > $new_pos and $count > $curr_pos
$count < $new_pos $count < $curr_pos $count > $new_pos $count > $curr_pos
Truth table0%0/7
A and B or C and D
A= $count < $new_posB= $count < $curr_posC= $count > $new_posD= $count > $curr_pos
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
299 if (($count < $new_pos && $count < $curr_pos)
300 || ($count > $new_pos && $count > $curr_pos));
301 0 ▶ push @new_terms_list, $term
Branch: 1 branch
BranchTrueFalseTotal
if $count == $new_pos 0 0 0
302 if ($count == $new_pos);
303 }
304  
305 0 delete $self->{'_terms'};
306 0 $self->{'_terms'} = \@new_terms_list;
307 }
308 }
309  
310 =pod
311  
312 =head2 increase_term_pos
313  
314 This method is similar to I<reposition_term> above except that its
315 increment is optional. If not present, the increment is assumed to be 1.
316  
317 =over
318  
319 =item Parameters
320  
321 =over
322  
323 =item increment
324  
325 An optional positive or negative integer giving the number of steps the
326 term has to be moved from.
327  
328 =back
329  
330 =item Return values
331  
332 This method returns nothing.
333  
334 =back
335  
336 =cut
337  
338 0 ▶sub increase_term_pos {
Subroutine increase_term_pos: not called CC=2 SCAR=17.9
Pod documented
339 0 my ($self, $term, $increment) = @_;
340  
341 0 ▶ $increment = 1 unless (defined $increment);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $increment 0 0 0
342  
343 0 $self->reposition_term ($term, $increment);
344 }
345  
346 =pod
347  
348 =head2 decrease_term_pos
349  
350 This method is similar to I<reposition_term> above except that its
351 decrement is optional and considered negative. If not present, the
352 decrement is assumed to be 1.
353  
354 =over
355  
356 =item Parameters
357  
358 =over
359  
360 =item decrement
361  
362 An optional positive or negative integer giving the number of steps the
363 term has to be moved from.
364  
365 =back
366  
367 =item Return values
368  
369 This method returns nothing.
370  
371 =back
372  
373 =cut
374  
375 0 ▶sub decrease_term_pos {
Subroutine decrease_term_pos: not called CC=2 SCAR=17.9
Pod documented
376 0 my ($self, $term, $decrement) = @_;
377  
378 0 ▶ $decrement = 1 unless (defined $decrement);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $decrement 0 0 0
379  
380 0 $self->reposition_term ($term, -$decrement);
381 }
382  
383 =pod
384  
385 =head2 increase_term_pos_by_key
386  
387 =over
388  
389 =item Parameters
390  
391 =over
392  
393 =item key
394  
395 The key corresponding to the I<CGI::Listman::dictionary::term> you would
396 like to increase in the list.
397  
398 =item increment
399  
400 An integer representing the amount of slots to increment the term by. If not
401 specified, this amount will be 1.
402  
403 =back
404  
405 =item Return values
406  
407 This method returns nothing.
408  
409 =back
410  
411 =cut
412  
413 0 ▶sub increase_term_pos_by_key {
Subroutine increase_term_pos_by_key: not called CC=1 SCAR=6.9
Pod documented
414 0 my ($self, $key, $increment) = @_;
415  
416 0 my $term = $self->get_term ($key);
417  
418 0 $self->increase_term_pos ($term, $increment);
419 }
420  
421 =pod
422  
423 =head2 decrease_term_pos_by_key
424  
425 =over
426  
427 =item Parameters
428  
429 =over
430  
431 =item key
432  
433 The key corresponding to the I<CGI::Listman::dictionary::term> you would
434 like to move in the list.
435  
436 =item decrement
437  
438 An integer representing the amount of slots to decrement the term by. If not
439 specified, this amount will be 1.
440  
441 =back
442  
443 =item Return values
444  
445 This method returns nothing.
446  
447 =back
448  
449 =cut
450  
451 0 ▶sub decrease_term_pos_by_key {
Subroutine decrease_term_pos_by_key: not called CC=1 SCAR=6.9
Pod documented
452 0 my ($self, $key, $decrement) = @_;
453  
454 0 my $term = $self->get_term ($key);
455  
456 0 $self->decrease_term_pos ($term, $decrement);
457 }
458  
459 =pod
460  
461 =head2 save
462  
463 =over
464  
465 =item Parameters
466  
467 This method takes no parameter.
468  
469 =item Return values
470  
471 This method returns nothing.
472  
473 =back
474  
475 =cut
476  
477 0 ▶sub save {
Subroutine save: not called CC=8 SCAR=42.8
Pod documented
478 0 my $self = shift;
479  
480 open DOUTF, '>'.$self->{'filename'}
481 or croak "Could not open dictionary (\""
482 0 ▶ .$self->{'filename'}."\" for writing).\n";
Branch: 1 branch
BranchTrueFalseTotal
open DOUTF, '>' . $self->{"filename"} or croak('Could not open dictionary ("' . $self->{"filename"} . qq[" for writing).\n]) 0 0 0
483 0 my $terms_ref = $self->{'_terms'};
484 0 foreach my $term (@$terms_ref) {
485 0 my $line = $term->{'key'};
486 0 my $definition = $term->definition ();
487 0 ▶ $line .= ':'.$definition if (defined $definition && $definition ne '');
Branch: 1 branch
BranchTrueFalseTotal
if defined $definition and $definition ne "" 0 0 0
Condition0%0/3
defined $definition and $definition ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
defined $definition and $definition ne ""
defined $definition $definition ne ""
Truth table0%0/3
A and B
A= defined $definitionB= $definition ne ""
ABresult
0X0
100
111
488 0 ▶ if ($term->{'mandatory'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($term->{"mandatory"}) 0 0 0
489 0 ▶ $line .= (defined $definition && $definition ne '') ? ':!' : '::!';
Branch: 1 branch
BranchTrueFalseTotal
defined $definition && $definition ne '' ? : 0 0 0
Condition0%0/3
defined $definition and $definition ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
defined $definition and $definition ne ""
defined $definition $definition ne ""
Truth table0%0/3
A and B
A= defined $definitionB= $definition ne ""
ABresult
0X0
100
111
490 }
491 0 print DOUTF $line."\n";
492 }
493 0 close DOUTF;
494 }
495  
496 ### private methods
497  
498 0 ▶sub _load {
Subroutine _load: not called CC=8 SCAR=42.8
499 0 my $self = shift;
500  
501 0 ▶ return if $self->{'_loading'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_loading"} 0 0 0
502  
503 0 $self->{'_loading'} = 1;
504 croak "No dictionary filename.\n"
505 0 ▶ unless (defined $self->{'filename'});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"filename"} 0 0 0
506  
507 open DINF, $self->{'filename'}
508 0 ▶ or croak 'Could not open dictionary ("'.$self->{'filename'}.'")'."\n";
Branch: 1 branch
BranchTrueFalseTotal
open DINF, $self->{"filename"} or croak('Could not open dictionary ("' . $self->{"filename"} . '")' . "\n") 0 0 0
509  
510 0 my @terms;
511 0 ▶ while (<DINF>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline DINF)) 0 0 0
512 0 my $line = $_;
513 0 chomp $line;
514 0 $line =~ m/([^:]*)(:([^:]+)?(:([!]))?)?/;
515  
516 0 my $key = $1;
517 0 ▶ my $definition = $3 || '';
Condition0%0/2
$3 || ''
l!l
00
MC/DC0%0/1
$3 || ''
$3
Truth table0%0/2
A
A= $3
Aresult
11
00
518 0 ▶ my $mandatory = (defined $5 && $5 eq '!');
Condition0%0/3
defined $5 && $5 eq '!'
!ll&&!rl&&r
000
MC/DC0%0/2
defined $5 && $5 eq '!'
defined $5 $5 eq '!'
Truth table0%0/3
A && B
A= defined $5B= $5 eq '!'
ABresult
0X0
100
111
519  
520 0 ▶ croak "Dictionary entry \"".$key."\" is duplicated."
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->get_term($key) 0 0 0
521 if (defined $self->get_term ($key));
522  
523 my $term_object = CGI::Listman::dictionary::term->new ($key,
524 $definition,
525 $mandatory,
526 0 $self->{'count'});
527 0 push @terms, $term_object;
528 }
529 0 close DINF;
530  
531 0 $self->{'_terms'} = \@terms;
532 0 $self->{'_loading'} = 0;
533 }
534  
535 1 1;
536 __END__
↑ Summary Listman.pm »