blib/lib/Dancer/Template/TemplateSandbox.pm

filter: Statementstmt 67.2% 37 / 55 Branchbran 33.3% 10 / 30 Conditioncond 60.0% 3 / 5 MC/DCmcdc 66.6% 2 / 3 Subroutinesub 100% 7 / 7 Podpod 100% 2 / 2 totaltotal 59.8% 61 / 102 CC 18 SCAR 39.9
CC
18
Cov
56%
init
96.9
render
6.0
BEGIN
1.0
CRAP 54.2
↑ Summary
1 package Dancer::Template::TemplateSandbox;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Dancer::Config 'setting';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Dancer::FileUtils 'path';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 ▶use base 'Dancer::Template::Abstract';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 3 our $VERSION = '1.00';
11  
12 3 my %_template_config;
13  
14 sub init
15 2 ▶{
Subroutine init: called CC=13 SCAR=45.7
Pod documented
16 2 my ( $self ) = @_;
17  
18 2 ▶ die "Template::Sandbox is needed by Dancer::Template::TemplateSandbox"
Branch: 1 branch
BranchTrueFalseTotal
unless "Dancer::ModuleLoader"->load("Template::Sandbox") 1 1 2
19 unless Dancer::ModuleLoader->load( 'Template::Sandbox' );
20  
21 %_template_config = (
22 open_delimiter => '<%',
23 close_delimiter => '%>',
24 template_toolkit_compat => 1,
25 1 %{$self->config},
26 );
27  
28 1 ▶ $_template_config{ template_root } = setting( 'views' )
Branch: 1 branch
BranchTrueFalseTotal
if setting("views") 0 1 1
29 if setting( 'views' );
30  
31  
32 1 ▶ if( $_template_config{ cache } )
Branch: 1 branch
BranchTrueFalseTotal
if ($_template_config{"cache"}) 0 1 1
33 {
34 0 my ( $cache_type, $cache_dir );
35  
36 $cache_dir = delete $_template_config{ cache_dir }
37 0 ▶ if $_template_config{ cache_dir };
Branch: 1 branch
BranchTrueFalseTotal
if $_template_config{"cache_dir"} 0 0 0
38 $cache_type = delete $_template_config{ cache_type }
39 0 ▶ if $_template_config{ cache_type };
Branch: 1 branch
BranchTrueFalseTotal
if $_template_config{"cache_type"} 0 0 0
40 0 ▶ $cache_type ||= 'memory';
Condition0%0/2
$cache_type ||= "memory"
l!l
00
MC/DC0%0/1
$cache_type ||= "memory"
$cache_type
Truth table0%0/2
A
A= $cache_type
Aresult
11
00
41  
42 0 ▶ if( $_template_config{ cache } eq 'cache_factory' )
Branch: 2 branches
BranchTrueFalseTotal
if ($_template_config{'cache'} eq 'cache_factory') { } 0 0 0
elsif ($_template_config{'cache'} eq 'chi') { } 0 0 0
43 {
44 0 my ( %options );
45  
46 0 ▶ die "Unable to load chosen template caching module: " .
Branch: 1 branch
BranchTrueFalseTotal
unless "Dancer::ModuleLoader"->load("Cache::CacheFactory") 0 0 0
47 "Cache::CacheFactory"
48 unless Dancer::ModuleLoader->load( 'Cache::CacheFactory' );
49  
50 0 ▶ if( $cache_type eq 'file' )
Branch: 1 branch
BranchTrueFalseTotal
if ($cache_type eq 'file') { } 0 0 0
51 {
52 0 %options = (
53 storage => { 'file' => { cache_root => $cache_dir, }, },
54 );
55 }
56 else
57 {
58 0 %options = (
59 storage => { $cache_type => {}, },
60 );
61 }
62  
63 0 $_template_config{ cache } = Cache::CacheFactory->new( %options );
64 }
65 elsif( $_template_config{ cache } eq 'chi' )
66 {
67 0 my ( %options );
68  
69 0 ▶ die "Unable to load chosen template caching module: CHI"
Branch: 1 branch
BranchTrueFalseTotal
unless "Dancer::ModuleLoader"->load("CHI") 0 0 0
70 unless Dancer::ModuleLoader->load( 'CHI' );
71  
72 0 ▶ if( $cache_type eq 'file' )
Branch: 1 branch
BranchTrueFalseTotal
if ($cache_type eq 'file') { } 0 0 0
73 {
74 0 %options = (
75 driver => 'File',
76 root_dir => $cache_dir,
77 );
78 }
79 else
80 {
81 0 %options = (
82 driver => Dancer::ModuleLoader->class_from_setting(
83 $cache_type ),
84 );
85 }
86  
87 0 $_template_config{ cache } = CHI->new( %options );
88 }
89 else
90 {
91 0 die "Unknown template caching module: $_template_config{ cache }";
92 }
93 }
94 }
95  
96 sub render($$$)
97 3 ▶{
Subroutine render: called CC=6 SCAR=18.0
Pod documented
98 3 my ( $self, $template, $tokens ) = @_;
99 3 my ( $ts );
100  
101 3 ▶ die "'$template' is not a regular file"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $template or -f $template 1 2 3
Condition100%3/3
ref $template or -f $template
l!l&&r!l&&!r
111
MC/DC100%2/2
ref $template or -f $template
ref $template -f $template
Truth table100%3/3
A or B
A= ref $templateB= -f $template
ABresult
1X1
011
000
102 if !ref($template) && (!-f $template);
103  
104 2 $ts = Template::Sandbox->new( %_template_config );
105 2 $ts->add_vars( $tokens );
106  
107 2 ▶ if( ref( $template ) )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $template) { } 1 1 2
108 {
109 1 $ts->set_template_string( ${$template} );
110 }
111 else
112 {
113 $template =~ s~^\Q$_template_config{template_root}/\E~~
114 1 ▶ if $_template_config{ template_root };
Branch: 1 branch
BranchTrueFalseTotal
if $_template_config{"template_root"} 0 1 1
115 1 $ts->set_template( $template );
116 }
117  
118 2 my $contentref = $ts->run();
119 2 ▶ return( $contentref ? ${$contentref} : undef );
Branch: 1 branch
BranchTrueFalseTotal
$contentref ? : 2 0 2
120 }
121  
122 3 1;
123 __END__
↑ Summary