lib/PDF/FromHTML/Template/Iterator.pm untested

filter: Statementstmt 0.0% 0 / 94 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 25 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 12 totaltotal 0.0% 0 / 184 CC 20 SCAR 25.4
CC
20
Cov
0%
new
30.0
more_iters
20.0
can_continue
12.0
CRAP 12.7
1 package PDF::FromHTML::Template::Iterator;
2  
3 use strict;
4  
5 BEGIN {
6 use vars qw(@ISA);
7 @ISA = qw(PDF::FromHTML::Template::Base);
8  
9 use PDF::FromHTML::Template::Base;
10 }
11  
12 sub new
13 {
14 my $class = shift;
15 my $self = $class->SUPER::new(@_);
16  
17 unless (PDF::FromHTML::Template::Factory::isa($self->{CONTEXT}, 'CONTEXT'))
18 {
19 die "Internal Error: No context object passed to ", __PACKAGE__, $/;
20 }
21  
22 $self->{MAXITERS} ||= 0;
23  
24 # This is the index we will work on NEXT, in whatever direction the
25 # iterator is going.
26 $self->{INDEX} = -1;
27  
28 # This will always increment because it's tracking how many iterations
29 # have happened on this page, regardless of the direction the iterator
30 # is traveling.
31 $self->{ITERS_THIS_PAGE} = 0;
32  
33 # This is a short-circuit parameter to let the iterator function in a
34 # null state.
35 $self->{NO_PARAMS} = 0;
36 unless ($self->{NAME} =~ /\w/)
37 {
38 $self->{NO_PARAMS} = 1;
39  
40 warn "INTERNAL ERROR: 'NAME' was blank was blank when passed to ", __PACKAGE__, $/;
41  
42 return $self;
43 }
44  
45 # Cache the reference to the appropriate data.
46 $self->{DATA} = $self->{CONTEXT}->param($self->{NAME});
47  
48 unless (UNIVERSAL::isa($self->{DATA}, 'ARRAY'))
49 {
50 $self->{NO_PARAMS} = 1;
51 warn "'$self->{NAME}' does not have a list of parameters", $/;
52  
53 return $self;
54 }
55  
56 unless (@{$self->{DATA}})
57 {
58 $self->{NO_PARAMS} = 1;
59 }
60  
61 $self->{MAX_INDEX} = $#{$self->{DATA}};
62  
63 return $self;
64 }
65  
66 sub enter_scope
67 {
68 my $self = shift;
69  
70 return 0 if $self->{NO_PARAMS};
71  
72 for my $x ($self->{DATA}[$self->{INDEX}])
73 {
74 $x->{uc $_} = delete $x->{$_} for keys %$x;
75 }
76 push @{$self->{CONTEXT}{PARAM_MAP}}, $self->{DATA}[$self->{INDEX}];
77  
78 return 1;
79 }
80  
81 sub exit_scope
82 {
83 my $self = shift;
84  
85 return 0 if $self->{NO_PARAMS};
86  
87 # There has to be the base parameter map and at least the one that
88 # Iterator::enter_scope() added on top.
89 @{$self->{CONTEXT}{PARAM_MAP}} > 1 ||
90 die "Internal Error: ", __PACKAGE__, "'s internal param_map off!", $/;
91  
92 pop @{$self->{CONTEXT}{PARAM_MAP}};
93  
94 return 1;
95 }
96  
97 sub can_continue
98 {
99 my $self = shift;
100  
101 return 0 if $self->{NO_PARAMS};
102  
103 return 1 if $self->more_params &&
104 $self->more_space &&
105 $self->more_iters;
106  
107 return 0;
108 }
109  
110 sub more_iters
111 {
112 my $self = shift;
113  
114 return 0 if $self->{NO_PARAMS};
115  
116 return 1 unless $self->{MAXITERS};
117  
118 return 1 if $self->{MAXITERS} > $self->{ITERS_THIS_PAGE};
119  
120 return 0;
121 }
122  
123 sub more_params
124 {
125 my $self = shift;
126  
127 return 0 if $self->{NO_PARAMS};
128  
129 return 1 if $self->{MAX_INDEX} > $self->{INDEX};
130  
131 return 0;
132 }
133  
134 sub more_space
135 {
136 my $self = shift;
137  
138 return 0 if $self->{NO_PARAMS};
139  
140 return 1 if $self->{CONTEXT}->get($self, 'Y') >= ($self->{CONTEXT}->get($self, 'END_Y'));
141  
142 return 0;
143 }
144  
145 # Call this method BEFORE incrementing the index to the next value.
146 sub _do_globals
147 {
148 my $self = shift;
149  
150 my $data = $self->{DATA}[$self->{INDEX}];
151  
152 # Perl's arrays are 0-indexed. Thus, the first element is at index "0".
153 # This means that odd-numbered elements are at even indices, and vice-versa.
154 # This also means that MAX (the number of elements in the array) can never
155 # be the value of an index. It is NOT the last index in the array.
156  
157 $data->{'__FIRST__'} ||= ($self->{INDEX} == 0);
158 $data->{'__INNER__'} ||= (0 < $self->{INDEX} && $self->{INDEX} < $self->{MAX_INDEX});
159 $data->{'__LAST__'} ||= ($self->{INDEX} == $self->{MAX_INDEX});
160 $data->{'__ODD__'} ||= !($self->{INDEX} % 2);
161  
162 return 1;
163 }
164  
165 sub next
166 {
167 my $self = shift;
168  
169 return 0 if $self->{NO_PARAMS};
170  
171 return 0 unless $self->more_params;
172  
173 $self->exit_scope;
174  
175 $self->{INDEX}++;
176 $self->{ITERS_THIS_PAGE}++;
177  
178 $self->_do_globals;
179  
180 $self->enter_scope;
181  
182 return 1;
183 }
184  
185 sub back_up
186 {
187 my $self = shift;
188  
189 return 0 if $self->{NO_PARAMS};
190  
191 $self->exit_scope;
192  
193 $self->{INDEX}--;
194 $self->{ITERS_THIS_PAGE}++;
195  
196 $self->_do_globals;
197  
198 $self->enter_scope;
199  
200 return 1;
201 }
202  
203 sub reset
204 {
205 my $self = shift;
206  
207 return 0 if $self->{NO_PARAMS};
208  
209 $self->{INDEX} = -1;
210 $self->{ITERS_THIS_PAGE} = 0;
211  
212 return 1;
213 }
214  
215 1;
216 __END__