blib/lib/Catalyst/View/MicroTemplate.pm

filter: Statementstmt 98.3% 58 / 59 Branchbran 50.0% 5 / 10 Conditioncond 35.7% 5 / 14 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 100% 12 / 12 Podpod 50.0% 2 / 4 totaltotal 75.9% 82 / 108 CC 11 SCAR 12.9
CC
11
Cov
82%
__ANON__
7.3
process
4.2
get_template_file
3.4
CRAP 3.6
↑ Summary
1 package Catalyst::View::MicroTemplate;
2 3 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use Text::MicroTemplate::Extended;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use namespace::clean -except => qw(meta);
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 our $VERSION = '0.00002';
7  
8 3 extends 'Catalyst::View';
9  
10 3 has context => (
11 is => 'rw',
12 isa => 'Catalyst',
13 );
14  
15 3 has content_type => (
16 is => 'ro',
17 isa => 'Str',
18 default => 'text/html'
19 );
20  
21 3 has charset => (
22 is => 'ro',
23 isa => 'Str',
24 default => 'utf8',
25 );
26  
27 3 has include_path => (
28 is => 'ro',
29 isa => 'ArrayRef',
30 );
31  
32 3 has template_suffix => (
33 is => 'ro',
34 isa => 'Str',
35 lazy_build => 1
36 );
37  
38 3 has stash_key => (
39 is => 'ro',
40 isa => 'Str',
41 lazy_build => 1
42 );
43  
44 3 has template_args => (
45 is => 'ro',
46 isa => 'HashRef',
47 lazy_build => 1
48 );
49  
50 3 has template => (
51 is => 'ro',
52 isa => 'Text::MicroTemplate::Extended',
53 lazy_build => 1,
54 );
55  
56 9 ▶around BUILDARGS => sub {
Subroutine __ANON__: called CC=6 SCAR=19.9
57 9 my ($next, $self, $c, $args) = @_;
58  
59 9 my $paths = $args->{include_path};
60 9 ▶ if (!$paths || ref $paths ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (not $paths or ref $paths ne "ARRAY") 9 0 9
Condition33%1/3
not $paths or ref $paths ne "ARRAY"
l!l&&r!l&&!r
900
MC/DC0%0/2
not $paths or ref $paths ne "ARRAY"
not $paths ref $paths ne "ARRAY"
Truth table33%1/3
not(A) or B
A= not $pathsB= ref $paths ne "ARRAY"
ABresult
1X1
011
000
61 9 ▶ if (! defined $paths) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $paths) { } 9 0 9
62 9 ▶ $paths ||= [];
Condition50%1/2
$paths ||= []
l!l
09
MC/DC0%0/1
$paths ||= []
$paths
Truth table50%1/2
A
A= $paths
Aresult
11
00
63 } else {
64 0 $paths = [ $paths ];
65 }
66 9 $args->{include_path} = $paths;
67 }
68  
69 9 ▶ if (scalar @$paths < 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$paths < 1) 9 0 9
70 9 push @$paths, $c->path_to('root');
71 }
72 9 return $self->$next($args);
73 3 };
74  
75 6 ▶sub ACCEPT_CONTEXT {
Subroutine ACCEPT_CONTEXT: called CC=1 SCAR=0.0
Pod documented
76 6 my ($self, $c) = @_;
77 6 $self->context($c);
78 6 return $self;
79 }
80  
81 1 ▶sub _build_template_suffix {
Subroutine _build_template_suffix: called CC=1 SCAR=0.0
82 1 return '.mt';
83 }
84  
85 3 ▶sub _build_stash_key {
Subroutine _build_stash_key: called CC=1 SCAR=0.0
86 3 my $self = shift;
87 3 return '__' . ref($self) . '_template';
88 }
89  
90 3 ▶sub _build_template_args {
Subroutine _build_template_args: called CC=1 SCAR=0.0
91 3 return {};
92 }
93  
94 3 ▶sub _build_template {
Subroutine _build_template: called CC=1 SCAR=0.0
95 3 my ($self) = @_;
96  
97 3 return Text::MicroTemplate::Extended->new(
98 extension => $self->template_suffix,
99 include_path => $self->include_path,
100 template_args => $self->template_args,
101 );
102 }
103  
104 6 ▶sub render {
Subroutine render: called CC=1 SCAR=0.0
Pod undocumented
105 6 my ($self, $template, $args) = @_;
106 6 my $mt = $self->template;
107 6 return $mt->render_file($template, $self->context, $args);
108 }
109  
110 6 ▶sub process {
Subroutine process: called CC=4 SCAR=14.4
Pod documented
111 6 my ($self) = @_;
112  
113 6 my $c = $self->context;
114 6 my $template = $self->get_template_file( $c );
115 6 ▶ $c->log->debug( sprintf("[%s] rendering template %s", blessed $self, $template ) ) if $c->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $c->debug 6 0 6
116  
117 6 $c->res->content_type( sprintf("%s; charset=%s", $self->content_type, $self->charset ) );
118 6 $self->template->template_args( $c->stash );
119 6 my $body = $self->render( $template, $c->stash );
120 6 ▶ if (blessed $body && $body->can('as_string')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($body) and $body->can("as_string")) 6 0 6
Condition33%1/3
builtin::blessed($body) and $body->can("as_string")
!ll&&!rl&&r
006
MC/DC0%0/2
builtin::blessed($body) and $body->can("as_string")
builtin::blessed($body) $body->can("as_string")
Truth table33%1/3
A and B
A= builtin::blessed($body)B= $body->can("as_string")
ABresult
0X0
100
111
121 6 $body = $body->as_string;
122 }
123 6 $c->res->body( $body );
124 }
125  
126 6 ▶sub get_template_file {
Subroutine get_template_file: called CC=3 SCAR=12.3
Pod undocumented
127 6 my ($self, $c) = @_;
128
129 # hopefully they're using the new $c->view->template
130 6 my $template = $c->stash->{$self->stash_key()};
131
132 # if that's empty, get the template the old way, $c->stash->{template}
133 6 ▶ $template ||= $c->stash->{template};
Condition33%1/3
$template ||= $c->stash->{"template"}
l!l&&r!l&&!r
006
MC/DC0%0/2
$template ||= $c->stash->{"template"}
$template $c->stash->{"template"}
Truth table33%1/3
A ||= B
A= $templateB= $c->stash->{"template"}
ABresult
1X1
011
000
134
135 # if those aren't set, try $c->action
136 6 ▶ $template ||= $c->action;
Condition33%1/3
$template ||= $c->action
l!l&&r!l&&!r
060
MC/DC0%0/2
$template ||= $c->action
$template $c->action
Truth table33%1/3
A ||= B
A= $templateB= $c->action
ABresult
1X1
011
000
137
138 6 return $template;
139 }
140  
141 3 __PACKAGE__->meta->make_immutable();
142  
143 3 1;
144  
145 __END__
↑ Summary