blib/lib/Kelp/Template.pm

filter: Statementstmt 95.3% 41 / 43 Branchbran 90.0% 9 / 10 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 88.8% 8 / 9 Podpod 50.0% 1 / 2 totaltotal 92.7% 64 / 69 CC 8 SCAR 10.2
CC
8
Cov
95%
process
5.0
find_template
3.0
_read_file
2.0
CRAP 2.8
« blib/lib/Kelp/Test.pm ↑ Summary blib/lib/Kelp/Generator.pm »
1 package Kelp::Template;
2  
3 41 ▶use Kelp::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 41 ▶use Template::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 41 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 41 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 0 ▶attr paths => sub { [] };
Subroutine __ANON__: not called CC=1 SCAR=1.2
9 41 attr encoding => 'UTF-8';
10 19 ▶attr tt => sub { Template::Tiny->new };
Subroutine __ANON__: called CC=1 SCAR=0.0
11  
12 sub process
13 97 ▶{
Subroutine process: called CC=5 SCAR=16.2
Pod documented
14 97 my ($self, $template, $vars) = @_;
15  
16 97 my $ref = ref $template;
17  
18 # A GLOB or an IO object will be read and returned as a SCALAR template
19 # No reference means a file name
20 97 ▶ if (!$ref) {
Branch: 3 branches
BranchTrueFalseTotal
if (not $ref) { } 76 21 97
elsif ($ref =~ /^IO/ or $ref eq 'GLOB') { } 5 16 21
elsif ($ref ne 'SCALAR') { } 0 16 16
21 76 $template = $self->_read_file($self->find_template($template));
22 }
23 ▶ elsif ($ref =~ /^IO/ || $ref eq 'GLOB') {
Condition100%3/3
$ref =~ /^IO/ or $ref eq "GLOB"
l!l&&r!l&&!r
2316
MC/DC100%2/2
$ref =~ /^IO/ or $ref eq "GLOB"
$ref =~ /^IO/ $ref eq "GLOB"
Truth table100%3/3
A or B
A= $ref =~ /^IO/B= $ref eq "GLOB"
ABresult
1X1
011
000
24 5 $template = $self->_read_file($template);
25 }
26 elsif ($ref ne 'SCALAR') {
27 0 croak "Template reference must be SCALAR, GLOB or an IO object";
28 }
29  
30 72 my $output;
31 72 $self->tt->process($template, $vars, \$output);
32 72 return $output;
33 }
34  
35 sub find_template
36 76 ▶{
Subroutine find_template: called CC=3 SCAR=11.0
Pod undocumented
37 76 my ($self, $name) = @_;
38  
39 76 my $file;
40 76 for my $p ('.', @{$self->paths}) {
41 150 $file = "$p/$name";
42 150 ▶ return $file if -e $file;
Branch: 1 branch
BranchTrueFalseTotal
if -e $file 51 99 150
43 }
44  
45 25 return undef;
46 }
47  
48 sub _read_file
49 81 ▶{
Subroutine _read_file: called CC=2 SCAR=6.9
50 81 my ($self, $file) = @_;
51 81 my $text;
52  
53 81 ▶ if (ref $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $file) { } 5 76 81
54  
55 # read the entire file
56 5 local $/ = undef;
57  
58 # make sure to properly rewind the handle after we read from it
59 5 my $pos = tell $file;
60 5 $text = readline $file;
61 5 seek $file, $pos, 0;
62 }
63 else {
64 76 $text = path($file)->slurp(
65 {binmode => ':encoding(' . $self->encoding . ')'}
66 );
67 }
68  
69 56 return \$text;
70 }
71  
72 41 1;
73  
74 __END__
« blib/lib/Kelp/Test.pm ↑ Summary blib/lib/Kelp/Generator.pm »