| 1 | package Gears::Template; | ||||||||||||||||||||
| 2 | 1 | $Gears::Template::VERSION = '0.105'; | |||||||||||||||||||
| 3 | 1 | ▶ | use v5.40; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 1 | ▶ | use Mooish::Base -standard; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | 1 | ▶ | use Path::Tiny qw(path); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | 1 | ▶ | use Gears::X::Template; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | 1 | has param 'encoding' => ( | |||||||||||||||||||
| 10 | isa => Str, | ||||||||||||||||||||
| 11 | default => 'UTF-8', | ||||||||||||||||||||
| 12 | ); | ||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | has param 'paths' => ( | ||||||||||||||||||||
| 15 | isa => ArrayRef [Str], | ||||||||||||||||||||
| 16 | 3 | ▶ | default => sub { [] }, | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 17 | 1 | ); | |||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | # implements actual rendering of a template | ||||||||||||||||||||
| 20 | # must be reimplemented | ||||||||||||||||||||
| 21 | 0 | ▶ | sub _render_template ($self, $template_content, $vars) | ||||||||||||||||||
|
Subroutine
_render_template: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 22 | { | ||||||||||||||||||||
| 23 | 0 | ...; | |||||||||||||||||||
| 24 | } | ||||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | 8 | ▶ | sub process ($self, $template, $vars = {}) | ||||||||||||||||||
|
Subroutine
process: called CC=6 SCAR=18.2
Pod
documented
| |||||||||||||||||||||
| 27 | 8 | { | |||||||||||||||||||
| 28 | 8 | 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 | ▶ | if (!$ref) { | ||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||
| 33 | 3 | $template = $self->_read_file($self->_find_template($template)); | |||||||||||||||||||
| 34 | } | ||||||||||||||||||||
| 35 | ▶ | elsif ($ref eq 'GLOB' || $template isa 'IO::Handle') { | |||||||||||||||||||
|
Condition66%2/3
$ref eq "GLOB" or $template isa "IO::Handle"
MC/DC50%1/2
$ref eq "GLOB" or $template isa "IO::Handle"
$ref eq "GLOB"
$template isa "IO::Handle"
Truth table66%2/3
A or B
A= $ref eq "GLOB"B= $template isa "IO::Handle"
| |||||||||||||||||||||
| 36 | 1 | $template = $self->_read_file($template); | |||||||||||||||||||
| 37 | } | ||||||||||||||||||||
| 38 | elsif ($ref eq 'SCALAR') { | ||||||||||||||||||||
| 39 | 4 | $template = $template->$*; | |||||||||||||||||||
| 40 | } | ||||||||||||||||||||
| 41 | else { | ||||||||||||||||||||
| 42 | 0 | Gears::X::Template->raise("Template must be either a SCALAR or GLOB reference, or an IO::Handle object"); | |||||||||||||||||||
| 43 | } | ||||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | 7 | return $self->_render_template($template, $vars); | |||||||||||||||||||
| 46 | } | ||||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | 3 | ▶ | sub _find_template ($self, $name) | ||||||||||||||||||
|
Subroutine
_find_template: called CC=3 SCAR=11.0
| |||||||||||||||||||||
| 49 | { | ||||||||||||||||||||
| 50 | 3 | for my $p ($self->paths->@*) { | |||||||||||||||||||
| 51 | 3 | my $file = "$p/$name"; | |||||||||||||||||||
| 52 | 3 | ▶ | return $file if -f $file; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 53 | } | ||||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | 1 | Gears::X::Template->raise("Template file not found: $name"); | |||||||||||||||||||
| 56 | } | ||||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | 3 | ▶ | sub _read_file ($self, $file) | ||||||||||||||||||
|
Subroutine
_read_file: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 59 | { | ||||||||||||||||||||
| 60 | 3 | my $text; | |||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | 3 | ▶ | if (ref $file) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 63 | # read the entire file | ||||||||||||||||||||
| 64 | 1 | local $/ = undef; | |||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | # make sure to properly rewind the handle after we read from it | ||||||||||||||||||||
| 67 | 1 | my $pos = tell $file; | |||||||||||||||||||
| 68 | 1 | $text = readline $file; | |||||||||||||||||||
| 69 | 1 | seek $file, $pos, 0; | |||||||||||||||||||
| 70 | } | ||||||||||||||||||||
| 71 | else { | ||||||||||||||||||||
| 72 | 2 | $text = path($file)->slurp( | |||||||||||||||||||
| 73 | {binmode => ':encoding(' . $self->encoding . ')'} | ||||||||||||||||||||
| 74 | ); | ||||||||||||||||||||
| 75 | } | ||||||||||||||||||||
| 76 | |||||||||||||||||||||
| 77 | 3 | return $text; | |||||||||||||||||||
| 78 | } | ||||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | __END__ | ||||||||||||||||||||