blib/lib/Template/Toolkit/Simple.pm

filter: Statementstmt 88.9% 105 / 118 Branchbran 71.4% 30 / 42 Conditioncond 85.7% 6 / 7 MC/DCmcdc 75.0% 3 / 4 Subroutinesub 88.4% 23 / 26 Podpod 66.6% 6 / 9 totaltotal 83.9% 173 / 206 CC 21 SCAR 13.7
CC
21
Cov
84%
render
10.1
_run_command
6.1
_file_to_hash
5.2
CRAP 3.9
↑ Summary bin/tt-render »
1 7 ▶use strict; use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
2 package Template::Toolkit::Simple;
3 7 our $VERSION = '0.31';
4  
5 7 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use Template::Constants qw( :debug );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use YAML::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 7 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 7 our @EXPORT = qw(tt);
13  
14 3 ▶sub tt {
Subroutine tt: called CC=1 SCAR=0.0
Pod documented
15 3 return Template::Toolkit::Simple->new();
16 }
17  
18 7 my $default = {
19 data => undef,
20 config => undef,
21 output => undef,
22  
23 encoding => 'utf8',
24 include_path => undef,
25 eval_perl => 0,
26 start_tag => quotemeta('[' . '%'),
27 end_tag => quotemeta('%' . ']'),
28 tag_style => 'template',
29 pre_chomp => 0,
30 post_chomp => 0,
31 trim => 0,
32 interpolate => 0,
33 anycase => 0,
34 delimiter => ':',
35 absolute => 0,
36 relative => 0,
37 strict => 0,
38 default => undef,
39 blocks => undef,
40 auto_reset => 1,
41 recursion => 0,
42 pre_process => undef,
43 post_process => undef,
44 process_template => undef,
45 error_template => undef,
46 output_path => undef,
47 debug => 0,
48 cache_size => undef,
49 compile_ext => undef,
50 compile_dir => undef,
51 };
52  
53 7 my $abbreviations = {
54 data => 'd',
55 include_path => 'path|i',
56 output => 'o',
57 config => 'c',
58 };
59  
60 6 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
61 6 my $class = shift;
62 6 ▶ return bless shift || {%$default}, $class;
Condition100%2/2
shift() || {%$default}
l!l
33
MC/DC100%1/1
shift() || {%$default}
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
63 }
64  
65 203 ▶sub field {
Subroutine field: called CC=1 SCAR=0.0
Pod undocumented
66 203 my ($name, $value) = @_;
67 12 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
68 12 my $self = shift;
69 12 ▶ $self->{$name} = @_ ? shift : $value;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 9 3 12
70 12 return $self;
71 203 };
72 }
73  
74 {
75 7 for my $name (keys %$default) {
76 217 ▶ next if $name =~ /^(data|config)/;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /^(data|config)/ 14 203 217
77 203 my $value = $default->{$name};
78 203 ▶ if (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 119 84 203
79 119 ▶ $value = 1 - $value if $value =~/^[01]$/;
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /^[01]$/ 84 35 119
80 119 ▶ $value = [] if $name eq 'include_path';
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "include_path" 0 119 119
81 }
82 7 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
83 203 *{__PACKAGE__ . '::' . $name} = field($name, $value);
84 }
85 }
86  
87 {
88 7 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
89 7 *path = \&include_path;
90 }
91  
92 6 ▶sub render {
Subroutine render: called CC=8 SCAR=23.2
Pod documented
93 6 my $self = shift;
94 6 ▶ my $template = shift
Branch: 1 branch
BranchTrueFalseTotal
my $template = shift() or die "render method requires a template name" 6 0 6
95 or die "render method requires a template name";
96 6 ▶ if ($template =~ qr{//}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($template =~ qr"//") 1 5 6
97 1 my $path;
98 1 ($path, $template) = split '//', $template, 2;
99 1 $self->include_path($path);
100 }
101 6 ▶ $self->data(shift(@_)) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 6 6
102 6 ▶ $self->output(shift(@_)) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 6 6
103  
104 6 ▶ if ($self->{output}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"output"}) 0 6 6
105 $self->process($template, $self->{data}, $self->{output})
106 0 ▶ or $self->croak;
Branch: 1 branch
BranchTrueFalseTotal
$self->process($template, $self->{"data"}, $self->{"output"}) or $self->croak 0 0 0
107 0 return '';
108 }
109  
110 6 my $output = '';
111 6 ▶ $self->process($template, $self->{data}, \$output)
Branch: 1 branch
BranchTrueFalseTotal
$self->process($template, $self->{"data"}, \$output) or $self->croak 6 0 6
112 or $self->croak;
113 6 return Encode::encode_utf8($output);
114 }
115  
116 0 ▶sub usage {
Subroutine usage: not called CC=1 SCAR=6.9
Pod undocumented
117 return <<'...'
118 Usage:
119  
120 tt-render --path=path/to/templates/ --data=data.yaml foo.tt2
121  
122 ...
123 0 }
124  
125 0 ▶sub croak {
Subroutine croak: not called CC=1 SCAR=6.9
Pod undocumented
126 0 my $self = shift;
127 0 require Carp;
128 0 my $error = $self->{tt}->error;
129 0 chomp $error;
130 0 Carp::croak($error . "\n");
131 };
132  
133 6 ▶sub process {
Subroutine process: called CC=2 SCAR=7.1
Pod documented
134 6 my $self = shift;
135  
136 $self->{tt} = Template->new(
137 ENCODING => $self->{encoding},
138 INCLUDE_PATH => $self->{include_path},
139 EVAL_PERL => $self->{eval_perl},
140 START_TAG => $self->{start_tag},
141 END_TAG => $self->{end_tag},
142 PRE_CHOMP => $self->{pre_chomp},
143 POST_CHOMP => $self->{post_chomp},
144 TRIM => $self->{trim},
145 INTERPOLATE => $self->{interpolate},
146 ANYCASE => $self->{anycase},
147 DELIMITER => $self->{delimiter},
148 ABSOLUTE => $self->{absolute},
149 STRICT => $self->{strict},
150 DEFAULT => $self->{default},
151 BLOCKS => $self->{blocks},
152 AUTO_RESET => $self->{auto_reset},
153 RECURSION => $self->{recursion},
154 PRE_PROCESS => $self->{pre_process},
155 POST_PROCESS => $self->{post_process},
156 PROCESS_TEMPLATE => $self->{process_template},
157 ERROR_TEMPLATE => $self->{error_template},
158 OUTPUT_PATH => $self->{output_path},
159 DEBUG =>
160 ($self->{debug} && DEBUG_ALL ^ DEBUG_CALLER ^ DEBUG_CONTEXT),
161 CACHE_SIZE => $self->{cache_size},
162 COMPILE_EXT => $self->{compile_ext},
163 COMPILE_DIR => $self->{compile_dir},
164 6 ▶ );
Condition50%1/2
$self->{'debug'} && 6111
!ll
60
MC/DC0%0/1
$self->{'debug'} && 6111
$self->{'debug'}
Truth table50%1/2
A
A= $self->{'debug'}
Aresult
00
11
165  
166 6 return $self->{tt}->process(@_);
167 }
168  
169 6 ▶sub data {
Subroutine data: called CC=1 SCAR=0.0
Pod documented
170 6 my $self = shift;
171 6 $self->{data} = $self->_file_to_hash(@_);
172 6 return $self;
173 }
174  
175 0 ▶sub config {
Subroutine config: not called CC=1 SCAR=6.9
Pod documented
176 0 my $self = shift;
177 0 $self = {
178 %$self,
179 $self->_file_to_hash(@_)
180 };
181 0 return $self;
182 }
183  
184 6 ▶sub _file_to_hash {
Subroutine _file_to_hash: called CC=5 SCAR=16.4
185 6 my $self = shift;
186 6 my $file_name = shift;
187 return
188 6 ▶ (ref($file_name) eq 'HASH')
Branch: 4 branches
BranchTrueFalseTotal
ref $file_name eq 'HASH' ? : 0 6 6
$file_name =~ /\.(?:yaml|yml)$/i ? : 4 2 6
$file_name =~ /\.json$/i ? : 1 1 2
$file_name =~ /\.xml$/i ? : 1 0 1
189 ? $file_name
190 : ($file_name =~ /\.(?:yaml|yml)$/i)
191 ? $self->_load_yaml($file_name)
192 : ($file_name =~ /\.json$/i)
193 ? $self->_load_json($file_name)
194 : ($file_name =~ /\.xml$/i)
195 ? $self->_load_xml($file_name)
196 : die "Expected '$file_name' to end with .yaml, .json or .xml";
197 }
198  
199 4 ▶sub _load_yaml {
Subroutine _load_yaml: called CC=1 SCAR=0.0
200 4 my $self = shift;
201 4 YAML::XS::LoadFile(shift);
202 }
203  
204 1 ▶sub _load_json {
Subroutine _load_json: called CC=1 SCAR=0.0
205 1 my $self = shift;
206 1 require JSON::XS;
207 1 my $json = do { local $/; open my $json, '<', shift; <$json> };
208 1 JSON::XS::decode_json($json);
209 }
210  
211 1 ▶sub _load_xml {
Subroutine _load_xml: called CC=1 SCAR=0.0
212 1 my $self = shift;
213 1 require XML::Simple;
214 1 XML::Simple::XMLin(shift);
215 }
216  
217 3 ▶sub _run_command {
Subroutine _run_command: called CC=6 SCAR=18.0
218 3 my $class = shift;
219 3 my $self = $class->new($default);
220 3 local @ARGV = @_;
221 3 ▶ my $template = pop or do {
Branch: 1 branch
BranchTrueFalseTotal
my $template = pop() or do { print STDERR $self->usage; return } 3 0 3
222 0 print STDERR $self->usage();
223 0 return;
224 };
225 8 ▶ my $setter = sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
226 8 my ($name, $value) = @_;
227 8 my $method = lc($name);
228 8 $method =~ s/-/_/g;
229 8 ▶ $value = quotemeta($value)
Branch: 1 branch
BranchTrueFalseTotal
if $method =~ /_tag$/ 0 8 8
230 if $method =~ /_tag$/;
231 8 $self->$method($value);
232 3 };
233 GetOptions(
234 map {
235 3 my $option = $_;
236 93 my $option2 = $option;
237 93 ▶ $option .= "|$option2" if $option2 =~ s/_/-/g;
Branch: 1 branch
BranchTrueFalseTotal
if $option2 =~ s/_/-/g 48 45 93
238 $option .= "|$abbreviations->{$_}"
239 93 ▶ if defined $abbreviations->{$_};
Branch: 1 branch
BranchTrueFalseTotal
if defined $abbreviations->{$_} 12 81 93
240 93 ▶ $option .= ((not defined $default->{$_} or $option =~/\-tag$/) ? '=s' : '');
Branch: 1 branch
BranchTrueFalseTotal
!defined($default->{$_}) || $option =~ /\-tag$/ ? : 48 45 93
Condition100%3/3
not defined $default->{$_} or $option =~ /\-tag$/
l!l&&r!l&&!r
42645
MC/DC100%2/2
not defined $default->{$_} or $option =~ /\-tag$/
not defined $default->{$_} $option =~ /\-tag$/
Truth table100%3/3
not(A) or B
A= not defined $default->{$_}B= $option =~ /\-tag$/
ABresult
1X1
011
000
241 93 ($option, $setter);
242 } keys %$default
243 );
244  
245 3 print STDOUT $self->render($template);
246 }
247  
248 7 1;
↑ Summary bin/tt-render »