Listman/line.pm

filter: Statementstmt 14.5% 7 / 48 Branchbran 0.0% 0 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 20.0% 2 / 10 Podpod 100% 7 / 7 totaltotal 22.5% 16 / 71 CC 4 SCAR 13.1
CC
4
Cov
13%
set_fields
6.0
update_fields
6.0
_build_from_listman_data
6.0
CRAP 3.7
« Listman/selection.pm ↑ Summary
1 # line.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::line;
13  
14 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 =pod
17  
18 =head1 NAME
19  
20 CGI::Listman::line - the internal representation of a database row
21  
22 =head1 SYNOPSIS
23  
24 use CGI::Listman::line;
25  
26 =head1 DESCRIPTION
27  
28 A I<CGI::Listman::line> represents a row of database stored in a way that
29 permits I<CGI::Listman> and the developer to easily manage some aspects of
30 it. For example, it also contains data on when the row was created, if it
31 was exported through a I<CGI::Listman::exporter>, if it was listed or
32 displayed in some way within a web interface.
33  
34 The order in which the fields are specified or used should be the same
35 throughout the use of the same instance of I<CGI::Listman>.
36  
37 =head1 API
38  
39 =head2 new
40  
41 This method is to instantiate a I<CGI::Listman::line>. It optionnally
42 takes an array ref as argument which will be the database row this
43 instance will represent in the framework of I<CGI::Listman>.
44  
45 =over
46  
47 =item Parameters
48  
49 The parameter "data" is optional with this method.
50  
51 =over
52  
53 =item data
54  
55 A reference to an ARRAY representing a database record. This parameter is
56 optional.
57  
58 =back
59  
60 =item Return values
61  
62 This method returns a blessed reference to a I<CGI::Listman::line>.
63  
64 =back
65  
66 =cut
67  
68 # line format: (number, timestamp, seen, exported, fields...)
69 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
70 0 my $class = shift;
71  
72 0 my $self = {};
73 0 $self->{'number'} = 0;
74 0 $self->{'timestamp'} = 0;
75 0 $self->{'seen'} = 0;
76 0 $self->{'exported'} = 0;
77 0 $self->{'data'} = shift;
78  
79 0 $self->{'_updated'} = 1;
80 0 $self->{'_new_line'} = 1;
81 0 $self->{'_deleted'} = 0;
82  
83 0 bless $self, $class;
84 }
85  
86 =pod
87  
88 =head2 mark_seen
89  
90 This method should be called whenever the line is considered as being
91 displayed on a user interface.
92  
93 =over
94  
95 =item Parameters
96  
97 This method takes no parameter.
98  
99 =item Return values
100  
101 This method returns nothing.
102  
103 =back
104  
105 =cut
106  
107 0 ▶sub mark_seen {
Subroutine mark_seen: not called CC=1 SCAR=6.9
Pod documented
108 0 my $self = shift;
109  
110 0 $self->{'seen'} = 1;
111 0 $self->{'_updated'} = 1;
112 }
113  
114 =pod
115  
116 =head2 mark_exported
117  
118 This method is called internally by I<CGI::Listman> whenever the line is
119 added to an instance of I<CGI::Listman::exporter>. It should not
120 otherwise be used directly by the developer, except when using its own
121 implementation of an exporter, but please choose to extend the
122 I<CGI::Listman::exporter> API instead.
123  
124 =over
125  
126 =item Parameters
127  
128 This method takes no parameter.
129  
130 =item Return values
131  
132 This method returns nothing.
133  
134 =back
135  
136 =cut
137  
138 0 ▶sub mark_exported {
Subroutine mark_exported: not called CC=1 SCAR=6.9
Pod documented
139 0 my $self = shift;
140  
141 0 $self->{'exported'} = 1;
142 0 $self->{'_updated'} = 1;
143 }
144  
145 =pod
146  
147 =head2 number
148  
149 This method returns the integer representing its position in the database
150 table. This number will be assigned only if the instance get added to a
151 I<CGI::Listman>, otherwise it will stay at 0.
152  
153 =over
154  
155 =item Parameters
156  
157 This method takes no parameter.
158  
159 =item Return values
160  
161 An integer representing the position of this line.
162  
163 =back
164  
165 =cut
166  
167 0 ▶sub number {
Subroutine number: not called CC=1 SCAR=6.9
Pod documented
168 0 my $self = shift;
169  
170 0 return $self->{'number'};
171 }
172  
173 =pod
174  
175 =head2 set_fields
176  
177 This will set the actual data row of this instance if not previously set
178 by new or this method.
179  
180 =over
181  
182 =item Parameters
183  
184 =over
185  
186 =item fields
187  
188 A reference to an ARRAY representing a database record.
189  
190 =back
191  
192 =item Return values
193  
194 This method returns nothing.
195  
196 =back
197  
198 =cut
199  
200 0 ▶sub set_fields {
Subroutine set_fields: not called CC=2 SCAR=17.9
Pod documented
201 0 my ($self, $fields_ref) = @_;
202  
203 croak "Fields already defined for line.\n"
204 0 ▶ if (defined $self->{'data'});
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"data"} 0 0 0
205  
206 0 $self->{'data'} = $fields_ref;
207 0 $self->{'_updated'} = 1;
208 }
209  
210 =pod
211  
212 =head2 update_fields
213  
214 Since I<set_fields> can only be called once and updating a row requires
215 some internal handling, you need to use this method whenever you update
216 a line's data.
217  
218 =over
219  
220 =item Parameters
221  
222 =over
223  
224 =item fields
225  
226 A reference to an ARRAY representing a database record.
227  
228 =back
229  
230 =item Return values
231  
232 This method returns nothing.
233  
234 =back
235  
236 =cut
237  
238 0 ▶sub update_fields {
Subroutine update_fields: not called CC=2 SCAR=17.9
Pod documented
239 0 my ($self, $fields_ref) = @_;
240  
241 delete $self->{'data'}
242 0 ▶ if (defined $self->{'data'});
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"data"} 0 0 0
243  
244 0 $self->{'data'} = $fields_ref;
245 0 $self->{'_updated'} = 1;
246 }
247  
248 =pod
249  
250 =head2 line_fields
251  
252 Use this method whenever you want to access the data encapsulated within
253 this object. Be careful to not modify the resulting fields unless you
254 want to do so since it returns the actual data and not a copy.
255  
256 =over
257  
258 =item Parameters
259  
260 This method takes no parameter.
261  
262 =item Return values
263  
264 A reference to an ARRAY representing the instance of I<CGI::Listman::line>.
265  
266 =back
267  
268 =cut
269  
270 0 ▶sub line_fields {
Subroutine line_fields: not called CC=1 SCAR=6.9
Pod documented
271 0 my $self = shift;
272  
273 0 return $self->{'data'};
274 }
275  
276 ### private methods
277  
278 0 ▶sub _build_from_listman_data {
Subroutine _build_from_listman_data: not called CC=2 SCAR=17.9
279 0 my ($self, $listman_data_ref) = @_;
280  
281 0 my @backend_data = @$listman_data_ref;
282  
283 0 my $number = shift @backend_data;
284 0 $number =~ m/^([0-9]*)$/;
285 0 ▶ $number = $1 or croak 'Wrong number ("'.$number
Branch: 1 branch
BranchTrueFalseTotal
$number = $1 or croak('Wrong number ("' . $number . '") containing non-digit characters' . "\n") 0 0 0
286 .'") containing non-digit characters'."\n";
287  
288 0 $self->{'number'} = $number;
289 0 $self->{'timestamp'} = shift @backend_data;
290 0 $self->{'seen'} = shift @backend_data;
291 0 $self->{'exported'} = shift @backend_data;
292 0 $self->{'data'} = \@backend_data;
293  
294 0 $self->{'_updated'} = 0;
295 0 $self->{'_new_line'} = 0;
296 }
297  
298 1 1;
299 __END__
« Listman/selection.pm ↑ Summary