File Coverage

blib/lib/Gears/Template.pm
Criterion Covered Total %
statement 42 49 85.7
branch 9 10 90.0
condition 2 3 66.6
subroutine 7 8 87.5
pod 1 1 100.0
total 61 71 85.9


line stmt bran cond sub pod time code
1             package Gears::Template;
2             $Gears::Template::VERSION = '0.101';
3 1     1   217536 use v5.40;
  1         4  
4 1     1   524 use Mooish::Base -standard;
  1         85528  
  1         9  
5              
6 1     1   271068 use Path::Tiny qw(path);
  1         10585  
  1         80  
7 1     1   497 use Gears::X::Template;
  1         77  
  1         375  
8              
9             has param 'encoding' => (
10             isa => Str,
11             default => 'UTF-8',
12             );
13              
14             has param 'paths' => (
15             isa => ArrayRef [Str],
16             default => sub { [] },
17             );
18              
19             # implements actual rendering of a template
20             # must be reimplemented
21 0         0 sub _render_template ($self, $template_content, $vars)
  0         0  
22 0     0   0 {
  0         0  
  0         0  
23 0         0 ...;
24             }
25              
26 8         17 sub process ($self, $template, $vars = {})
  8         24  
27 8     8 1 13431 {
  8         16  
  8         13  
28 8         22 my $ref = ref $template;
29              
30             # A GLOB or an IO object will be read and returned as a SCALAR template
31             # No reference means a file name
32 8 100 66     49 if (!$ref) {
    100          
    50          
33 3         18 $template = $self->_read_file($self->_find_template($template));
34             }
35             elsif ($ref eq 'GLOB' || $template isa 'IO::Handle') {
36 1         6 $template = $self->_read_file($template);
37             }
38             elsif ($ref eq 'SCALAR') {
39 4         9 $template = $template->$*;
40             }
41             else {
42 0         0 Gears::X::Template->raise("Template must be either a SCALAR or GLOB reference, or an IO::Handle object");
43             }
44              
45 7         36 return $self->_render_template($template, $vars);
46             }
47              
48 3         6 sub _find_template ($self, $name)
49 3     3   7 {
  3         5  
  3         8  
50 3         19 for my $p ($self->paths->@*) {
51 3         10 my $file = "$p/$name";
52 3 100       158 return $file if -f $file;
53             }
54              
55 1         21 Gears::X::Template->raise("Template file not found: $name");
56             }
57              
58 3         8 sub _read_file ($self, $file)
59 3     3   6 {
  3         6  
  3         5  
60 3         5 my $text;
61              
62 3 100       9 if (ref $file) {
63             # read the entire file
64 1         10 local $/ = undef;
65              
66             # make sure to properly rewind the handle after we read from it
67 1         8 my $pos = tell $file;
68 1         52 $text = readline $file;
69 1         15 seek $file, $pos, 0;
70             }
71             else {
72 2         36 $text = path($file)->slurp(
73             {binmode => ':encoding(' . $self->encoding . ')'}
74             );
75             }
76              
77 3         4837 return $text;
78             }
79              
80             __END__