blib/lib/HTML/Template/Nest.pm

filter: Statementstmt 100% 83 / 83 Branchbran 77.2% 34 / 44 Conditioncond 55.0% 11 / 20 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 100% 16 / 16 Podpod 100% 7 / 7 totaltotal 82.9% 151 / 182 SCAR 35.7
CC
33
Cov
87%
_hash_to_html
7.0
new
6.0
show_labels
5.6
CRAP 35.4
↑ Summary
1 package HTML::Template::Nest;
2  
3 2 ▶use 5.020001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use HTML::Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11  
12 2 our $VERSION = '0.03';
13  
14 sub new{
15 2 ▶ my ($class,%opts) = @_;
Subroutine new: called CC=6 SCAR=18.0
Pod documented
16 2 my $self = {%opts};
17  
18 #defaults
19 2 ▶ $self->{comment_tokens} = [ '<!--','-->' ] unless $self->{comment_tokens};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"comment_tokens"} 2 0 2
20 2 ▶ $self->{name_label} = 'NAME' unless $self->{name_label};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"name_label"} 1 1 2
21 2 ▶ $self->{template_dir} = '' unless defined $self->{template_dir};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"template_dir"} 1 1 2
22 2 ▶ $self->{template_ext} = '.html' unless defined $self->{template_ext};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"template_ext"} 1 1 2
23 2 ▶ $self->{show_labels} = 0 unless defined $self->{show_labels};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"show_labels"} 2 0 2
24  
25 2 bless $self,$class;
26 2 return $self;
27 }
28  
29  
30 sub template_dir{
31 12 ▶ my($self,$dir) = @_;
Subroutine template_dir: called CC=4 SCAR=14.1
Pod documented
32 12 ▶ confess "Expected a scalar directory name but got a ".ref($dir) if $dir && ref($dir);
Branch: 1 branch
BranchTrueFalseTotal
if $dir and ref $dir 0 12 12
Condition66%2/3
$dir and ref $dir
!ll&&!rl&&r
1110
MC/DC0%0/2
$dir and ref $dir
$dir ref $dir
Truth table66%2/3
A and B
A= $dirB= ref $dir
ABresult
0X0
100
111
33 12 ▶ $self->{template_dir} = $dir if $dir;
Branch: 1 branch
BranchTrueFalseTotal
if $dir 1 11 12
34 12 return $self->{template_dir};
35 }
36
37  
38 sub comment_tokens{
39 3 ▶ my ($self,$token1,$token2) = @_;
Subroutine comment_tokens: called CC=3 SCAR=11.0
Pod documented
40 3 ▶ if (defined $token1 ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $token1) 2 1 3
41 2 ▶ $token2 = $token2 || '';
Condition50%1/2
$token2 || ''
l!l
20
MC/DC0%0/1
$token2 || ''
$token2
Truth table50%1/2
A
A= $token2
Aresult
11
00
42 2 $self->{'comment_tokens'} = [ $token1, $token2 ];
43 }
44 3 return $self->{'comment_tokens'};
45  
46 }
47  
48  
49  
50  
51 sub show_labels{
52 13 ▶ my ($self,$show) = @_;
Subroutine show_labels: called CC=5 SCAR=17.2
Pod documented
53 13 ▶ confess "Expected a boolean but got $show" if $show && ! ( $show == 0 || $show == 1 );
Branch: 1 branch
BranchTrueFalseTotal
if $show and not $show == 0 || $show == 1 0 13 13
Condition50%3/6
$show and not $show == 0 || $show == 1
!ll&&!rl&&r
1120
$show == 0 || $show == 1
l!l&&r!l&&!r
020
MC/DC0%0/3
$show and not $show == 0 || $show == 1
$show $show == 0 $show == 1
Truth table50%2/4
A and not(B || C)
A= $showB= $show == 0C= $show == 1
ABCresult
0XX0
11X0
1010
1001
54 13 ▶ $self->{show_labels} = $show if $show;
Branch: 1 branch
BranchTrueFalseTotal
if $show 2 11 13
55 13 return $self->{show_labels};
56 }
57  
58  
59 sub template_ext{
60 12 ▶ my ($self,$ext) = @_;
Subroutine template_ext: called CC=4 SCAR=14.1
Pod documented
61 12 ▶ confess "Expected a scalar extension name but got a ".ref($ext) if defined $ext && ref($ext);
Branch: 1 branch
BranchTrueFalseTotal
if defined $ext and ref $ext 0 12 12
Condition66%2/3
defined $ext and ref $ext
!ll&&!rl&&r
1110
MC/DC0%0/2
defined $ext and ref $ext
defined $ext ref $ext
Truth table66%2/3
A and B
A= defined $extB= ref $ext
ABresult
0X0
100
111
62 12 ▶ $self->{template_ext} = $ext if defined $ext;
Branch: 1 branch
BranchTrueFalseTotal
if defined $ext 1 11 12
63 12 return $self->{template_ext};
64 }
65
66  
67 sub name_label{
68 32 ▶ my ($self,$label) = @_;
Subroutine name_label: called CC=4 SCAR=14.1
Pod documented
69 32 ▶ confess "Expected a scalar name label but got a ".ref($label) if defined $label && ref($label);
Branch: 1 branch
BranchTrueFalseTotal
if defined $label and ref $label 0 32 32
Condition66%2/3
defined $label and ref $label
!ll&&!rl&&r
3110
MC/DC0%0/2
defined $label and ref $label
defined $label ref $label
Truth table66%2/3
A and B
A= defined $labelB= ref $label
ABresult
0X0
100
111
70 32 ▶ $self->{name_label} = $label if $label;
Branch: 1 branch
BranchTrueFalseTotal
if $label 1 31 32
71 32 return $self->{name_label};
72 }
73  
74  
75 sub to_html{
76 16 ▶ my ($self,$comp) = @_;
Subroutine to_html: called CC=3 SCAR=11.0
Pod documented
77  
78 16 my $html;
79 16 ▶ if ( ref($comp) =~ /array/i ){
Branch: 2 branches
BranchTrueFalseTotal
if (ref($comp) =~ /array/iu) { } 2 14 16
elsif (ref($comp) =~ /hash/iu) { } 10 4 14
80 2 $html = $self->_array_to_html( $comp );
81 } elsif( ref( $comp ) =~ /hash/i ){
82 10 $html = $self->_hash_to_html( $comp );
83 } else {
84 4 $html = $comp;
85 }
86  
87 16 return $html;
88 }
89  
90  
91  
92 sub _hash_to_html{
93 10 ▶ my ($self,$h) = @_;
Subroutine _hash_to_html: called CC=7 SCAR=19.5
94  
95 10 ▶ confess "Expected a hashref. Instead got a ".ref($h) unless ref($h) =~ /hash/i;
Branch: 1 branch
BranchTrueFalseTotal
unless ref($h) =~ /hash/iu 0 10 10
96  
97 10 my $template_name = $h->{ $self->name_label };
98  
99 10 ▶ confess 'Encountered hash with no name_label ("'.$self->name_label.'"): '.Dumper( $h ) unless $template_name;
Branch: 1 branch
BranchTrueFalseTotal
unless $template_name 0 10 10
100  
101 10 my $param = {};
102  
103 10 foreach my $k ( keys %$h ){
104 20 ▶ next if $k eq $self->name_label;
Branch: 1 branch
BranchTrueFalseTotal
if $k eq $self->name_label 10 10 20
105 10 $param->{$k} = $self->to_html( $h->{$k} );
106 }
107  
108 10 my $filename = File::Spec->catdir(
109 $self->template_dir,
110 $template_name.$self->template_ext
111 );
112  
113 10 my $temp = HTML::Template->new( filename => $filename );
114 10 $temp->param( $_ => $param->{$_} ) foreach keys %$param;
115  
116 10 my $html = $temp->output;
117 10 ▶ if ( $self->show_labels ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->show_labels) 5 5 10
118  
119 5 my $ca = $self->{comment_tokens}->[0];
120 5 my $cb = $self->{comment_tokens}->[1];
121  
122 5 $html = "$ca BEGIN $template_name $cb\n$html\n$ca END $template_name $cb\n";
123 }
124  
125 10 return $html;
126  
127 }
128  
129  
130  
131  
132 sub _array_to_html{
133  
134 2 ▶ my ($self, $arr, $delim) = @_;
Subroutine _array_to_html: called CC=5 SCAR=17.2
135 2 ▶ die "Expected an array. Instead got a ".ref($arr) unless ref($arr) =~ /array/i;
Branch: 1 branch
BranchTrueFalseTotal
unless ref($arr) =~ /array/iu 0 2 2
136 2 my $html = '';
137 2 foreach my $comp (@$arr){
138 4 ▶ $html.= $delim if ($delim && $html);
Branch: 1 branch
BranchTrueFalseTotal
if $delim and $html 0 4 4
Condition33%1/3
$delim and $html
!ll&&!rl&&r
400
MC/DC0%0/2
$delim and $html
$delim $html
Truth table33%1/3
A and B
A= $delimB= $html
ABresult
0X0
100
111
139 4 $html.= $self->to_html( $comp );
140 }
141 2 return $html;
142  
143 }
144  
145  
146  
147 2 1;
148 __END__
↑ Summary