Flute/List.pm untested

filter: Statementstmt 0.0% 0 / 100 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 32 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 23 Podpod 0.0% 0 / 21 totaltotal 0.0% 0 / 204 CC 15 SCAR 25.0
CC
15
Cov
0%
new
30.0
input
30.0
filter
20.0
CRAP 12.2
1 package Template::Flute::List;
2  
3 use strict;
4 use warnings;
5  
6 =head1 NAME
7  
8 Template::Flute::List - List object for Template::Flute templates.
9  
10 =head1 CONSTRUCTOR
11  
12 =head2 new
13  
14 Creates Template::Flute::List object.
15  
16 =cut
17  
18 # Constructor
19 sub new {
20 my ($class, $sob, $static, $spec, $name) = @_;
21 my ($self, $lf);
22
23 $static ||= [];
24
25 $self = {sob => $sob, static => $static, valid_input => undef};
26  
27 if (exists $sob->{iterator}) {
28 $self->{iterator} = {name => $sob->{iterator}};
29 }
30 $self->{limit} = $sob->{limit} if defined $sob->{limit};
31
32 bless $self, $class;
33
34 if ($spec && $name) {
35 $self->inputs_add($spec->list_inputs($name));
36 $self->filters_add($spec->list_filters($name));
37 $self->sorts_add($spec->list_sorts($name));
38
39 if ($lf = $spec->list_paging($name)) {
40 $self->paging_add($lf);
41 }
42 }
43
44 return $self;
45 }
46  
47 =head1 METHODS
48  
49 =head2 params_add PARAMS
50  
51 Add parameters from PARAMS to list.
52  
53 =cut
54
55 sub params_add {
56 my ($self, $params) = @_;
57  
58 $self->{params} = $params || [];
59 }
60  
61 =head2 separators_add SEPARATORS
62  
63 Add separators from SEPARATORS to list:
64  
65 =cut
66  
67 sub separators_add {
68 my ($self, $separators) = @_;
69  
70 $self->{separators} = $separators || [];
71 }
72  
73 =head2 inputs_add INPUTS
74  
75 Add inputs from INPUTS to list.
76  
77 =cut
78  
79 sub inputs_add {
80 my ($self, $inputs) = @_;
81  
82 if (ref($inputs) eq 'HASH') {
83 $self->{inputs} = $inputs;
84 $self->{valid_input} = 0;
85 }
86 }
87  
88 =head2 increments_add INCREMENTS
89  
90 Add increments from INCREMENTS to list.
91  
92 =cut
93  
94 sub increments_add {
95 my ($self, $increments) = @_;
96  
97 $self->{increments} = $increments;
98 }
99  
100 =head2 filters_add FILTERS
101  
102 Add filters from FILTERS to list.
103  
104 =cut
105  
106 sub filters_add {
107 my ($self, $filters) = @_;
108  
109 $self->{filters} = $filters;
110 }
111  
112 =head2 sorts_add SORT
113  
114 Add sort from SORT to list.
115  
116 =cut
117  
118 sub sorts_add {
119 my ($self, $sort) = @_;
120  
121 $self->{sorts} = $sort;
122 }
123  
124 =head2 paging_add PAGING
125  
126 Add paging from PAGING to list.
127  
128 =cut
129
130 sub paging_add {
131 my ($self, $paging) = @_;
132  
133 $self->{paging} = $paging;
134 }
135  
136 =head2 name
137  
138 Returns name of the list.
139  
140 =cut
141  
142 sub name {
143 my ($self) = @_;
144  
145 return $self->{sob}->{name};
146 }
147  
148 =head2 iterator [ARG]
149  
150 Returns list iterator object when called without ARG.
151 Returns list iterator name when called with ARG 'name'.
152  
153 =cut
154
155 sub iterator {
156 my ($self, $arg) = @_;
157  
158 if (defined $arg && $arg eq 'name') {
159 return $self->{iterator}->{name};
160 }
161
162 return $self->{iterator}->{object};
163 }
164  
165 =head2 set_iterator ITERATOR
166  
167 Sets list iterator object to ITERATOR.
168  
169 =cut
170  
171 sub set_iterator {
172 my ($self, $iterator) = @_;
173
174 $self->{iterator}->{object} = $iterator;
175 }
176  
177 =head2 set_static_class CLASS
178  
179 Set static class for list to CLASS.
180  
181 =cut
182  
183 sub set_static_class {
184 my ($self, $class) = @_;
185  
186 push(@{$self->{static}}, $class);
187 }
188  
189 =head2 static_class ROW_POS
190  
191 Apply static class for ROW_POS.
192  
193 =cut
194
195 sub static_class {
196 my ($self, $row_pos) = @_;
197 my ($idx);
198  
199 if (@{$self->{static}}) {
200 $idx = $row_pos % scalar(@{$self->{static}});
201
202 return $self->{static}->[$idx];
203 }
204 }
205  
206 =head2 elt
207  
208 Returns corresponding HTML template element of the list.
209  
210 =cut
211  
212 sub elt {
213 my ($self) = @_;
214  
215 return $self->{sob}->{elts}->[0];
216 }
217  
218 =head2 params
219  
220 Returns list parameters.
221  
222 =cut
223  
224 sub params {
225 my ($self) = @_;
226  
227 return $self->{params};
228 }
229  
230 =head2 separators
231  
232 Return list separators.
233  
234 =cut
235  
236 sub separators {
237 my ($self) = @_;
238  
239 return $self->{separators};
240 }
241  
242 =head2 input PARAMS
243  
244 Verifies that input parameters are sufficient.
245 Returns 1 in case of success, 0 otherwise.
246  
247 =cut
248  
249 sub input {
250 my ($self, $params) = @_;
251 my ($error_count);
252  
253 if ((! $params || ! (keys %$params)) && $self->{valid_input} == 1) {
254 return 1;
255 }
256
257 $error_count = 0;
258 $params ||= {};
259
260 for my $input (values %{$self->{inputs}}) {
261 if ($input->{optional} && (! defined $params->{$input->{name}}
262 || $params->{$input->{name}} !~ /\S/)) {
263 # skip optional inputs without a value
264 next;
265 }
266 if ($input->{required} && (! defined $params->{$input->{name}}
267 || $params->{$input->{name}} !~ /\S/)) {
268 warn "Missing input for $input->{name}.\n";
269 $error_count++;
270 }
271 else {
272 $input->{value} = $params->{$input->{name}};
273 }
274 }
275  
276 if ($error_count) {
277 return 0;
278 }
279  
280 $self->{valid_input} = 1;
281 return 1;
282 }
283  
284 =head2 set_limit TYPE LIMIT
285  
286 Set list limit for type TYPE to LIMIT.
287  
288 =cut
289  
290 # set_limit method - set list limit
291 sub set_limit {
292 my ($self, $type, $limit) = @_;
293  
294 $self->{limits}->{$type} = $limit;
295 }
296  
297 =head2 set_filter NAME
298  
299 Set global filter for list to NAME.
300  
301 =cut
302
303 sub set_filter {
304 my ($self, $name) = @_;
305  
306 $self->{filter} = $name;
307 }
308  
309 =head2 filter FLUTE ROW
310  
311 Run row filter on ROW if applicable.
312  
313 =cut
314
315 sub filter {
316 my ($self, $flute, $row) = @_;
317 my ($new_row);
318
319 if ($self->{filters}) {
320 if (ref($self->{filters}) eq 'HASH') {
321 $new_row = $row;
322
323 for my $f (keys %{$self->{filters}}) {
324 $new_row = $flute->filter($f, $new_row);
325 return unless $new_row;
326 }
327  
328 return $new_row;
329 }
330  
331 return $flute->filter($self->{filters}, $row);
332 }
333
334 return $row;
335 }
336  
337 =head2 increment
338  
339 Increment all increments of the list.
340  
341 =cut
342  
343 sub increment {
344 my ($self) = @_;
345  
346 for my $inc (@{$self->{increments}}) {
347 $inc->increment();
348 }
349 }
350  
351 =head1 AUTHOR
352  
353 Stefan Hornburg (Racke), <racke@linuxia.de>
354  
355 =head1 LICENSE AND COPYRIGHT
356  
357 Copyright 2010-2019 Stefan Hornburg (Racke) <racke@linuxia.de>.
358  
359 This program is free software; you can redistribute it and/or modify it
360 under the terms of either: the GNU General Public License as published
361 by the Free Software Foundation; or the Artistic License.
362  
363 See http://dev.perl.org/licenses/ for more information.
364  
365 =cut
366  
367 1;