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 39 ▶use Kelp::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 39 ▶use Template::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 39 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 39 ▶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 39 attr encoding => 'UTF-8';
10 18 ▶attr tt => sub { Template::Tiny->new };
Subroutine __ANON__: called CC=1 SCAR=0.0
11  
12 sub process
13 94 ▶{
Subroutine process: called CC=5 SCAR=16.2
Pod documented
14 94 my ($self, $template, $vars) = @_;
15  
16 94 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 94 ▶ if (!$ref) {
Branch: 3 branches
BranchTrueFalseTotal
if (not $ref) { } 73 21 94
elsif ($ref =~ /^IO/ or $ref eq 'GLOB') { } 5 16 21
elsif ($ref ne 'SCALAR') { } 0 16 16
21 73 $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 70 my $output;
31 70 $self->tt->process($template, $vars, \$output);
32 70 return $output;
33 }
34  
35 sub find_template
36 73 ▶{
Subroutine find_template: called CC=3 SCAR=11.0
Pod undocumented
37 73 my ($self, $name) = @_;
38  
39 73 my $file;
40 73 for my $p ('.', @{$self->paths}) {
41 143 $file = "$p/$name";
42 143 ▶ return $file if -e $file;
Branch: 1 branch
BranchTrueFalseTotal
if -e $file 49 94 143
43 }
44  
45 24 return undef;
46 }
47  
48 sub _read_file
49 78 ▶{
Subroutine _read_file: called CC=2 SCAR=6.9
50 78 my ($self, $file) = @_;
51 78 my $text;
52  
53 78 ▶ if (ref $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $file) { } 5 73 78
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 73 $text = path($file)->slurp(
65 {binmode => ':encoding(' . $self->encoding . ')'}
66 );
67 }
68  
69 54 return \$text;
70 }
71  
72 39 1;
73  
74 __END__
75  
76 =pod
77  
78 =head1 NAME
79  
80 Kelp::Template - A very minimal template rendering engine for Kelp
81  
82 =head1 SYNOPSIS
83  
84 my $t = Kelp::Template->new;
85 say $t->process('file.tt', { bar => 'foo' });
86  
87 =head1 DESCRIPTION
88  
89 This module provides basic template rendering using L<Template::Tiny>.
90  
91 =head1 ATTRIBUTES
92  
93 =head2 paths
94  
95 An arrayref of paths to use when looking for template files.
96  
97 =head2 encoding
98  
99 Specifies the text encoding of the template files. The default value is C<UTF-8>.
100  
101 =head1 METHODS
102  
103 =head2 process( $template, \%vars )
104  
105 Processes a template and returns the parsed text. The template may be a file name,
106 a reference to a text, a GLOB or an IO object.
107  
108 say $t->process(\"Hello [% who %]", { who => 'you' });
109  
110 =cut
111  
« blib/lib/Kelp/Test.pm ↑ Summary blib/lib/Kelp/Generator.pm »