Plex.pm

filter: Statementstmt 91.3% 138 / 151 Branchbran 77.5% 31 / 40 Conditioncond 54.1% 13 / 24 MC/DCmcdc 13.3% 2 / 15 Subroutinesub 88.8% 32 / 36 Podpod 68.4% 13 / 19 totaltotal 80.3% 229 / 285 CC 31 SCAR 13.1
CC
31
Cov
85%
cache
7.7
slot
7.1
render
6.0
CRAP 3.7
↑ Summary Plex/Internal.pm »
1 package Template::Plex;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use version; our $VERSION = version->declare('v0.4.1');
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use feature qw<say isa refaliasing>;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶no warnings "experimental";
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10  
11 2 ▶use Log::ger;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Log::OK; #Allow control of logging from the command line
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 ▶use Symbol qw<delete_package>;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 2 ▶use constant KEY_OFFSET=>0;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 use enum ("plex_=0",qw<meta_ args_ sub_ package_ init_done_flag_ skip_
18  
19 cache_
20 slots_ parent_ default_result_
21 2 ▶ >);
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 2 ▶use constant KEY_COUNT=>default_result_ - plex_ +1;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 #Template::Plex::Internal uses the field name constants so import it AFTER
26 #we define them
27 2 ▶use Template::Plex::Internal;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 2 our %top_level_cache;
30 sub new {
31 19 ▶ my ($package, $plex)=@_;
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
32 19 my $self=[];
33 19 $self->[plex_]=$plex;
34 19 $self->[cache_]={};
35 19 bless $self, $package;
36 }
37 sub get_cache {
38 0 ▶ $_[0][cache_];
Subroutine get_cache: not called CC=1 SCAR=6.9
Pod undocumented
39 }
40  
41 #Returns a template loaded and intialised
42 sub load {
43 19 ▶ my ($self, $path, $vars, %opts)=@_;
Subroutine load: called CC=4 SCAR=13.9
Pod documented
44 19 my $template;
45 19 ▶ if(ref($self)){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) { } 6 13 19
46 6 Log::OK::TRACE and log_trace __PACKAGE__." instance load called for $path";
47 6 \my %fields=$self->args;
48  
49 6 my %options=$self->meta->%{qw<root use base>}; #copy
50 6 ▶ $template=Template::Plex::Internal->new(\&Template::Plex::Internal::_prepare_template, $path, $vars?$vars:\%fields, %opts?%opts:%options);
Branch: 2 branches
BranchTrueFalseTotal
$vars ? : 2 4 6
%opts ? : 2 4 6
51  
52 }
53 else{
54 13 Log::OK::TRACE and log_trace __PACKAGE__." class load called for $path";
55 #called on package
56 13 $template=Template::Plex::Internal->new(\&Template::Plex::Internal::_prepare_template, $path, $vars, %opts);
57  
58 }
59 19 $template->setup;
60 19 $template;
61 }
62  
63 #Returns a template which was already loaded can called from the callers position
64 sub cache {
65 10 ▶ my ($self, $id, $path, $vars, %opts)=@_;
Subroutine cache: called CC=7 SCAR=20.4
Pod documented
66 10 Log::OK::TRACE and log_trace __PACKAGE__." cache: $path";
67 10 ▶ $id//=$path.join "", caller; #Set if undefined
Condition66%2/3
$id //= $path . join("", caller)
l!l&&r!l&&!r
820
MC/DC0%0/2
$id //= $path . join("", caller)
$id $path . join("", caller)
Truth table66%2/3
A //= B
A= $idB= $path . join("", caller)
ABresult
1X1
011
000
68 10 ▶ if(ref($self)){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) { } 4 6 10
69 4 ▶ $self->[cache_]{$id} and return $self->[cache_]{$id};
Branch: 1 branch
BranchTrueFalseTotal
$self->[7]{$id} and return $self->[7]{$id} 0 4 4
70  
71 4 my $template=$self->load($path, $vars,%opts);
72 4 ▶ $self->[cache_]{$id}//=$template;
Condition33%1/3
$self->[7]{$id} //= $template
l!l&&r!l&&!r
040
MC/DC0%0/2
$self->[7]{$id} //= $template
$self->[7]{$id} $template
Truth table33%1/3
A //= B
A= $self->[7]{$id}B= $template
ABresult
1X1
011
000
73 }
74 else{
75 6 ▶ $top_level_cache{$id} and return $top_level_cache{$id};
Branch: 1 branch
BranchTrueFalseTotal
$Template::Plex::top_level_cache{$id} and return $Template::Plex::top_level_cache{$id} 1 5 6
76  
77 5 my $template=$self->load($path, $vars,%opts);
78 5 ▶ $top_level_cache{$id}//=$template;
Condition33%1/3
$Template::Plex::top_level_cache{$id} //= $template
l!l&&r!l&&!r
050
MC/DC0%0/2
$Template::Plex::top_level_cache{$id} //= $template
$Template::Plex::top_level_cache{$id} $template
Truth table33%1/3
A //= B
A= $Template::Plex::top_level_cache{$id}B= $template
ABresult
1X1
011
000
79  
80 }
81 }
82 sub immediate {
83 4 ▶ my ($self, $id, $path, $vars, @opts)=@_;
Subroutine immediate: called CC=3 SCAR=12.3
Pod documented
84  
85 4 Log::OK::TRACE and log_trace __PACKAGE__." cache: $path";
86 4 ▶ $id//=$path.join "", caller; #Set if undefined
Condition33%1/3
$id //= $path . join("", caller)
l!l&&r!l&&!r
040
MC/DC0%0/2
$id //= $path . join("", caller)
$id $path . join("", caller)
Truth table33%1/3
A //= B
A= $idB= $path . join("", caller)
ABresult
1X1
011
000
87
88 4 my $template=$self->cache($id, $path, $vars,@opts);
89 4 ▶ return $template->render if $template;
Branch: 1 branch
BranchTrueFalseTotal
if $template 4 0 4
90 0 "";
91  
92 }
93  
94 sub _plex_ {
95 0 ▶ $_[0][Template::Plex::plex_];
Subroutine _plex_: not called CC=1 SCAR=6.9
96 }
97  
98 8 ▶sub meta :lvalue { $_[0][Template::Plex::meta_]; }
Subroutine meta: called CC=1 SCAR=0.0
Pod documented
99  
100 9 ▶sub args :lvalue{ $_[0][Template::Plex::args_]; }
Subroutine args: called CC=1 SCAR=0.0
Pod documented
101  
102 59 ▶sub init_done_flag:lvalue{ $_[0][Template::Plex::init_done_flag_]; }
Subroutine init_done_flag: called CC=1 SCAR=0.0
Pod undocumented
103  
104  
105 sub _render {
106 #sub in plex requires self as first argument
107 41 ▶ return $_[0][sub_](@_);
Subroutine _render: called CC=1 SCAR=0.0
108 }
109  
110 sub skip {
111 21 ▶ Log::OK::DEBUG and log_debug("Template::Plex: Skipping Template: ".$_[0]->meta->{file});
Subroutine skip: called CC=1 SCAR=0.0
Pod documented
112 21 $_[0]->[skip_]->();
113 }
114  
115 #A call to this method will run the sub an preparation
116 #and immediately stop rendering the template
117 sub _init {
118 41 ▶ my ($self, $sub)=@_;
Subroutine _init: called CC=3 SCAR=11.4
119
120 41 ▶ return if $self->[init_done_flag_];
Branch: 1 branch
BranchTrueFalseTotal
if $self->[5] 20 21 41
121 21 Log::OK::DEBUG and log_debug("Template::Plex: Initalising Template: ".$self->meta->{file});
122 21 ▶ unless($self isa Template::Plex){
Branch: 1 branch
BranchTrueFalseTotal
unless ($self isa "Template::Plex") 0 21 21
123 #if($self->[meta_]{package} ne caller){
124 0 Log::OK::ERROR and log_error("Template::Plex: init must only be called within a template: ".$self->meta->{file});
125 0 return;
126 }
127  
128 21 $self->pre_init;
129 21 $sub->();
130 21 $self->post_init;
131  
132 21 $self->[init_done_flag_]=1;
133 21 $self->skip;
134 0 ""; #Must return an empty string
135 }
136  
137 ▶sub pre_init {
Subroutine pre_init: called CC=1 SCAR=0.0
Pod documented
138  
139 }
140  
141 ▶sub post_init {
Subroutine post_init: called CC=1 SCAR=0.0
Pod documented
142  
143 }
144 ▶sub prefix {
Subroutine prefix: called CC=1 SCAR=0.0
Pod undocumented
145 }
146 ▶sub postfix {
Subroutine postfix: called CC=1 SCAR=0.0
Pod undocumented
147 }
148  
149 #Execute the template in setup mode
150 sub setup {
151 21 ▶ my $self=shift;
Subroutine setup: called CC=3 SCAR=11.8
Pod undocumented
152 #Test that the caller is not the template package
153 21 Log::OK::DEBUG and log_debug("Template::Plex: Setup Template: ".$self->meta->{file});
154 21 ▶ if($self->[meta_]{package} eq caller){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[1]{"package"} eq caller) 0 21 21
155 #Log::OK::ERROR and log_error("Template::Plex: setup must only be called outside a template: ".$self->meta->{file});
156 # return;
157 }
158 21 $self->[init_done_flag_]=undef;
159 21 $self->render(@_);
160
161 #Check if an init block was used
162 21 ▶ unless($self->[init_done_flag_]){
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->[5]) 0 21 21
163 0 Log::OK::WARN and log_warn "Template::Plex ignoring no \@{[init{...}]} block in template from ". $self->meta->{file};
164 0 $self->[init_done_flag_]=1;
165 }
166 21 "";
167 }
168  
169 # Slotting and Inheritance
170 #
171 #
172  
173 #Marks a slot in a parent template.
174 #A child template can fill this out by calling fill_slot on the parent
175 sub slot {
176 6 ▶ my ($self, $slot_name, $default_value)=@_;
Subroutine slot: called CC=7 SCAR=19.5
Pod documented
177 6 ▶ $slot_name//="default"; #If no name assume default
Condition100%2/2
$slot_name //= "default"
l!l
42
MC/DC100%1/1
$slot_name //= "default"
$slot_name
Truth table100%2/2
A
A= $slot_name
Aresult
11
00
178  
179 6 Log::OK::TRACE and log_trace __PACKAGE__.": Template called slot: $slot_name";
180 6 my $data=$self->[slots_]{$slot_name};
181 6 my $output="";
182
183 6 ▶ $data//=$default_value;
Condition66%2/3
$data //= $default_value
l!l&&r!l&&!r
510
MC/DC0%0/2
$data //= $default_value
$data $default_value
Truth table66%2/3
A //= B
A= $dataB= $default_value
ABresult
1X1
011
000
184 6 ▶ if($data isa Template::Plex){
Branch: 1 branch
BranchTrueFalseTotal
if ($data isa 'Template::Plex') { } 3 3 6
185 #render template
186 3 ▶ if($slot_name eq "default"){
Branch: 1 branch
BranchTrueFalseTotal
if ($slot_name eq 'default') { } 2 1 3
187 2 Log::OK::TRACE and log_trace __PACKAGE__.": copy default slot";
188 2 ▶ $output.=$self->[default_result_]//"";
Condition50%1/2
$self->[10] // ""
l!l
20
MC/DC0%0/1
$self->[10] // ""
$self->[10]
Truth table50%1/2
A
A= $self->[10]
Aresult
11
00
189 }
190 else {
191 1 Log::OK::TRACE and log_trace __PACKAGE__.": render non default template slot";
192 1 $output.=$data->render;
193 }
194 }
195 else {
196 3 Log::OK::TRACE and log_trace __PACKAGE__.": render non template slot";
197 #otherwise treat as text
198 3 ▶ $output.=$data//"";
Condition50%1/2
$data // ""
l!l
30
MC/DC0%0/1
$data // ""
$data
Truth table50%1/2
A
A= $data
Aresult
11
00
199 }
200 6 $output
201 }
202  
203 sub fill_slot {
204 3 ▶ my ($self)=shift;
Subroutine fill_slot: called CC=4 SCAR=15.5
Pod documented
205 3 my $parent=$self->[parent_];
206 3 ▶ unless($parent){
Branch: 1 branch
BranchTrueFalseTotal
unless ($parent) 0 3 3
207 0 Log::OK::WARN and log_warn __PACKAGE__.": No parent setup for: ". $self->meta->{file};
208 0 return;
209 }
210  
211 3 ▶ unless(@_){
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 3 0 3
212 #An unnamed fill spec implies the default slot
213 0 $parent->[slots_]{default}=$self;
214 }
215 else{
216 3 for my ($k,$v)(@_){
217 3 $parent->[slots_]{$k}=$v;
218 }
219 }
220 3 "";
221 }
222  
223  
224 sub inherit {
225 2 ▶ my ($self, $path)=@_;
Subroutine inherit: called CC=1 SCAR=0.0
Pod documented
226 2 Log::OK::DEBUG and log_debug __PACKAGE__.": Inherit: $path";
227 #If any parent variables have be setup load the parent template
228  
229 #Setup the parent. Cached with path
230 2 my $p=$self->load($path, $self->args, $self->meta->%*);
231 2 $p->[slots_]={};
232  
233 #Add this template to the default slot
234 2 $p->[slots_]{default}=$self;
235 2 $self->[parent_]=$p;
236 }
237  
238 sub render {
239 59 ▶ my ($self, $fields, $top_down)=@_;
Subroutine render: called CC=6 SCAR=17.9
Pod documented
240 #We don't call parent render if we are uninitialised
241  
242  
243
244 #If the template uninitialized, we just do a first pass
245 59 ▶ unless($self->init_done_flag){
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->init_done_flag) 21 38 59
246  
247 21 return $self->_render;
248  
249 }
250  
251 38 Log::OK::TRACE and log_trace __PACKAGE__.": render :".$self->meta->{file}." flag: ".($top_down//"");
252  
253 #locate the 'top level' template and call downwards
254 38 my $p=$self;
255 38 ▶ if(!$top_down){
Branch: 1 branch
BranchTrueFalseTotal
if (not $top_down) { } 18 20 38
256 18 ▶ while($p->[parent_]){
Branch: 1 branch
BranchTrueFalseTotal
if ($p->[9]) 2 18 20
257 2 $p=$p->[parent_];
258 }
259 18 $p->render($fields,1);
260 }
261 else{
262 #This is Normal template or top of hierarchy
263 #child has called parent and parent is the top
264 #
265 #Turn it around and call back down the chain
266 #
267  
268 20 Log::OK::TRACE and log_trace __PACKAGE__.": render: no parent bottom up. assume normal render";
269 #Check slots. Slots indicate we need to call the child first
270 20 ▶ if($self->[slots_] and $self->[slots_]->%*){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[8] and %{$self->[8]}) 2 18 20
Condition66%2/3
$self->[8] and %{$self->[8]}
!ll&&!rl&&r
1802
MC/DC50%1/2
$self->[8] and %{$self->[8]}
$self->[8] %{$self->[8]}
Truth table66%2/3
A and B
A= $self->[8]B= %{$self->[8]}
ABresult
0X0
100
111
271 2 Log::OK::TRACE and log_trace __PACKAGE__.": render: rendering default slot";
272 2 $self->[default_result_]=$self->[slots_]{default}->render($fields,1);
273 }
274  
275 #now call render on self. This renders non hierarchial templates
276 20 Log::OK::TRACE and log_trace __PACKAGE__.": render: rendering body and sub templates";
277 20 my $total=$self->_render($fields); #Call down the chain with top_down flag
278 20 $self->[default_result_]=""; #Clear
279 20 return $total;
280 }
281 }
282  
283 0 ▶sub parent {$_[0][parent_];}
Subroutine parent: not called CC=1 SCAR=6.9
Pod documented
284  
285  
286  
287  
288  
289  
290  
291 sub DESTROY {
292 0 ▶ delete_package $_[0][package_] if $_[0][package_];
Subroutine DESTROY: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if $_[0][4] 0 0 0
293 }
294  
295 #Internal testing use only
296 sub __internal_test_proxy__ {
297 1 ▶ "PROXY";
Subroutine __internal_test_proxy__: called CC=1 SCAR=0.0
298 }
299  
300 2 1;
↑ Summary Plex/Internal.pm »