View/Xslate.pm

filter: Statementstmt 92.6% 113 / 122 Branchbran 75.0% 18 / 24 Conditioncond 44.4% 4 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 95.8% 23 / 24 Podpod 80.0% 4 / 5 totaltotal 85.2% 162 / 190 CC 19 SCAR 13.0
CC
19
Cov
87%
process
7.9
render
7.3
_build_xslate
4.0
CRAP 3.7
« Helper/View/Xslate.pm ↑ Summary
1 package Catalyst::View::Xslate;
2 5 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 5 ▶use Moose::Util::TypeConstraints qw(coerce from where via subtype);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use Text::Xslate;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use Scalar::Util qw/blessed weaken/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use File::Find ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 5 our $VERSION = '0.00019';
11  
12 5 extends 'Catalyst::View';
13  
14 5 with 'Catalyst::Component::ApplicationAttribute';
15  
16 5 has catalyst_var => (
17 is => 'rw',
18 isa => 'Str',
19 default => 'c'
20 );
21  
22 5 has template_extension => (
23 is => 'rw',
24 isa => 'Str',
25 default => '.tx'
26 );
27  
28 5 has content_charset => (
29 is => 'rw',
30 isa => 'Str',
31 default => 'UTF-8'
32 );
33  
34 5 has encode_body => (
35 is => 'rw',
36 isa => 'Bool',
37 default => 1,
38 );
39  
40 17 ▶my $clearer = sub { $_[0]->clear_xslate };
Subroutine __ANON__: called CC=1 SCAR=0.0
41  
42 5 has path => (
43 is => 'rw',
44 isa => 'ArrayRef',
45 trigger => $clearer,
46 lazy => 1, builder => '_build_path',
47 );
48  
49 18 ▶sub _build_path { return [ shift->_app->path_to('root') ] }
Subroutine _build_path: called CC=1 SCAR=0.0
50  
51 5 has cache_dir => (
52 is => 'rw',
53 isa => 'Str',
54 trigger => $clearer,
55 );
56  
57 5 has cache => (
58 is => 'rw',
59 isa => 'Int',
60 default => 1,
61 trigger => $clearer,
62 );
63  
64 has function => (
65 is => 'rw',
66 isa => 'HashRef',
67 19 ▶ default => sub { +{} },
Subroutine __ANON__: called CC=1 SCAR=0.0
68 5 trigger => $clearer,
69 );
70  
71 has footer => (
72 is => 'rw',
73 isa => 'ArrayRef',
74 16 ▶ default => sub { +[] },
Subroutine __ANON__: called CC=1 SCAR=0.0
75 5 trigger => $clearer
76 );
77  
78 has header => (
79 is => 'rw',
80 isa => 'ArrayRef',
81 16 ▶ default => sub { +[] },
Subroutine __ANON__: called CC=1 SCAR=0.0
82 5 trigger => $clearer
83 );
84  
85 has module => (
86 is => 'rw',
87 isa => 'ArrayRef',
88 19 ▶ default => sub { +[] },
Subroutine __ANON__: called CC=1 SCAR=0.0
89 5 trigger => $clearer,
90 );
91  
92 5 has input_layer => (
93 is => 'rw',
94 isa => 'Str',
95 trigger => $clearer,
96 );
97  
98 5 has syntax => (
99 is => 'rw',
100 isa => 'Str',
101 trigger => $clearer,
102 );
103  
104 5 has escape => (
105 is => 'rw',
106 isa => 'Str',
107 trigger => $clearer,
108 );
109  
110 5 has type => (
111 is => 'rw',
112 isa => 'Str',
113 trigger => $clearer,
114 );
115  
116 5 has suffix => (
117 is => 'rw',
118 isa => 'Str',
119 trigger => $clearer,
120 default => '.tx',
121 );
122  
123 5 has verbose => (
124 is => 'rw',
125 isa => 'Int',
126 trigger => $clearer,
127 );
128  
129 5 has xslate => (
130 is => 'rw',
131 isa => 'Text::Xslate',
132 clearer => 'clear_xslate',
133 lazy => 1, builder => '_build_xslate',
134 );
135  
136 5 has [qw/line_start tag_start tag_end/] => (is=>'rw', isa=>'Str');
137 5 has [qw/warn_handler die_handler pre_process_handler/] => (is=>'rw', isa=>'CodeRef');
138  
139  
140 9 ▶my $expose_methods_tc = subtype 'HashRef', where { $_ };
Subroutine __ANON__: called CC=1 SCAR=0.0
141 coerce $expose_methods_tc,
142 from 'ArrayRef',
143 3 ▶ via {
Subroutine __ANON__: called CC=1 SCAR=0.0
144 3 my %values = map { $_ => $_ } @$_;
145 3 return \%values;
146 5 };
147  
148 5 has expose_methods => (
149 is => 'ro',
150 isa => $expose_methods_tc,
151 predicate => 'has_expose_methods',
152 coerce => 1,
153 );
154  
155 5 has preload => (
156 is => 'ro',
157 isa => 'Bool',
158 default => 1,
159 );
160  
161 20 ▶sub _build_xslate {
Subroutine _build_xslate: called CC=4 SCAR=14.0
162 20 my $self = shift;
163  
164 20 my $app = $self->_app;
165 20 my $name = $app;
166 20 $name =~ s/::/_/g;
167  
168 my %args = (
169 path => $self->path,
170 cache_dir => $self->cache_dir || File::Spec->catdir(File::Spec->tmpdir, $name),
171 20 ▶ map { ($_ => $self->$_) }
Condition33%1/3
$self->cache_dir || 'File::Spec'->catdir('File::Spec'->tmpdir, $name)
l!l&&r!l&&!r
0200
MC/DC0%0/2
$self->cache_dir || 'File::Spec'->catdir('File::Spec'->tmpdir, $name)
$self->cache_dir 'File::Spec'->catdir('File::Spec'->tmpdir, $name)
Truth table33%1/3
A || B
A= $self->cache_dirB= 'File::Spec'->catdir('File::Spec'->tmpdir, $name)
ABresult
1X1
011
000
172 qw( cache footer function header module )
173 );
174  
175 # optional stuff
176 20 foreach my $field ( qw( input_layer syntax escape verbose suffix type line_start tag_start tag_end warn_handler die_handler pre_process_handler) ) {
177 240 ▶ if (defined(my $value = $self->$field)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $value = $self->$field)) 30 210 240
178 30 $args{$field} = $value;
179 }
180 }
181  
182 20 return Text::Xslate->new(%args);
183 }
184  
185 19 ▶sub BUILD {
Subroutine BUILD: called CC=2 SCAR=7.1
Pod undocumented
186 19 my $self = shift;
187 19 ▶ if ($self->preload) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->preload) 19 0 19
188 19 $self->preload_templates();
189 }
190 }
191  
192 19 ▶sub preload_templates {
Subroutine preload_templates: called CC=2 SCAR=6.9
Pod documented
193 19 my $self = shift;
194 19 my ( $paths, $suffix ) = ( $self->path, $self->suffix );
195 19 my $xslate = $self->xslate;
196 19 foreach my $path (@$paths) {
197 136 ▶ File::Find::find( sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
198 136 ▶ if (/\Q$suffix\E$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/\Q$suffix\E$/) 96 40 136
199 96 my $file = $File::Find::name;
200 96 $file =~ s/\Q$path\E .//xsm;
201 96 $xslate->load_file($file);
202 }
203 19 }, $path);
204 }
205 }
206  
207 5 ▶sub process {
Subroutine process: called CC=7 SCAR=20.7
Pod documented
208 5 my ($self, $c) = @_;
209  
210 5 my $stash = $c->stash;
211 5 ▶ my $template = $stash->{template} || $c->action . $self->template_extension;
Condition66%2/3
$stash->{'template'} || $c->action . $self->template_extension
l!l&&r!l&&!r
410
MC/DC0%0/2
$stash->{'template'} || $c->action . $self->template_extension
$stash->{'template'} $c->action . $self->template_extension
Truth table66%2/3
A || B
A= $stash->{'template'}B= $c->action . $self->template_extension
ABresult
1X1
011
000
212  
213 5 ▶ if (! defined $template) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $template) 0 5 5
214 0 ▶ $c->log->debug('No template specified for rendering') if $c->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $c->debug 0 0 0
215 0 return 0;
216 }
217  
218 5 my $output = eval {
219 5 $self->render( $c, $template, $stash );
220 };
221 5 ▶ if (my $err = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = $@) 0 5 5
222 0 return $self->_rendering_error($c, $err);
223 }
224  
225 5 my $res = $c->response;
226 5 ▶ if (! $res->content_type) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res->content_type) 4 1 5
227 4 $res->content_type('text/html; charset=' . $self->content_charset);
228 }
229  
230 5 ▶ if ( $self->encode_body ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->encode_body) { } 4 1 5
231 4 $res->body( encode($self->content_charset, $output) );
232 }
233 else {
234 1 $res->body( $output );
235 }
236  
237 5 return 1;
238 }
239  
240 6 ▶sub build_exposed_method {
Subroutine build_exposed_method: called CC=1 SCAR=0.0
Pod documented
241 6 my ( $self, $ctx, $code ) = @_;
242 6 my $weak_ctx = $ctx;
243 6 weaken $weak_ctx;
244  
245 6 ▶ return sub { $self->$code($weak_ctx, @_) };
Subroutine __ANON__: called CC=1 SCAR=0.0
246 }
247  
248 14 ▶sub render {
Subroutine render: called CC=7 SCAR=19.8
Pod documented
249 14 my ($self, $c, $template, $vars) = @_;
250  
251 14 ▶ $vars = $vars ? $vars : $c->stash;
Branch: 1 branch
BranchTrueFalseTotal
$vars ? : 11 3 14
252  
253 14 ▶ if ($self->has_expose_methods) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_expose_methods) 3 11 14
254 3 foreach my $exposed_method( keys %{$self->expose_methods} ) {
255 6 ▶ if(my $code = $self->can( $self->expose_methods->{$exposed_method} )) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $code = $self->can($self->expose_methods->{$exposed_method})) { } 6 0 6
256 6 $vars->{$exposed_method} = $self->build_exposed_method($c, $code);
257 } else {
258 0 Catalyst::Exception->throw( "$exposed_method not found in Xslate view" );
259 }
260  
261 }
262 }
263  
264 local $vars->{ $self->catalyst_var } =
265 14 ▶ $vars->{ $self->catalyst_var } || $c;
Condition33%1/3
$$vars{$self->catalyst_var} || $c
l!l&&r!l&&!r
0140
MC/DC0%0/2
$$vars{$self->catalyst_var} || $c
$$vars{$self->catalyst_var} $c
Truth table33%1/3
A || B
A= $$vars{$self->catalyst_var}B= $c
ABresult
1X1
011
000
266  
267 14 ▶ if(ref $template eq 'SCALAR') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $template eq 'SCALAR') { } 3 11 14
268 3 return $self->xslate->render_string( $$template, $vars );
269 } else {
270 11 return $self->xslate->render($template, $vars );
271 }
272 }
273  
274 0 ▶sub _rendering_error {
Subroutine _rendering_error: not called CC=1 SCAR=6.9
275 0 my ($self, $c, $err) = @_;
276 0 my $error = qq/Couldn't render template "$err"/;
277 0 $c->log->error($error);
278 0 $c->error($error);
279 0 return 0;
280 }
281  
282 5 __PACKAGE__->meta->make_immutable();
283  
284 5 1;
285  
286 __END__
« Helper/View/Xslate.pm ↑ Summary