lib/PDF/FromHTML/Template/Container/Conditional.pm untested

filter: Statementstmt 0.0% 0 / 92 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 42 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 177 CC 13 SCAR 33.6
CC
13
Cov
0%
conditional_passes
72.0
render
12.0
max_of
6.0
CRAP 28.7
1 package PDF::FromHTML::Template::Container::Conditional;
2  
3 #GGG Convert <conditional> to be a special case of <switch>?
4  
5 use strict;
6  
7 BEGIN {
8 use vars qw(@ISA);
9 @ISA = qw(PDF::FromHTML::Template::Container);
10  
11 use PDF::FromHTML::Template::Container;
12 }
13  
14 my %isOp = (
15 '=' => '==',
16 (map { $_ => $_ } ( '>', '<', '==', '!=', '>=', '<=' )),
17 (map { $_ => $_ } ( 'gt', 'lt', 'eq', 'ne', 'ge', 'le' )),
18 );
19  
20 # This cannot be within a should_render() function because the conditional needs
21 # to return true even if the conditional is false. We are indicating that this
22 # branch has done everything it needs to do, not that this branch is calling for
23 # a pagebreak.
24  
25 sub conditional_passes
26 {
27 my $self = shift;
28 my ($context) = @_;
29  
30 my $name = $context->get($self, 'NAME');
31 return 0 unless $name =~ /\S/;
32  
33 my $val = $context->param($name);
34 $val = @{$val} while UNIVERSAL::isa($val, 'ARRAY');
35 $val = ${$val} while UNIVERSAL::isa($val, 'SCALAR');
36  
37 my $istrue = (defined $val && $val) ? 1 : 0;
38 my $value = $context->get($self, 'VALUE');
39 if (defined $value)
40 {
41 my $op = $context->get($self, 'OP');
42 $op = defined $op && exists $isOp{$op}
43 ? $isOp{$op}
44 : '==';
45  
46 my $res;
47 for ($op)
48 {
49 /^>$/ && do { $res = ($val > $value); last };
50 /^<$/ && do { $res = ($val < $value); last };
51 /^==$/ && do { $res = ($val == $value); last };
52 /^!=$/ && do { $res = ($val != $value); last };
53 /^>=$/ && do { $res = ($val >= $value); last };
54 /^<=$/ && do { $res = ($val <= $value); last };
55 /^gt$/ && do { $res = ($val gt $value); last };
56 /^lt$/ && do { $res = ($val lt $value); last };
57 /^eq$/ && do { $res = ($val eq $value); last };
58 /^ne$/ && do { $res = ($val ne $value); last };
59 /^ge$/ && do { $res = ($val ge $value); last };
60 /^le$/ && do { $res = ($val le $value); last };
61  
62 die "Unknown operator '$op' in conditional resolve", $/;
63 }
64  
65 return 1;
66 }
67 elsif (my $is = uc $context->get($self, 'IS'))
68 {
69 if ($is eq 'TRUE')
70 {
71 return $istrue;
72 }
73 else
74 {
75 warn "Conditional 'is' value was [$is], defaulting to 'FALSE'" . $/
76 if $is ne 'FALSE';
77  
78 return !$istrue;
79 }
80 }
81  
82 return $istrue;
83 }
84  
85 sub render
86 {
87 my $self = shift;
88 my ($context) = @_;
89  
90 return 0 unless $self->should_render($context);
91  
92 return 1 unless $self->conditional_passes($context);
93  
94 return $self->iterate_over_children($context);
95 }
96  
97 sub max_of
98 {
99 my $self = shift;
100 my ($context, $attr) = @_;
101  
102 return 0 unless $self->conditional_passes($context);
103  
104 return $self->SUPER::max_of($context, $attr);
105 }
106  
107 sub total_of
108 {
109 my $self = shift;
110 my ($context, $attr) = @_;
111  
112 return 0 unless $self->conditional_passes($context);
113  
114 return $self->SUPER::total_of($context, $attr);
115 }
116  
117 sub _do_page
118 {
119 my $self = shift;
120 return unless $self->conditional_passes(@_);
121 return $self->SUPER::_do_page( @_ );
122 }
123  
124 sub begin_page
125 {
126 _do_page(@_,'begin_page');
127 }
128  
129 sub end_page
130 {
131 _do_page(@_,'end_page');
132 }
133  
134 1;
135 __END__