Handlebars.pm

filter: Statementstmt 100% 85 / 85 Branchbran 100% 8 / 8 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 23 / 23 Podpod 33.3% 2 / 6 totaltotal 95.2% 121 / 127 CC 7 SCAR 3.9
CC
7
Cov
99%
canonicalize_vars
3.0
__ANON__
2.0
__ANON__
2.0
CRAP 1.5
« Handlebars/Compiler.pm ↑ Summary Handlebars/Symbol.pm »
1 package Text::Handlebars;
2 15 our $AUTHORITY = 'cpan:DOY';
3 15 $Text::Handlebars::VERSION = '0.05';
4 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 # ABSTRACT: http://handlebarsjs.com/ for Text::Xslate
7  
8 15 ▶use Text::Xslate 2.0000;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 15 ▶use base 'Text::Xslate';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 15 ▶use Scalar::Util 'weaken';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 15 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14  
15 162 ▶sub default_helpers {
Subroutine default_helpers: called CC=1 SCAR=0.0
Pod undocumented
16 162 my $class = shift;
17 return {
18 3 ▶ with => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
19 3 my ($context, $new_context, $options) = @_;
20 3 return $options->{fn}->($new_context);
21 },
22 14 ▶ each => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
23 14 my ($context, $list, $options) = @_;
24 14 return join '', map { $options->{fn}->($_) } @$list;
25 },
26 8 ▶ if => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
27 8 my ($context, $conditional, $options) = @_;
28 return $conditional
29 ? $options->{fn}->($context)
30 8 ▶ : $options->{inverse}->($context);
Branch: 1 branch
BranchTrueFalseTotal
$conditional ? : 4 4 8
31 },
32 2 ▶ unless => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
33 2 my ($context, $conditional, $options) = @_;
34 return $conditional
35 ? $options->{inverse}->($context)
36 2 ▶ : $options->{fn}->($context);
Branch: 1 branch
BranchTrueFalseTotal
$conditional ? : 1 1 2
37 },
38 162 };
39 }
40  
41 81 ▶sub default_functions {
Subroutine default_functions: called CC=1 SCAR=0.0
Pod undocumented
42 81 my $class = shift;
43 return {
44 81 %{ $class->SUPER::default_functions(@_) },
45 81 %{ $class->default_helpers },
46 };
47 }
48  
49 81 ▶sub options {
Subroutine options: called CC=1 SCAR=0.0
Pod undocumented
50 81 my $class = shift;
51  
52 81 my $options = $class->SUPER::options(@_);
53  
54 81 $options->{compiler} = 'Text::Handlebars::Compiler';
55 81 $options->{helpers} = {};
56  
57 81 return $options;
58 }
59  
60 81 ▶sub _register_builtin_methods {
Subroutine _register_builtin_methods: called CC=2 SCAR=6.9
61 81 my $self = shift;
62 81 my ($funcs) = @_;
63  
64 81 weaken(my $weakself = $self);
65 3 ▶ $funcs->{'(render_string)'} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
66 3 my ($to_render, $vars) = @_;
67 3 return $weakself->render_string($to_render, $vars);
68 81 };
69 34 ▶ $funcs->{'(make_block_helper)'} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
70 34 my ($vars, $code, $raw_text, $else_raw_text, $hash) = @_;
71  
72 34 my $options = {};
73 49 ▶ $options->{fn} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
74 49 my ($new_vars) = @_;
75 $new_vars = {
76 49 %{ canonicalize_vars($new_vars) },
77 '..' => $vars,
78 };
79 49 return $weakself->render_string($raw_text, $new_vars);
80 34 };
81 7 ▶ $options->{inverse} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
82 7 my ($new_vars) = @_;
83 $new_vars = {
84 7 %{ canonicalize_vars($new_vars) },
85 '..' => $vars,
86 };
87 7 return $weakself->render_string($else_raw_text, $new_vars);
88 34 };
89 34 $options->{hash} = $hash;
90  
91 34 ▶ return sub { $code->(@_, $options); };
Subroutine __ANON__: called CC=1 SCAR=0.0
92 81 };
93  
94 81 for my $helper (keys %{ $self->{helpers} }) {
95 19 $funcs->{$helper} = $self->{helpers}{$helper};
96 }
97 }
98  
99 145 ▶sub _compiler {
Subroutine _compiler: called CC=2 SCAR=6.9
100 145 my $self = shift;
101  
102 145 ▶ if (!ref($self->{compiler})) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $self->{'compiler'}) { } 81 64 145
103 81 my $compiler = $self->SUPER::_compiler(@_);
104 81 $compiler->define_helper(keys %{ $self->{helpers} });
105 81 $compiler->define_helper(keys %{ $self->default_helpers });
106 81 return $compiler;
107 }
108 else {
109 64 return $self->SUPER::_compiler(@_);
110 }
111 }
112  
113 139 ▶sub render_string {
Subroutine render_string: called CC=1 SCAR=0.0
Pod documented
114 139 my $self = shift;
115 139 my ($string, $vars) = @_;
116  
117 139 return $self->SUPER::render_string($string, canonicalize_vars($vars));
118 }
119  
120 176 ▶sub render {
Subroutine render: called CC=1 SCAR=0.0
Pod documented
121 176 my $self = shift;
122 176 my ($name, $vars) = @_;
123  
124 176 return $self->SUPER::render($name, canonicalize_vars($vars));
125 }
126  
127 371 ▶sub canonicalize_vars {
Subroutine canonicalize_vars: called CC=3 SCAR=11.0
Pod undocumented
128 371 my ($vars) = @_;
129 371 ▶ if (ref($vars) && ref($vars) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $vars and ref $vars eq 'HASH') { } 174 197 371
Condition66%2/3
ref $vars and ref $vars eq "HASH"
!ll&&!rl&&r
1970174
MC/DC50%1/2
ref $vars and ref $vars eq "HASH"
ref $vars ref $vars eq "HASH"
Truth table66%2/3
A and B
A= ref $varsB= ref $vars eq "HASH"
ABresult
0X0
100
111
130 174 return $vars;
131 }
132 else {
133 197 return { '.' => $vars };
134 }
135 }
136  
137  
138 15 1;
139  
140 __END__
« Handlebars/Compiler.pm ↑ Summary Handlebars/Symbol.pm »