Flute/Specification.pm untested

filter: Statementstmt 0.0% 0 / 293 Branchbran 0.0% 0 / 76 Conditioncond 0.0% 0 / 51 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 30 Podpod 0.0% 0 / 27 totaltotal 0.0% 0 / 477 CC 39 SCAR 28.7
CC
39
Cov
0%
form_add
56.0
list_add
42.0
value_add
30.0
CRAP 17.7
1 package Template::Flute::Specification;
2  
3 use strict;
4 use warnings;
5  
6 use Template::Flute::Iterator;
7  
8 =head1 NAME
9  
10 Template::Flute::Specification - Specification class for Template::Flute
11  
12 =head1 SYNOPSIS
13  
14 $xml_spec = new Template::Flute::Specification::XML;
15 $spec = $xml_spec->parse_file('spec.xml');
16 $spec->set_iterator('cart', $cart);
17  
18 $conf_spec = new Template::Flute::Specification::Scoped;
19 $spec = $conf_spec->parse_file('spec.conf);
20  
21 =head1 DESCRIPTION
22  
23 Specification class for L<Template::Flute>.
24  
25 =head1 CONSTRUCTOR
26  
27 =head2 new
28  
29 Creates Template::Flute::Specification object.
30  
31 =cut
32  
33 # Constructor
34  
35 sub new {
36 my ($class, $self);
37 my (%params);
38  
39 $class = shift;
40 %params = (encoding => 'utf8', @_);
41  
42 $self = \%params;
43  
44 # lookup hash for elements by class
45 $self->{classes} = {};
46  
47 # lookup hash for elements by id
48 $self->{ids} = {};
49  
50 # lookup hash for elements by name attribute
51 $self->{names} = {};
52  
53 $self->{pagings} = {};
54  
55 # named patterns
56 $self->{patterns} = {};
57  
58 bless $self, $class;
59 }
60  
61 sub _ids {
62 return keys %{shift->{ids}};
63 }
64  
65 sub _classes {
66 return keys %{shift->{classes}};
67 }
68  
69 sub _names {
70 return keys %{shift->{names}};
71 }
72  
73 =head1 METHODS
74  
75 =head2 name NAME
76  
77 Set or get the name of the specification.
78  
79 =cut
80  
81 sub name {
82 my $self = shift;
83  
84 if (scalar @_ > 0) {
85 $self->{name} = shift;
86 }
87  
88 return $self->{name};
89 }
90  
91 =head2 encoding ENCODING
92  
93 Set or get the encoding of the HTML template
94 which is parsed according to this specification.
95  
96 =cut
97
98 sub encoding {
99 my $self = shift;
100  
101 if (scalar @_ > 0) {
102 $self->{encoding} = shift;
103 }
104  
105 return $self->{encoding};
106 }
107  
108 =head2 container_add CONTAINER
109  
110 Add container specified by hash reference CONTAINER.
111  
112 =cut
113  
114 sub container_add {
115 my ($self, $new_containerref) = @_;
116 my ($containerref, $container_name, $id, $class);
117  
118 $container_name = $new_containerref->{container}->{name};
119  
120 $containerref = $self->{containers}->{$new_containerref->{container}->{name}} = {input => {}};
121  
122 $class = $new_containerref->{container}->{class} || $container_name;
123  
124 if ($id = $new_containerref->{container}->{id}) {
125 push @{$self->{ids}->{$id}}, {%{$new_containerref->{container}}, type => 'container'};
126 }
127 else {
128 $self->{classes}->{$class} = [{%{$new_containerref->{container}}, type => 'container'}];
129 }
130  
131 # loop through values for this container
132 for my $value (@{$new_containerref->{value}}) {
133 if ($value->{id}) {
134 push @{$self->{ids}->{$value->{id}}}, {%{$value}, type => 'value', container => $container_name};
135 }
136 else {
137 $class = $value->{class} || $value->{name};
138 unless ($class) {
139 die "Neither class nor name for value within container $container_name.\n";
140 }
141 push @{$self->{classes}->{$class}}, {%{$value}, type => 'value', container => $container_name};
142 }
143 }
144  
145 return $containerref;
146 }
147  
148 =head2 list_add LIST
149  
150 Add list specified by hash reference LIST.
151  
152 =cut
153
154 sub list_add {
155 my ($self, $new_listref) = @_;
156 my ($listref, $list_name, $class);
157  
158 $list_name = $new_listref->{list}->{name};
159  
160 $listref = $self->{lists}->{$new_listref->{list}->{name}} = {input => {}};
161  
162 $class = $new_listref->{list}->{class} || $list_name;
163  
164 $self->{classes}->{$class} = [{%{$new_listref->{list}}, type => 'list'}];
165  
166 if (exists $new_listref->{list}->{iterator}) {
167 $listref->{iterator} = $new_listref->{list}->{iterator};
168 }
169  
170 # loop through filters for this list
171 for my $filter (@{$new_listref->{filter}}) {
172 $listref->{filter}->{$filter->{name}} = $filter;
173 }
174  
175 # loop through inputs for this list
176 for my $input (@{$new_listref->{input}}) {
177 $listref->{input}->{$input->{name}} = $input;
178 }
179  
180 # loop through sorts for this list
181 for my $sort (@{$new_listref->{sort}}) {
182 $listref->{sort}->{$sort->{name}} = $sort;
183 }
184  
185 # loop through containers for this list
186 for my $container (@{$new_listref->{container}}) {
187 $class = $container->{class} || $container->{name};
188 unless ($class) {
189 die "Neither class nor name for container within list $list_name.\n";
190 }
191  
192 push @{$self->{classes}->{$class}}, {%{$container}, type => 'container', list => $list_name};
193 }
194  
195 # loop through separators for this list
196 for my $separator (@{$new_listref->{separator}}) {
197 $class = $separator->{class} || $separator->{name};
198 unless ($class) {
199 die "Neither class nor name for separator within list $list_name.\n";
200 }
201 $listref->{separator}->{$separator->{name}} = $separator;
202 push @{$self->{classes}->{$class}}, {%{$separator}, type => 'separator', list => $list_name};
203 }
204  
205 # loop through params for this list
206 for my $param (@{$new_listref->{param}}) {
207 $class = $param->{class} || $param->{name};
208 unless ($class) {
209 die "Neither class nor name for param within list $list_name.\n";
210 }
211 push @{$self->{classes}->{$class}}, {%{$param}, type => 'param', list => $list_name};
212 }
213  
214 # loop through paging for this list
215 for my $paging (@{$new_listref->{paging}}) {
216 if (exists $listref->{paging}) {
217 die "Only one paging allowed per list\n";
218 }
219 $listref->{paging} = $paging;
220 $class = $paging->{class} || $paging->{name};
221 $self->{classes}->{$class} = [{%{$paging}, type => 'paging', list => $list_name}];
222 }
223
224 return $listref;
225 }
226  
227 =head2 paging_add PAGING
228  
229 =cut
230  
231 sub paging_add {
232 my ($self, $new_pagingref) = @_;
233 my ($name, $class, $pagingref);
234  
235 $name = $new_pagingref->{paging}->{name};
236  
237 $pagingref = $self->{pagings}->{$name} = {elements => $new_pagingref->{paging}->{elements}, list => $new_pagingref->{paging}->{list}};
238  
239 # loop through paging elements
240 for my $element (values %{$new_pagingref->{paging}->{elements}}) {
241 $class = $element->{class} || $element->{name};
242 push @{$self->{classes}->{$class}}, {%{$element}, element_type => $element->{type}, type => 'element', list => $new_pagingref->{paging}->{list}, paging => $name};
243 }
244
245 $class = $new_pagingref->{paging}->{class} || $name;
246  
247 push @{$self->{classes}->{$class}}, {%{$new_pagingref->{paging}}, type => 'paging'};
248  
249 return $pagingref;
250 }
251  
252 =head2 form_add FORM
253  
254 Add form specified by hash reference FORM.
255  
256 =cut
257
258 sub form_add {
259 my ($self, $new_formref) = @_;
260 my ($formref, $form_name, $form_link, $form_loc, $field_loc, $id, $class);
261  
262 $form_name = $new_formref->{form}->{name};
263 $form_link = $new_formref->{form}->{link} || '';
264
265 $formref = $self->{forms}->{$new_formref->{form}->{name}} = {input => {}};
266  
267 my @checks = qw/id class/;
268  
269 $form_loc = {%{$new_formref->{form}}, type => 'form'};
270
271 if ($id = $new_formref->{form}->{id}) {
272 $self->{ids}->{$id} = [$form_loc];
273 }
274 elsif ($class = $new_formref->{form}->{class}) {
275 $class = $new_formref->{form}->{class};
276  
277 $self->{classes}->{$class} = [$form_loc];
278 }
279 elsif ($form_link eq 'name') {
280 $self->{names}->{$form_name} = [$form_loc];
281 }
282 else {
283 $class = $form_name;
284
285 $self->{classes}->{$class} = [$form_loc];
286 }
287
288 # loop through inputs for this form
289 for my $input (@{$new_formref->{input}}) {
290 $formref->{input}->{$input->{name}} = $input;
291 }
292
293 # loop through params for this form
294 for my $param (@{$new_formref->{param}}) {
295 $class = $param->{class} || $param->{name};
296  
297 push @{$self->{classes}->{$class}}, {%{$param}, type => 'param', form => $form_name};
298 }
299  
300 # loop through fields for this form
301 for my $field (@{$new_formref->{field}}) {
302 $field_loc = {%{$field}, type => 'field', form => $form_name};
303  
304 if (exists $field->{id}) {
305 push @{$self->{ids}->{$field->{id}}}, $field_loc;
306 }
307 elsif (exists $field->{class}) {
308 push @{$self->{classes}->{$field->{class}}}, $field_loc;
309 }
310 elsif ($form_link eq 'name') {
311 push @{$self->{names}->{$field->{name}}}, $field_loc;
312 }
313 else {
314 push @{$self->{classes}->{$field->{name}}}, $field_loc;
315 }
316 }
317
318 return $formref;
319 }
320  
321 =head2 value_add VALUE
322  
323 Add value specified by hash reference VALUE.
324  
325 =cut
326
327 sub value_add {
328 my ($self, $new_valueref) = @_;
329 my ($valueref, $value_name, $id, $class);
330  
331 $value_name = $new_valueref->{value}->{name};
332  
333 unless (defined $value_name && $value_name =~ /\S/) {
334 die "Value needs a name attribute.";
335 }
336  
337 if (exists $new_valueref->{value}->{include}) {
338 # include implies hooking resulting value
339 $new_valueref->{value}->{op} = 'hook';
340 }
341 elsif (exists $new_valueref->{value}->{field}
342 && $new_valueref->{value}->{field} =~ /\./) {
343 $new_valueref->{value}->{field} = [split /\./, $new_valueref->{value}->{field}];
344 }
345  
346 $valueref = $self->{values}->{$new_valueref->{value}->{name}} = {};
347
348 if ($id = $new_valueref->{value}->{id}) {
349 push @{$self->{ids}->{$id}}, {%{$new_valueref->{value}}, type => 'value'};
350 }
351 else {
352 $class = $new_valueref->{value}->{class} || $value_name;
353  
354 push @{$self->{classes}->{$class}}, {%{$new_valueref->{value}}, type => 'value'};
355 }
356  
357 return $valueref;
358 }
359  
360 =head2 i18n_add I18N
361  
362 Add i18n specified by hash reference I18N.
363  
364 =cut
365
366 sub i18n_add {
367 my ($self, $new_i18nref) = @_;
368 my ($i18nref, $i18n_name, $id, $class);
369  
370 $i18n_name = $new_i18nref->{value}->{name}
371 || $new_i18nref->{value}->{class};
372
373 $i18nref = $self->{i18n}->{$i18n_name} = {};
374
375 if ($id = $new_i18nref->{value}->{id}) {
376 push @{$self->{ids}->{$id}}, {%{$new_i18nref->{value}}, type => 'i18n'};
377 }
378 else {
379 $class = $new_i18nref->{value}->{class} || $i18n_name;
380  
381 push @{$self->{classes}->{$class}}, {%{$new_i18nref->{value}}, type => 'i18n'};
382 }
383
384 return $i18nref;
385 }
386  
387 =head2 pattern_add({ name => 'pxt', regexp => qr/\Qmy string\E/ });
388  
389 Add a pattern to the specification. The two keys C<name> and
390 C<regexp> are mandatory.
391  
392 =head2 patterns
393  
394 Returns a plain hash with name => regexp pairs of the patterns set in
395 the specification.
396  
397 =cut
398  
399 sub pattern_add {
400 my ($self, $pattern) = @_;
401 my $name = $pattern->{name} or die "Couldn't add pattern: missing name";
402 my $regexp = $pattern->{regexp} or die "Missing regexp for pattern $name";
403 # print "Adding $name $regexp\n";
404 $self->{patterns}->{$name} = $regexp;
405 # print Dumper($self->{patterns});
406 }
407  
408 sub patterns {
409 return %{shift->{patterns}};
410 }
411  
412  
413  
414 =head2 list_iterator NAME
415  
416 Returns iterator for list named NAME or undef.
417  
418 =cut
419  
420 sub list_iterator {
421 my ($self, $list_name) = @_;
422  
423 if (exists $self->{lists}->{$list_name}) {
424 return $self->{lists}->{$list_name}->{iterator};
425 }
426 }
427  
428 =head2 list_inputs NAME
429  
430 Returns inputs for list named NAME or undef.
431  
432 =cut
433
434 sub list_inputs {
435 my ($self, $list_name) = @_;
436  
437 if (exists $self->{lists}->{$list_name}) {
438 return $self->{lists}->{$list_name}->{input};
439 }
440 }
441  
442 =head2 list_sorts NAME
443  
444 Return sorts for list named NAME or undef.
445  
446 =cut
447  
448 sub list_sorts {
449 my ($self, $list_name) = @_;
450  
451 if (exists $self->{lists}->{$list_name}) {
452 return $self->{lists}->{$list_name}->{sort};
453 }
454 }
455  
456 =head2 list_filters NAME
457  
458 Return filters for list named NAME or undef.
459  
460 =cut
461  
462 sub list_filters {
463 my ($self, $list_name) = @_;
464  
465 if (exists $self->{lists}->{$list_name}) {
466 return $self->{lists}->{$list_name}->{filter};
467 }
468 }
469  
470 =head2 form_inputs NAME
471  
472 Return inputs for form named NAME or undef.
473  
474 =cut
475  
476 sub form_inputs {
477 my ($self, $form_name) = @_;
478  
479 if (exists $self->{forms}->{$form_name}) {
480 return $self->{forms}->{$form_name}->{input};
481 }
482 }
483  
484 =head2 iterator NAME
485  
486 Returns iterator identified by NAME.
487  
488 =cut
489  
490 sub iterator {
491 my ($self, $name) = @_;
492  
493 if (exists $self->{iters}->{$name}) {
494 return $self->{iters}->{$name};
495 }
496 }
497  
498 =head2 set_iterator NAME ITER
499  
500 Sets iterator for NAME to ITER. ITER can be a iterator
501 object like L<Template::Flute::Iterator> or a reference
502 to an array containing hash references.
503  
504 =cut
505  
506 sub set_iterator {
507 my ($self, $name, $iter) = @_;
508 my ($iter_ref);
509  
510 $iter_ref = ref($iter);
511  
512 if ($iter_ref eq 'ARRAY') {
513 $iter = new Template::Flute::Iterator($iter);
514 }
515
516 $self->{iters}->{$name} = $iter;
517 }
518  
519 =head2 resolve_iterator INPUT
520  
521 Resolves iterator INPUT.
522  
523 =cut
524  
525 sub resolve_iterator {
526 my ($self, $input) = @_;
527 my ($input_ref, $iter);
528  
529 $input_ref = ref($input);
530  
531 if ($input_ref eq 'ARRAY') {
532 $iter = new Template::Flute::Iterator($input);
533 }
534 elsif ($input_ref) {
535 # iterator already resolved
536 $iter = $input_ref;
537 }
538 elsif (exists $self->{iters}->{$input}) {
539 $iter = $self->{iters}->{$input};
540 }
541 else {
542 die "Failed to resolve iterator $input.\n";
543 }
544  
545 return $iter;
546 }
547  
548 =head2 elements_by_class NAME
549  
550 Returns element(s) of the specification tied to HTML class NAME or undef.
551  
552 =cut
553  
554 sub elements_by_class {
555 my ($self, $class) = @_;
556  
557 if (exists $self->{classes}->{$class}) {
558 return $self->{classes}->{$class};
559 }
560  
561 return;
562 }
563  
564 =head2 elements_by_name NAME
565  
566 Returns element(s) of the specification tied to HTML attribute name or undef.
567  
568 =cut
569  
570 sub elements_by_name {
571 my ($self, $name) = @_;
572  
573 if (exists $self->{names}->{$name}) {
574 return $self->{names}->{$name};
575 }
576  
577 return;
578 }
579  
580 =head2 elements_by_id NAME
581  
582 Returns element(s) of the specification tied to HTML id NAME or undef.
583  
584 =cut
585  
586 sub elements_by_id {
587 my ($self, $id) = @_;
588  
589 if (exists $self->{ids}->{$id}) {
590 return $self->{ids}->{$id};
591 }
592  
593 return;
594 }
595  
596 =head2 list_paging NAME
597  
598 Returns paging for list NAME.
599  
600 =cut
601
602 sub list_paging {
603 my ($self, $list_name) = @_;
604 my ($name, $paging_ref);
605  
606 if (exists $self->{lists}->{$list_name}) {
607 while (($name, $paging_ref) = each %{$self->{pagings}}) {
608 if ($paging_ref->{list} eq $list_name) {
609 return $paging_ref;
610 }
611 }
612 }
613 }
614  
615 =head2 dangling
616  
617 Method to check if the template is consistent with the specification.
618 The method retrieves the list of ids, classes and names, and check if
619 there are template elements attached.
620  
621 For each specification element without template elements attached,
622 it produces a hash reference with the name, type and a dump of the element.
623  
624 It returns a list of these hash references, so you can check the template with
625  
626 my $flute = Template::Flute->new(....);
627 my @bad_elts = $flute->specification->dangling;
628  
629 if (@bad_elts) {
630 warn "empty elements" . Dumper(\@bad_elts);
631 }
632 else {
633 print "all ok\n";
634 }
635  
636 Each hashref returned has the following keys set:
637  
638 =over 4
639  
640 =item name
641  
642 =item type
643  
644 =item dump
645  
646 =back
647  
648 Beware that to call this method successfully, the specification must
649 already be processed, so it's safer to call it after C<$flute-E<gt>process>.
650  
651 =cut
652  
653  
654 sub dangling {
655 my $self = shift;
656 my @empty;
657 my %methods = (
658 id => {
659 list => '_ids',
660 elts => 'elements_by_id',
661 },
662 name => {
663 list => '_names',
664 elts => 'elements_by_name',
665 },
666 class => {
667 list => '_classes',
668 elts => 'elements_by_class',
669 },
670 );
671 foreach my $internal (keys %methods) {
672 my $method = $methods{$internal}{list};
673 my $get_elements = $methods{$internal}{elts};
674  
675 my @structs = $self->$method;
676 foreach my $struct (@structs) {
677 # here we have to look in the internals
678 if (my $arrayref = $self->$get_elements($struct)) {
679 foreach my $el (@$arrayref) {
680 unless (exists ($el->{elts})) {
681 push @empty, {
682 type => $internal,
683 name => $struct,
684 dump => $el,
685 }
686 }
687 }
688 }
689 }
690 }
691 return @empty;
692 }
693  
694 =head1 AUTHOR
695  
696 Stefan Hornburg (Racke), C<< <racke at linuxia.de> >>
697  
698 =head1 BUGS
699  
700 Please report any bugs or feature requests at L<https://github.com/racke/Template-Flute/issues>.
701  
702 =head1 LICENSE AND COPYRIGHT
703  
704 Copyright 2010-2019 Stefan Hornburg (Racke) <racke@linuxia.de>.
705  
706 This program is free software; you can redistribute it and/or modify it
707 under the terms of either: the GNU General Public License as published
708 by the Free Software Foundation; or the Artistic License.
709  
710 See http://dev.perl.org/licenses/ for more information.
711  
712 =cut
713
714 1;