Listman/dictionary/term.pm

filter: Statementstmt 25.0% 7 / 28 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 25.0% 2 / 8 Podpod 100% 6 / 6 totaltotal 22.7% 15 / 66 CC 8 SCAR 22.8
CC
8
Cov
16%
set_key
20.0
set_definition
12.0
new
6.0
CRAP 9.7
« Listman.pm ↑ Summary Listman/exporter.pm »
1 # term.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::term;
13  
14 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 =pod
17  
18 =head1 NAME
19  
20 CGI::Listman::dictionary::term - element of a I<CGI::Listman::dictionary>
21  
22 =head1 SYNOPSIS
23  
24 use CGI::Listman::dictionary::term;
25  
26 =head1 DESCRIPTION
27  
28 A I<CGI::Listman::dictionary::term> is what an instance of
29 I<CGI::Listman::dictionary> is made of. In our terminology, a "term" is
30 made of a "key" and a "definition". The similarity with the words in a
31 lingual dictionary is not innocent.
32  
33 Within a CGI script, you are most certainly receiving and transmitting
34 variables to a form. A couple of those variables will be of internal use
35 only while the others will be part of the interactivity with your users.
36 Generally, those variables will have names that are explicit to you as a
37 programmer; however they might mean nothing or little to a normal user.
38  
39 Now, in the context of I<CGI::Listman>, those variables are juste fields
40 of a database row and it makes even less sense to give them a
41 user-explicit name (imagine a name such as "Your father's forename"). So
42 the B<key> of a I<CGI::Listman::dictionary::line> will represent a
43 database column and the B<definition> will be its user representation.
44 This is mostly useful when you have to represent those variables in the
45 sight of your users; for example when validating user input (see
46 I<CGI::Listman>'s method I<check_params>).
47  
48 A I<CGI::Listman::dictionary::term> also contains validation information.
49 Currently it only supports a flag indicating whether the field is
50 mandatory or not but it will probably contain other flags in a later
51 release.
52  
53 =head1 API
54  
55 =head2 new
56  
57 This method is to instantiate a term. It optionnally takes arguments for
58 the term's key, definition and mandatory flag. All the parameters are
59 optional with this method.
60  
61 =over
62  
63 =item Parameters
64  
65 =over
66  
67 =item key
68  
69 A string representing the term's key.
70  
71 =item definition
72  
73 A string representing the term's definition.
74  
75 =item mandatory
76  
77 A boolean (0 or 1) integer defining whether user input is required or not
78 for this term.
79  
80 =back
81  
82 =item Return values
83  
84 This method returns a blessed reference to a
85 I<CGI::Listman::dictionary::term>.
86  
87 =back
88  
89 =cut
90  
91 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
92 0 my $class = shift;
93  
94 0 my $self = {};
95 0 $self->{'key'} = shift;
96 0 $self->{'_definition'} = shift;
97 0 ▶ $self->{'mandatory'} = shift || 0;
Condition0%0/2
shift() || 0
l!l
00
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
98  
99 0 bless $self, $class;
100 }
101  
102 =pod
103  
104 =head2 set_key
105  
106 This method set the term's key to the provided value.
107  
108 =over
109  
110 =item Parameters
111  
112 =over
113  
114 =item key
115  
116 A string representing the term's key.
117  
118 =back
119  
120 =item Return values
121  
122 This method returns nothing.
123  
124 =back
125  
126 =cut
127  
128 0 ▶sub set_key {
Subroutine set_key: not called CC=4 SCAR=30.0
Pod documented
129 0 my ($self, $key) = @_;
130  
131 0 ▶ croak "Bad key name.\n" unless (defined $key && $key ne '');
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key and $key ne "" 0 0 0
Condition0%0/3
defined $key and $key ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
defined $key and $key ne ""
defined $key $key ne ""
Truth table0%0/3
A and B
A= defined $keyB= $key ne ""
ABresult
0X0
100
111
132 croak 'This term already has a key name ("'.$self->{'key'}.")\"\n"
133 0 ▶ if (defined $self->{'key'});
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"key"} 0 0 0
134 0 $self->{'key'} = $key;
135 }
136  
137 =pod
138  
139 =head2 set_definition
140  
141 This method set the term's definition to the provided value.
142  
143 =over
144  
145 =item Parameters
146  
147 =over
148  
149 =item definition
150  
151 A string representing the term's definition.
152  
153 =back
154  
155 =item Return values
156  
157 This method returns nothing.
158  
159 =back
160  
161 =cut
162  
163 0 ▶sub set_definition {
Subroutine set_definition: not called CC=3 SCAR=24.8
Pod documented
164 0 my ($self, $definition) = @_;
165  
166 0 ▶ $definition = undef if (defined $definition
Branch: 1 branch
BranchTrueFalseTotal
if defined $definition and $definition =~ /^\s+$/ 0 0 0
Condition0%0/3
defined $definition and $definition =~ /^\s+$/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $definition and $definition =~ /^\s+$/
defined $definition $definition =~ /^\s+$/
Truth table0%0/3
A and B
A= defined $definitionB= $definition =~ /^\s+$/
ABresult
0X0
100
111
167 && ($definition =~ m/^\s+$/));
168 0 $self->{'_definition'} = $definition;
169 }
170  
171 =pod
172  
173 =head2 set_mandatory
174  
175 This method declares that user input is required for this term.
176  
177 =over
178  
179 =item Parameters
180  
181 This method take no parameter.
182  
183 =item Return values
184  
185 This method returns nothing.
186  
187 =back
188  
189 =cut
190  
191 0 ▶sub set_mandatory {
Subroutine set_mandatory: not called CC=1 SCAR=6.9
Pod documented
192 0 my $self = shift;
193  
194 0 $self->{'mandatory'} = 1;
195 }
196  
197 =pod
198  
199 =head2 definition
200  
201 This method returns the definition for the specified instance of
202 I<CGI::Listman::dictionary::term>.
203  
204 =over
205  
206 =item Parameters
207  
208 This method take no parameter.
209  
210 =item Return values
211  
212 This method returns a string representing either the term's definition.
213  
214 =back
215  
216 =cut
217  
218 0 ▶sub definition {
Subroutine definition: not called CC=1 SCAR=6.9
Pod documented
219 0 my $self = shift;
220  
221 0 my $definition = $self->{'_definition'};
222  
223 0 return $definition;
224 }
225  
226 =pod
227  
228 =head2 definition_or_key
229  
230 This method returns the definition for the specified instance of
231 I<CGI::Listman::dictionary::term>. If the definition is not existing, the
232 key is returned instead.
233  
234 =over
235  
236 =item Parameters
237  
238 This method take no parameter.
239  
240 =item Return values
241  
242 This method returns a string representing either the term's definition or
243 its key whenever its definition is void.
244  
245 =back
246  
247 =cut
248  
249 0 ▶sub definition_or_key {
Subroutine definition_or_key: not called CC=2 SCAR=17.9
Pod documented
250 0 my $self = shift;
251  
252 0 ▶ my $definition = $self->definition () || $self->{'key'};
Condition0%0/3
$self->definition || $self->{'key'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->definition || $self->{'key'}
$self->definition $self->{'key'}
Truth table0%0/3
A || B
A= $self->definitionB= $self->{'key'}
ABresult
1X1
011
000
253  
254 0 return $definition;
255 }
256  
257 1 1;
258 __END__
« Listman.pm ↑ Summary Listman/exporter.pm »