Muster/Hook/Template.pm

filter: Statementstmt 21.3% 29 / 136 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 47.3% 9 / 19 Podpod 100% 6 / 6 totaltotal 20.4% 44 / 215 CC 28 SCAR 36.6
CC
28
Cov
16%
process
90.0
format_yaml
56.0
_format_hash
56.0
CRAP 39.0
« Muster/Hook/DynamicField.pm ↑ Summary Muster/Hook/Shortcut.pm »
1 package Muster::Hook::Template;
2 1 $Muster::Hook::Template::VERSION = '0.96';
3 =head1 NAME
4  
5 Muster::Hook::Template - Muster template directive.
6  
7 =head1 VERSION
8  
9 version 0.96
10  
11 =head1 DESCRIPTION
12  
13 L<Muster::Hook::Template> for template directives inside pages.
14  
15 =cut
16  
17 1 ▶use Mojo::Base 'Muster::Hook::Directives';
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use Muster::LeafFile;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use Muster::Hooks;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use Muster::Hook::Links;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use File::Basename qw(basename);
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use YAML::Any;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use Text::NeatTemplate;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use Math::Calc::Parser;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 1 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 =head1 METHODS
29  
30 L<Muster::Hook::Template> inherits all methods from L<Muster::Hook::Directives>.
31  
32 =head2 register
33  
34 Do some intialization.
35  
36 =cut
37 0 ▶sub register {
Subroutine register: not called CC=1 SCAR=6.9
Pod documented
38 0 my $self = shift;
39 0 my $hookmaster = shift;
40 0 my $conf = shift;
41  
42 0 $self->{neat} = Text::NeatTemplate->new();
43  
44 0 ▶ $hookmaster->add_hook('template' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
45 0 my %args = @_;
46  
47 return $self->do_directives(
48 no_scan=>1,
49 directive=>'template',
50 0 ▶ call=>sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
51 0 my %args2 = @_;
52  
53 0 return $self->process(directive=>'template',%args2);
54 },
55 0 %args,
56 );
57 },
58 0 );
59 0 return $self;
60 } # register
61  
62 =head2 process
63  
64 Process templates.
65  
66 =cut
67 0 ▶sub process {
Subroutine process: not called CC=9 SCAR=45.0
Pod documented
68 0 my $self = shift;
69 0 my %args = @_;
70  
71 0 my $directive = $args{directive};
72 0 my $leaf = $args{leaf};
73 0 my $phase = $args{phase};
74 0 my @p = @{$args{params}};
75 0 my %params = @p;
76  
77 0 foreach my $wanted (qw(template))
78 {
79 0 ▶ if (! exists $params{$wanted})
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $params{$wanted}) 0 0 0
80 {
81 0 return "ERROR: missing $wanted parameter";
82 }
83 }
84 0 ▶ if ($phase eq $Muster::Hooks::PHASE_SCAN)
Branch: 1 branch
BranchTrueFalseTotal
if ($phase eq $Muster::Hooks::PHASE_SCAN) 0 0 0
85 {
86 0 return "";
87 }
88  
89 # Check simple IF condition
90 0 my $go_ahead = 1;
91 0 ▶ if (exists $params{if} and $params{if})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params{"if"} and $params{"if"}) 0 0 0
Condition0%0/3
exists $params{"if"} and $params{"if"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $params{"if"} and $params{"if"}
exists $params{"if"} $params{"if"}
Truth table0%0/3
A and B
A= exists $params{"if"}B= $params{"if"}
ABresult
0X0
100
111
92 {
93 $go_ahead = (exists $leaf->{meta}->{$params{if}}
94 and defined $leaf->{meta}->{$params{if}}
95 and $leaf->{meta}->{$params{if}}
96 0 ▶ );
Condition0%0/6
exists $leaf->{'meta'}{$params{'if'}} && defined $leaf->{'meta'}{$params{'if'}} && $leaf->{'meta'}{$params{'if'}}
!ll&&!rl&&r
000
exists $leaf->{"meta"}{$params{"if"}} and defined $leaf->{"meta"}{$params{"if"}}
!ll&&!rl&&r
000
MC/DC0%0/3
exists $leaf->{'meta'}{$params{'if'}} && defined $leaf->{'meta'}{$params{'if'}} && $leaf->{'meta'}{$params{'if'}}
exists $leaf->{"meta"}{$params{"if"}} defined $leaf->{"meta"}{$params{"if"}} $leaf->{'meta'}{$params{'if'}}
Truth table0%0/4
A and B && C
A= exists $leaf->{"meta"}{$params{"if"}}B= defined $leaf->{"meta"}{$params{"if"}}C= $leaf->{'meta'}{$params{'if'}}
ABCresult
0XX0
10X0
1100
1111
97 }
98  
99 0 my $result = '';
100 0 ▶ if ($go_ahead)
Branch: 1 branch
BranchTrueFalseTotal
if ($go_ahead) 0 0 0
101 {
102 # fill in the template with the leaf's data
103 $result = $self->{neat}->fill_in(
104 data_hash=>$leaf->{meta},
105 template=>$params{template},
106 0 );
107 }
108  
109 0 return $result;
110 } # process
111  
112 # -------------------------------------------------------------
113 # Callable Functions for Text::NeatTemplate
114 # -------------------------------------------------------------
115  
116 =head2 format_yaml
117  
118 {&format_yaml(fieldname,yaml_value)}
119  
120 Format a yaml field.
121  
122 =cut
123 0 ▶sub format_yaml {
Subroutine format_yaml: not called CC=7 SCAR=40.3
Pod documented
124 0 my $fieldname = shift;
125 0 my $value = shift;
126  
127 # if they didn't give us anything, return
128 0 ▶ if (!$value)
Branch: 1 branch
BranchTrueFalseTotal
unless ($value) 0 0 0
129 {
130 0 return '';
131 }
132  
133 0 my $loaded = Load($value);
134 0 ▶ if (!$loaded)
Branch: 1 branch
BranchTrueFalseTotal
unless ($loaded) 0 0 0
135 {
136 0 return $value;
137 }
138 0 ▶ if (!ref $loaded)
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $loaded) 0 0 0
139 {
140 0 return $loaded;
141 }
142  
143 0 my $out = '';
144 0 ▶ if ($fieldname)
Branch: 1 branch
BranchTrueFalseTotal
if ($fieldname) 0 0 0
145 {
146 0 $out .= "<b>" . uc($fieldname) . ":</b>\n";
147 }
148 0 ▶ if (ref $loaded eq 'HASH')
Branch: 2 branches
BranchTrueFalseTotal
if (ref $loaded eq 'HASH') { } 0 0 0
elsif (ref $loaded eq 'ARRAY') { } 0 0 0
149 {
150 0 $out .= _format_hash($loaded,0);
151 }
152 elsif (ref $loaded eq 'ARRAY')
153 {
154 0 $out .= _format_array($loaded,0);
155 }
156 0 $out .= "<br/>\n";
157 0 return $out;
158 } # format_yaml
159  
160 0 ▶sub _format_hash {
Subroutine _format_hash: not called CC=7 SCAR=40.3
161 0 my $hash = shift;
162 0 my $level = shift;
163  
164 0 my $out = '';
165 0 foreach my $key (sort keys %{$hash})
166 {
167 0 ▶ if ($level == 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($level == 0) { } 0 0 0
168 {
169 0 $out .= "<div class=yhash><b>$key:</b> ";
170 }
171 else
172 {
173 0 $out .= sprintf('<span class=lev%d>',$level) . '&nbsp;&nbsp;' x $level . $key . ': ';
174 }
175  
176 0 my $v = $hash->{$key};
177 0 ▶ if (!ref $v)
Branch: 3 branches
BranchTrueFalseTotal
if (not ref $v) { } 0 0 0
elsif (ref $v eq 'HASH') { } 0 0 0
elsif (ref $v eq 'ARRAY') { } 0 0 0
178 {
179 0 $out .= $v;
180 }
181 elsif (ref $v eq 'HASH')
182 {
183 0 $out .= _format_hash($v,$level + 1);
184 }
185 elsif (ref $v eq 'ARRAY')
186 {
187 0 $out .= _format_array($v,$level + 1);
188 }
189 0 ▶ if ($level == 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($level == 0) { } 0 0 0
190 {
191 0 $out .= "</div>\n";
192 }
193 else
194 {
195 0 $out .= "</span>\n";
196 }
197 }
198 0 return $out;
199 } # _format_hash
200  
201 0 ▶sub _format_array {
Subroutine _format_array: not called CC=5 SCAR=34.0
202 0 my $array = shift;
203 0 my $level = shift;
204  
205 0 my $out = '';
206 0 foreach my $item (@{$array})
207 {
208 0 ▶ if (!ref $item)
Branch: 3 branches
BranchTrueFalseTotal
if (not ref $item) { } 0 0 0
elsif (ref $item eq 'HASH') { } 0 0 0
elsif (ref $item eq 'ARRAY') { } 0 0 0
209 {
210 0 $out .= $item . "<br/>\n";
211 }
212 elsif (ref $item eq 'HASH')
213 {
214 0 $out .= _format_hash($item,$level + 1);
215 }
216 elsif (ref $item eq 'ARRAY')
217 {
218 0 $out .= _format_array($item,$level + 1);
219 }
220 }
221 0 return $out;
222 } # _format_array
223  
224 =head2 repeat_n
225  
226 {&repeat_n(num,value)}
227  
228 Repeat a format N times.
229 Simple substitution: $$N is the number
230 and $$A is the equivalent letter.
231  
232 =cut
233 0 ▶sub repeat_n {
Subroutine repeat_n: not called CC=2 SCAR=17.9
Pod documented
234 0 my $num = shift;
235 0 my @values = @_;
236  
237 0 my $value = join(',', @values);
238 0 my @out = ();
239 0 my $a = 'A';
240 0 ▶ for (my $i = 1; $i <= $num; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $num) 0 0 0
241 {
242 0 my $line = $value;
243 0 $line =~ s/\$\$N/$i/g;
244 0 $line =~ s/\$\$A/$a/g;
245 0 push @out, $line;
246 0 $a++;
247 }
248 0 return join("\n", @out);
249 } # repeat_n
250  
251 =head2 repeat_for
252  
253 {&repeat_for(pipe_value,value)}
254  
255 Repeat a format for each "pipe-value" (pipe-separated value).
256 Simple substitution:
257 $$F is the "for" value
258 $$N is the number
259 and $$A is the equivalent letter.
260  
261 =cut
262 0 ▶sub repeat_for {
Subroutine repeat_for: not called CC=2 SCAR=17.9
Pod documented
263 0 my $pipeval = shift;
264 0 my @values = @_;
265  
266 0 my @tags = split(/[|]/, $pipeval);
267 0 my $value = join(',', @values);
268 0 my @out = ();
269 0 my $a = 'A';
270 0 my $i = 1;
271 0 foreach my $tag (@tags)
272 {
273 0 my $line = $value;
274 0 $line =~ s/\$\$F/$tag/g;
275 0 $line =~ s/\$\$N/$i/g;
276 0 $line =~ s/\$\$A/$a/g;
277 0 push @out, $line;
278 0 $a++;
279 0 $i++;
280 }
281 0 return join("\n", @out);
282 } # repeat_for
283  
284 =head2 math
285  
286 {&math(mathexpr)}
287  
288 Do math with the given fields.
289  
290 =cut
291 0 ▶sub math {
Subroutine math: not called CC=2 SCAR=17.9
Pod documented
292 0 my $expression = shift;
293  
294 # if they didn't give us anything, return
295 0 ▶ if (!$expression)
Branch: 1 branch
BranchTrueFalseTotal
unless ($expression) 0 0 0
296 {
297 0 return '';
298 }
299 0 my $result = Math::Calc::Parser->evaluate($expression);
300  
301 0 return $result;
302 } # math
303  
304 1 1;
« Muster/Hook/DynamicField.pm ↑ Summary Muster/Hook/Shortcut.pm »