View/Haml.pm

filter: Statementstmt 54.7% 46 / 84 Branchbran 13.6% 3 / 22 Conditioncond 16.6% 1 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 55.5% 10 / 18 Podpod 66.6% 2 / 3 totaltotal 45.2% 62 / 137 CC 15 SCAR 30.5
CC
15
Cov
45%
process
72.0
render
12.4
_reset_attribute
6.0
CRAP 21.2
↑ Summary Helper/View/Haml.pm »
1 package Catalyst::View::Haml;
2 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use Text::Haml;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Path::Class::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 extends 'Catalyst::View';
11 2 our $VERSION = '1.00';
12  
13 2 has 'haml' => ( is => 'rw', isa => 'Text::Haml' );
14  
15 2 has 'catalyst_var' => ( is => 'rw', isa => 'Str', default => 'c' );
16  
17 2 has 'template_extension' => ( is => 'rw', isa => 'Str', default => '.haml' );
18  
19 2 has 'path' => ( is => 'rw', isa => 'ArrayRef' );
20  
21 2 has 'charset' => ( is => 'rw', isa => 'Str', default => 'utf-8' );
22  
23 has 'format' => (
24 is => 'rw',
25 isa => 'Str',
26 default => 'xhtml',
27 0 ▶ trigger => sub { \&_reset_attribute('format', @_) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
28 2 );
29  
30 has 'vars_as_subs' => (
31 is => 'rw',
32 isa => 'Bool',
33 default => 0,
34 0 ▶ trigger => sub { \&_reset_attribute('vars_as_subs', @_) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
35 2 );
36  
37 has 'escape_html' => (
38 is => 'rw',
39 isa => 'Bool',
40 default => 1,
41 0 ▶ trigger => sub { \&_reset_attribute('escape_html', @_) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
42 2 );
43  
44 0 ▶sub _reset_attribute {
Subroutine _reset_attribute: not called CC=2 SCAR=17.9
45 0 my ($method_name, $self, $value )= @_;
46
47 0 ▶ $self->_build_haml() unless $self->haml;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->haml 0 0 0
48 0 $self->haml->$method_name( $value );
49 }
50  
51 1 ▶sub _build_haml {
Subroutine _build_haml: called CC=2 SCAR=7.0
52 1 my ($self, $c) = @_;
53  
54 1 my $haml = Text::Haml->new(
55 vars_as_subs => $self->vars_as_subs,
56 encoding => $self->charset,
57 escape_html => $self->escape_html,
58 format => $self->format,
59 );
60 1 ▶ $self->path([ $c->path_to('root') ]) unless $self->path;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->path 1 0 1
61 1 $self->haml( $haml );
62 }
63  
64  
65 2 ▶sub ACCEPT_CONTEXT {
Subroutine ACCEPT_CONTEXT: called CC=2 SCAR=6.9
Pod documented
66 2 my ($self, $c) = @_;
67  
68 2 ▶ $self->_build_haml($c) unless $self->haml;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->haml 1 1 2
69 2 return $self;
70 }
71  
72  
73 0 ▶sub process {
Subroutine process: not called CC=8 SCAR=42.8
Pod documented
74 0 my ($self, $c) = @_;
75  
76 0 my $stash = $c->stash;
77 my $template = $stash->{template}
78 0 ▶ || $c->action . $self->template_extension;
Condition0%0/3
$stash->{'template'} || $c->action . $self->template_extension
l!l&&r!l&&!r
000
MC/DC0%0/2
$stash->{'template'} || $c->action . $self->template_extension
$stash->{'template'} $c->action . $self->template_extension
Truth table0%0/3
A || B
A= $stash->{'template'}B= $c->action . $self->template_extension
ABresult
1X1
011
000
79  
80 0 ▶ unless (defined $template) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $template) 0 0 0
81 0 ▶ $c->log->debug('No template specified for rendering') if $c->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $c->debug 0 0 0
82 0 return 0;
83 }
84  
85 0 ▶ $self->_build_haml( $c ) unless $self->haml;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->haml 0 0 0
86  
87 0 foreach my $path ( @{ $self->path } ) {
88 0 my $file = Path::Class::File->new( $path, $template );
89 0 ▶ next unless -e $file->stringify;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $file->stringify 0 0 0
90  
91 0 my $output;
92 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
93 0 $output = eval { $self->render( $c, $file, $stash ) };
94 }
95 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
96 0 return $self->_rendering_error( $c, "$file : $_" );
97 0 };
98
99 0 my $res = $c->response;
100 0 ▶ unless ( $res->content_type ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res->content_type) 0 0 0
101 0 $res->content_type('text/html; charset=' . $self->charset);
102 }
103  
104 0 $res->body( $output );
105  
106 0 return 1;
107 }
108 }
109  
110  
111 2 ▶sub render {
Subroutine render: called CC=5 SCAR=25.2
Pod undocumented
112 2 my ($self, $c, $file, $vars) = @_;
113  
114 local $vars->{ $self->catalyst_var } =
115 2 ▶ $vars->{ $self->catalyst_var } || $c;
Condition33%1/3
$$vars{$self->catalyst_var} || $c
l!l&&r!l&&!r
020
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
116  
117 2 my $fh = $file->openr;
118 1 $fh->binmode(':utf8');
119
120 # slurp file (chunk size = 4096 bytes)
121 1 my $tmpl = '';
122 1 ▶ while ( $fh->sysread( my $buffer, 4096, 0 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fh->sysread(my $buffer, 4096, 0)) 0 0 0
123 0 $tmpl .= $buffer;
124 }
125  
126 # handle encoding
127 0 ▶ $tmpl = Encode::decode($self->charset, $tmpl) if $self->charset;
Branch: 1 branch
BranchTrueFalseTotal
if $self->charset 0 0 0
128  
129 # render
130 0 my $output = $self->haml->render($tmpl, %$vars);
131 0 ▶ if (my $error = $self->haml->error) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $self->haml->error) 0 0 0
132 0 croak $error;
133 }
134 0 return $output;
135 }
136  
137  
138 0 ▶sub _rendering_error {
Subroutine _rendering_error: not called CC=1 SCAR=6.9
139 0 my ($self, $c, $err) = @_;
140 0 my $error = qq/Couldn't render template "$err"/;
141 0 $c->log->error($error);
142 0 $c->error($error);
143 0 return 0;
144 }
145  
146 2 __PACKAGE__->meta->make_immutable();
147  
148  
149 2 42; # End of Catalyst::View::Haml
150  
151 __END__
↑ Summary Helper/View/Haml.pm »