EntityModel/Template.pm

filter: Statementstmt 18.9% 34 / 179 Branchbran 0.0% 0 / 62 Conditioncond 0.0% 0 / 19 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 20.5% 8 / 39 Podpod 85.7% 12 / 14 totaltotal 16.6% 54 / 325 CC 41 SCAR 37.9
CC
41
Cov
13%
from_now
156.0
short_date
110.0
new
30.0
CRAP 44.4
« EntityModel/Entity.pm ↑ Summary EntityModel/Support.pm »
1 package EntityModel::Template;
2 {
3 3 $EntityModel::Template::VERSION = '0.102';
4 }
5 3 ▶use EntityModel::Class {
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 include_path => { type => 'array', subclass => 'string' }
7 3 };
8  
9 =head1 NAME
10  
11 EntityModel::Template - template handling for L<EntityModel>
12  
13 =head1 VERSION
14  
15 version 0.102
16  
17 =head1 SYNOPSIS
18  
19 =head1 DESCRIPTION
20  
21 =cut
22  
23 3 ▶use Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 3 ▶use Template::Stash;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 3 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 3 ▶use Tie::Cache::LRU;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 3 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 3 ▶use POSIX qw/floor/;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 3 tie my %LONG_DATE_HASH, 'Tie::Cache::LRU', 5000;
31 3 tie my %SHORT_DATE_HASH, 'Tie::Cache::LRU', 5000;
32  
33 3 our $BasePath = '.';
34  
35 3 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=3.0
36 # Convenience functions so we can do something.arrayref and be sure to get back something FOREACH-suitable
37 0 ▶ $Template::Stash::LIST_OPS->{ arrayref } = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
38 0 my $list = shift;
39 0 return $list;
40 3 };
41 0 ▶ $Template::Stash::HASH_OPS->{ arrayref } = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
42 0 my $hash = shift;
43 0 return [ $hash ];
44 3 };
45 # hashops since we have datetime object... in theory.
46 #$Template::Stash::HASH_OPS->{ msDuration } = sub {
47 # my $v = shift;
48 # return DateTime::Format::Duration->new(pattern => '%H:%M:%S.%3N')->format_duration($v);
49 #};
50 0 ▶ $Template::Stash::HASH_OPS->{ from_now } = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
51 0 my $v = shift;
52 0 return from_now($v);
53 3 };
54 0 ▶ $Template::Stash::HASH_OPS->{ 'ref' } = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
55 0 my $scalar = shift;
56 0 return ref $scalar;
57 3 };
58 0 ▶ $Template::Stash::SCALAR_OPS->{ arrayref } = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
59 0 my $scalar = shift;
60 0 return [ $scalar ];
61 3 };
62 0 ▶ $Template::Stash::SCALAR_OPS->{ trim } = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
63 0 my $scalar = shift;
64 0 $scalar =~ s/^\s+//ms;
65 0 $scalar =~ s/\s+$//ms;
66 0 return $scalar;
67 3 };
68 0 ▶ $Template::Stash::SCALAR_OPS->{ js } = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
69 0 my $str = join('', @_);
70 0 $str =~ s/"/\\"/ms;
71 0 return '"' . $str . '"';
72 3 };
73 }
74  
75 0 ▶sub new {
Subroutine new: not called CC=5 SCAR=34.0
Pod documented
76 0 my $class = shift;
77 0 my $self = bless { data => { } }, $class;
78 0 my %args = @_;
79 0 ▶ if(defined(my $include = delete $args{include_path})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $include = delete $args{"include_path"})) 0 0 0
80 0 ▶ $include = [ $include ] unless ref $include;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $include 0 0 0
81 0 $self->include_path->push($_) for @$include;
82 }
83  
84 # We want access to _ methods, such as _view, so disable this.
85 0 undef $Template::Stash::PRIVATE;
86  
87 my %cfg = (
88 INCLUDE_PATH => [ $self->include_path->list ],
89 INTERPOLATE => 0,
90 ABSOLUTE => 0,
91 RELATIVE => 0,
92 RECURSION => 1,
93 AUTO_RESET => 0,
94 STAT_TTL => 15,
95 COMPILE_EXT => '.ttc',
96 COMPILE_DIR => '/tmp/ttc',
97 CACHE_SIZE => 4096,
98 PRE_DEFINE => {
99 # cfg => \%EntityModel::Config::Current,
100 # imageHost => 'http://' . EntityModel::Config::ImageHost,
101 # scriptHost => 'http://' . EntityModel::Config::ScriptHost,
102 },
103 FILTERS => {
104 long_date => [
105 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
106 0 my ($context, @args) = @_;
107 0 ▶ return sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
108 0 return long_date(shift, @args);
109 }
110 0 }, 1
111 ],
112 short_date => [
113 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
114 0 my ($context, @args) = @_;
115 0 ▶ return sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
116 0 return short_date(shift, @args);
117 }
118 0 }, 1
119 ],
120 ymd_date => [
121 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
122 0 my ($context, @args) = @_;
123 0 ▶ return sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
124 0 return ymd_date(shift, @args);
125 }
126 0 }, 1
127 ],
128 tidy_ymd => [
129 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
130 0 my ($context, @args) = @_;
131 0 ▶ return sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
132 0 return tidy_ymd(shift, @args);
133 }
134 0 }, 1
135 ],
136 from_now => [
137 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
138 0 my ($context, @args) = @_;
139 0 ▶ return sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
140 0 return from_now(shift, @args);
141 }
142 0 }, 1
143 ],
144 #as_duration => [
145 # sub {
146 # my ($context, @args) = @_;
147 # return sub {
148 # return as_duration(shift, @args);
149 # }
150 # }, 1
151 #],
152 },
153 );
154 #$cfg{CONTEXT} = new Template::Timer(%cfg) if EntityModel::Config::Debug;
155 0 ▶ my $tmpl = Template->new(%cfg) or die Template->error;
Branch: 1 branch
BranchTrueFalseTotal
my $tmpl = "Template"->new(%cfg) or die "Template"->error 0 0 0
156 0 $self->{ template } = $tmpl;
157 0 return $self;
158 }
159  
160 =head2 from_now
161  
162 Duration from/since now
163  
164 =cut
165  
166 0 ▶sub from_now {
Subroutine from_now: not called CC=12 SCAR=50.5
Pod documented
167 0 my $v = shift;
168 0 ▶ return ' ' unless $v;
Branch: 1 branch
BranchTrueFalseTotal
unless $v 0 0 0
169  
170 0 ▶ $v = DateTime->from_epoch(epoch => $1) if !ref($v) && $v =~ /^(\d+(?:\.\d*))$/;
Branch: 1 branch
BranchTrueFalseTotal
if not ref $v and $v =~ /^(\d+(?:\.\d*))$/ 0 0 0
Condition0%0/3
not ref $v and $v =~ /^(\d+(?:\.\d*))$/
!ll&&!rl&&r
000
MC/DC0%0/2
not ref $v and $v =~ /^(\d+(?:\.\d*))$/
not ref $v $v =~ /^(\d+(?:\.\d*))$/
Truth table0%0/3
not(A) and B
A= not ref $vB= $v =~ /^(\d+(?:\.\d*))$/
ABresult
0X0
100
111
171 0 my $delta = $v->epoch - time;
172 0 my $neg;
173 0 ▶ if($delta < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($delta < 0) 0 0 0
174 0 $neg = 1;
175 0 $delta = -$delta;
176 }
177 0 my @p;
178 0 my @match = (
179 second => 60,
180 minute => 60,
181 hour => 24,
182 day => 30,
183 month => 12,
184 year => 0
185 );
186 0 ▶ while($delta && @match) {
Branch: 1 branch
BranchTrueFalseTotal
if ($delta and @match) 0 0 0
Condition0%0/3
$delta and @match
!ll&&!rl&&r
000
MC/DC0%0/2
$delta and @match
$delta @match
Truth table0%0/3
A and B
A= $deltaB= @match
ABresult
0X0
100
111
187 0 my $k = shift @match;
188 0 my $m = shift @match;
189 0 ▶ my $unit = $m ? ($delta % $m) : $delta;
Branch: 1 branch
BranchTrueFalseTotal
$m ? : 0 0 0
190 0 ▶ $delta = floor($delta / $m) if $m;
Branch: 1 branch
BranchTrueFalseTotal
if $m 0 0 0
191 0 ▶ unshift @p, "$unit $k" . ($unit != 1 ? 's' : '');
Branch: 1 branch
BranchTrueFalseTotal
$unit != 1 ? : 0 0 0
192 }
193  
194 # Don't show too much resolution
195 0 ▶ @p = @p[0..1] if @p > 2;
Branch: 1 branch
BranchTrueFalseTotal
if @p > 2 0 0 0
196 0 my $pattern = join(', ', @p);
197  
198 0 ▶ return $pattern . ($neg ? ' ago' : ' from now');
Branch: 1 branch
BranchTrueFalseTotal
$neg ? : 0 0 0
199 }
200  
201 =head2 long_date
202  
203 Long date format filter.
204  
205 =cut
206  
207 0 ▶sub long_date {
Subroutine long_date: not called CC=5 SCAR=34.0
Pod documented
208 0 my ($v, $fmt) = @_;
209 0 ▶ return ' ' unless $v;
Branch: 1 branch
BranchTrueFalseTotal
unless $v 0 0 0
210 0 ▶ unless ($LONG_DATE_HASH{$v}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($LONG_DATE_HASH{$v}) 0 0 0
211 0 my $dt;
212 0 ▶ if($v =~ m/^(\d{4})-(\d{2})-(\d{2})\s+(\d{2}):(\d{2}):(\d{2})(?:\.(\d+))?$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v =~ /^(\d{4})-(\d{2})-(\d{2})\s+(\d{2}):(\d{2}):(\d{2})(?:\.(\d+))?$/) { } 0 0 0
213 0 my ($year, $month, $day, $hour, $minute, $second, $us) = ($1, $2, $3, $4, $5, $6, $7);
214 0 ▶ $dt = DateTime->new(
Condition0%0/2
$us // 0
l!l
00
MC/DC0%0/1
$us // 0
$us
Truth table0%0/2
A
A= $us
Aresult
11
00
215 year => $year,
216 month => $month,
217 day => $day,
218 hour => $hour,
219 minute => $minute,
220 second => $second,
221 nanosecond => 1000 * ($us // 0)
222 );
223 } else {
224 0 $dt = DateTime->from_epoch(epoch => $v);
225 }
226 0 $LONG_DATE_HASH{$v} = $dt->strftime('%e %b %Y, %l:%M %P');
227 }
228 0 return $LONG_DATE_HASH{$v};
229 }
230  
231 =head2 short_date
232  
233 Short date format filter.
234  
235 =cut
236  
237 0 ▶sub short_date {
Subroutine short_date: not called CC=10 SCAR=47.0
Pod documented
238 0 my ($v, $fmt) = @_;
239 0 ▶ return ' ' unless $v;
Branch: 1 branch
BranchTrueFalseTotal
unless $v 0 0 0
240 0 ▶ unless ($SHORT_DATE_HASH{$v}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($SHORT_DATE_HASH{$v}) 0 0 0
241 0 my $dt;
242 0 ▶ if($v =~ m/^(\d+)-(\d+)-(\d+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v =~ /^(\d+)-(\d+)-(\d+)/) { } 0 0 0
243 0 my ($year, $month, $day) = ($1, $2, $3);
244 0 $dt = DateTime->new(
245 year => $year,
246 month => $month,
247 day => $day,
248 );
249 } else {
250 0 $dt = DateTime->from_epoch(epoch => $v);
251 }
252 0 my $suffix = 'th';
253 0 ▶ if(($dt->day % 10) == 1 && ($dt->day != 11)) {
Branch: 3 branches
BranchTrueFalseTotal
if ($dt->day % 10 == 1 and $dt->day != 11) { } 0 0 0
elsif ($dt->day % 10 == 2 and $dt->day != 12) { } 0 0 0
elsif ($dt->day % 10 == 3 and $dt->day != 13) { } 0 0 0
Condition0%0/3
$dt->day % 10 == 1 and $dt->day != 11
!ll&&!rl&&r
000
MC/DC0%0/2
$dt->day % 10 == 1 and $dt->day != 11
$dt->day % 10 == 1 $dt->day != 11
Truth table0%0/3
A and B
A= $dt->day % 10 == 1B= $dt->day != 11
ABresult
0X0
100
111
254 0 $suffix = 'st';
255 ▶ } elsif(($dt->day % 10) == 2 && ($dt->day != 12)) {
Condition0%0/3
$dt->day % 10 == 2 and $dt->day != 12
!ll&&!rl&&r
000
MC/DC0%0/2
$dt->day % 10 == 2 and $dt->day != 12
$dt->day % 10 == 2 $dt->day != 12
Truth table0%0/3
A and B
A= $dt->day % 10 == 2B= $dt->day != 12
ABresult
0X0
100
111
256 0 $suffix = 'nd';
257 ▶ } elsif(($dt->day % 10) == 3 && ($dt->day != 13)) {
Condition0%0/3
$dt->day % 10 == 3 and $dt->day != 13
!ll&&!rl&&r
000
MC/DC0%0/2
$dt->day % 10 == 3 and $dt->day != 13
$dt->day % 10 == 3 $dt->day != 13
Truth table0%0/3
A and B
A= $dt->day % 10 == 3B= $dt->day != 13
ABresult
0X0
100
111
258 0 $suffix = 'rd';
259 }
260 0 $SHORT_DATE_HASH{$v} = $dt->strftime("%d$suffix %b");
261 }
262 0 return $SHORT_DATE_HASH{$v};
263 }
264  
265 =head2 ymd_date
266  
267 YMD date filter
268  
269 =cut
270  
271 0 ▶sub ymd_date {
Subroutine ymd_date: not called CC=3 SCAR=24.8
Pod documented
272 0 my ($v, $fmt) = @_;
273 0 ▶ return ' ' unless $v;
Branch: 1 branch
BranchTrueFalseTotal
unless $v 0 0 0
274 0 my $dt;
275 0 ▶ if($v =~ m/^(\d+)-(\d+)-(\d+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v =~ /^(\d+)-(\d+)-(\d+)/) { } 0 0 0
276 0 my ($year, $month, $day) = ($1, $2, $3);
277 0 return sprintf("%04d-%02d-%02d", $year, $month, $day);
278 } else {
279 0 $dt = DateTime->from_epoch(epoch => $v);
280 }
281 0 return $dt->strftime('%Y-%m-%d');
282 }
283  
284 =head2 tidy_ymd
285  
286 YMD date filter
287  
288 =cut
289  
290 0 ▶sub tidy_ymd {
Subroutine tidy_ymd: not called CC=3 SCAR=24.8
Pod documented
291 0 my ($v, $fmt) = @_;
292 0 ▶ return ' ' unless $v;
Branch: 1 branch
BranchTrueFalseTotal
unless $v 0 0 0
293 0 my $dt;
294 0 ▶ if($v =~ m/^(\d+)-(\d+)-(\d+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v =~ /^(\d+)-(\d+)-(\d+)/) { } 0 0 0
295 0 my ($year, $month, $day) = ($1, $2, $3);
296 0 return sprintf("%04d-%02d-%02d", $year, $month, $day);
297 } else {
298 0 $dt = DateTime->from_epoch(epoch => $v);
299 0 return $dt->strftime('%Y-%m-%d');
300 }
301 }
302  
303 =head2 as_duration
304  
305 Convert duration to MM:SS representation.
306  
307 =cut
308  
309 0 ▶sub as_duration {
Subroutine as_duration: not called CC=2 SCAR=17.9
Pod documented
310 0 my ($v, $fmt) = @_;
311 0 ▶ return ' ' unless $v;
Branch: 1 branch
BranchTrueFalseTotal
unless $v 0 0 0
312  
313 0 return sprintf('%02d:%02d', int($v / 60), int($v % 60));
314 }
315  
316 =head2 template
317  
318 Return the TT2 object, created as necessary.
319  
320 =cut
321  
322 0 ▶sub template { shift->{template} }
Subroutine template: not called CC=1 SCAR=6.9
Pod documented
323  
324 =head2 as_text
325  
326 Return template output as text.
327  
328 =cut
329  
330 0 ▶sub as_text {
Subroutine as_text: not called CC=3 SCAR=24.8
Pod documented
331 0 my ($self, $template, $newData) = @_;
332 0 ▶ $newData ||= {};
Condition0%0/2
$newData ||= {}
l!l
00
MC/DC0%0/1
$newData ||= {}
$newData
Truth table0%0/2
A
A= $newData
Aresult
11
00
333 0 my %data = ( %{ $self->{data} }, %$newData );
334 0 my $output;
335 0 my $tt = $self->template;
336 0 ▶ $tt->process($template, \%data, \$output) || die 'Failed template: ' . $tt->error;
Branch: 1 branch
BranchTrueFalseTotal
$tt->process($template, \%data, \$output) or die 'Failed template: ' . $tt->error 0 0 0
337 0 return $output;
338 }
339  
340 0 ▶sub process_template {
Subroutine process_template: not called CC=2 SCAR=17.9
Pod undocumented
341 0 my $self = shift;
342 0 my $tmpl = shift;
343 0 my $tt = $self->template;
344 0 ▶ $tt->process($tmpl, undef, \my $output) or die "Failed template: " . $tt->error;
Branch: 1 branch
BranchTrueFalseTotal
$tt->process($tmpl, undef, \my $output) or die 'Failed template: ' . $tt->error 0 0 0
345 0 return $self;
346 }
347  
348 =head2 processHTML
349  
350 Process HTML data.
351  
352 =cut
353  
354 0 ▶sub processHTML {
Subroutine processHTML: not called CC=2 SCAR=17.9
Pod documented
355 0 my ($self, $template, $newData) = @_;
356 0 my $data = { %{$self->{data}}, %$newData };
357  
358 0 my $tt = $self->template;
359 0 my $output;
360 0 ▶ $tt->process($template, $data, \$output) || die 'Failed template: ' . $tt->error;
Branch: 1 branch
BranchTrueFalseTotal
$tt->process($template, $data, \$output) or die 'Failed template: ' . $tt->error 0 0 0
361 0 if(0) {
362 my $origSize = length($output);
363 $output =~ s/<!--(.*?)-->//g;
364 my $tidy = HTML::Tidy->new({
365 tidy_mark => 0,
366 'preserve-entities' => 1,
367 wrap => 160,
368 'char-encoding' => 'utf8',
369 indent => 0
370 });
371 $output = $tidy->clean($output);
372 my $finalSize = length($output);
373 logDebug("From %d to %d: %3.2f%%", $origSize, $finalSize, (100.0 * $finalSize/$origSize));
374 }
375 0 return $output;
376 }
377  
378 =head2 output
379  
380 Generate output via Apache2 print.
381  
382 =cut
383  
384 0 ▶sub output {
Subroutine output: not called CC=3 SCAR=24.8
Pod documented
385 0 my ($self, $template, $newData, $r) = @_;
386 0 my $data = { %{$self->{data}}, %$newData };
387  
388 0 logInfo("Output");
389 0 my $output = $self->processHTML($template, $data);
390 0 ▶ if($r) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r) { } 0 0 0
391 0 ▶ $r->content_type('text/html') if $r;
Branch: 1 branch
BranchTrueFalseTotal
if $r 0 0 0
392 0 $r->no_cache(1);
393 0 $r->setLifetime(0);
394 0 $r->print($output);
395 } else {
396 0 print $output;
397 }
398 }
399  
400 =head2 error
401  
402 Handle any TT2 error messages.
403  
404 =cut
405  
406 0 ▶sub error {
Subroutine error: not called CC=1 SCAR=6.9
Pod documented
407 0 my $self = shift;
408 0 return $self->template->error;
409 }
410  
411 0 ▶sub addData {
Subroutine addData: not called CC=2 SCAR=17.9
Pod undocumented
412 0 my ($self, $data) = @_;
413 0 foreach (keys %$data) {
414 0 $self->{data}->{ $_ } = $data->{$_};
415 }
416 0 return $self;
417 }
418  
419 3 1;
420  
421 __END__
« EntityModel/Entity.pm ↑ Summary EntityModel/Support.pm »