blib/lib/Catalyst/View/HTML/Mason.pm

filter: Statementstmt 95.8% 137 / 143 Branchbran 69.0% 29 / 42 Conditioncond 35.7% 5 / 14 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 93.9% 31 / 33 Podpod 66.6% 2 / 3 totaltotal 83.6% 204 / 244 CC 28 SCAR 14.1
CC
28
Cov
86%
_fetch_comp
13.4
_build_interp
5.2
_get_component
4.2
CRAP 4.1
↑ Summary blib/lib/Catalyst/Helper/View/HTML/Mason.pm »
1 package Catalyst::View::HTML::Mason;
2 5 our $AUTHORITY = 'cpan:FLORA';
3 # ABSTRACT: HTML::Mason rendering for Catalyst
4 5 $Catalyst::View::HTML::Mason::VERSION = '0.19';
5 5 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use MooseX::Types::Moose qw/ArrayRef HashRef ClassName Str Bool Object CodeRef/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use MooseX::Types::Structured qw/Tuple/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use Encode::Encoding;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use Data::Visitor::Callback;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use Module::Runtime;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 5 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 5 extends 'Catalyst::View';
16  
17  
18 5 has interp => (
19 is => 'ro',
20 isa => Object,
21 lazy => 1,
22 builder => '_build_interp',
23 );
24  
25  
26 {
27 5 ▶ use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 5 my $tc = subtype as ClassName;
30 5 ▶ coerce $tc, from Str, via { Module::Runtime::require_module($_); $_ };
Subroutine __ANON__: called CC=1 SCAR=0.0
31  
32 5 has interp_class => (
33 is => 'ro',
34 isa => $tc,
35 coerce => 1,
36 builder => '_build_interp_class',
37 );
38 }
39  
40  
41 has interp_args => (
42 is => 'ro',
43 isa => HashRef,
44 0 ▶ default => sub { +{} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
45 5 );
46  
47  
48 5 has template_extension => (
49 is => 'ro',
50 isa => Str,
51 default => '',
52 );
53  
54  
55 5 has always_append_template_extension => (
56 is => 'ro',
57 isa => Bool,
58 default => 0,
59 );
60  
61  
62 {
63 5 my $tc = subtype as 'Encode::Encoding';
64 2 ▶ coerce $tc, from Str, via { Encode::find_encoding($_) };
Subroutine __ANON__: called CC=1 SCAR=0.0
65  
66 5 has encoding => (
67 is => 'ro',
68 isa => $tc,
69 coerce => 1,
70 predicate => 'has_encoding',
71 );
72 }
73  
74  
75 {
76 5 my $glob_spec = subtype as Tuple[Str,CodeRef];
77 18 ▶ coerce $glob_spec, from Str, via {
Subroutine __ANON__: called CC=1 SCAR=0.0
78 18 my ($type, $var) = split q//, $_, 2;
79 my $fn = {
80 9 ▶ '$' => sub { $_[0] },
Subroutine __ANON__: called CC=1 SCAR=0.0
81 3 ▶ '@' => sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
82 3 ▶ return unless defined $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 1 2 3
83 ref $_[0] eq 'ARRAY'
84 2 ▶ ? @{ $_[0] }
Branch: 2 branches
BranchTrueFalseTotal
ref $_[0] eq 'ARRAY' ? : 1 1 2
!ref($_[0]) ? : 1 0 1
85 : !ref $_[0]
86 ? $_[0]
87 : ();
88 },
89 3 ▶ '%' => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
90 3 ▶ return unless defined $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 1 2 3
91 ref $_[0] eq 'HASH'
92 2 ▶ ? %{ $_[0] }
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 1 1 2
93 : ();
94 },
95 18 }->{ $type };
96 18 ▶ [$_ => sub { $fn->( $_[1]->stash->{$var} ) }];
Subroutine __ANON__: called CC=1 SCAR=0.0
97 5 };
98  
99 5 my $tc = subtype as ArrayRef[$glob_spec];
100 11 ▶ coerce $tc, from ArrayRef, via { [map { $glob_spec->coerce($_) } @{ $_ } ]};
Subroutine __ANON__: called CC=1 SCAR=0.0
101 1 ▶ coerce $tc, from Str, via { [ $glob_spec->coerce( $_ ) ] };
Subroutine __ANON__: called CC=1 SCAR=0.0
102  
103 5 has globals => (
104 is => 'ro',
105 isa => $tc,
106 coerce => 1,
107 builder => '_build_globals',
108 );
109 }
110  
111 13 ▶sub BUILD {
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
112 13 my ($self) = @_;
113 13 $self->interp;
114 }
115  
116 1 ▶sub _build_globals { [] }
Subroutine _build_globals: called CC=1 SCAR=0.0
117  
118 13 ▶sub _build_interp_class { 'HTML::Mason::Interp' }
Subroutine _build_interp_class: called CC=1 SCAR=0.0
119  
120 13 ▶sub _build_interp {
Subroutine _build_interp: called CC=5 SCAR=16.5
121 13 my ($self) = @_;
122  
123 13 my %args = %{ $self->interp_args };
124 13 ▶ if ($self->has_encoding) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_encoding) 2 11 13
125 2 my $old_func = delete $args{postprocess_text};
126 4 ▶ $args{postprocess_text} = sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
127 4 ▶ $old_func->($_[0]) if $old_func;
Branch: 1 branch
BranchTrueFalseTotal
if $old_func 0 4 4
128 4 ${ $_[0] } = $self->encoding->decode(${ $_[0] });
129 2 };
130 }
131  
132 13 ▶ $args{allow_globals} ||= [];
Condition50%1/2
$args{"allow_globals"} ||= []
l!l
013
MC/DC0%0/1
$args{"allow_globals"} ||= []
$args{"allow_globals"}
Truth table50%1/2
A
A= $args{"allow_globals"}
Aresult
11
00
133 13 unshift @{ $args{allow_globals}}, map { $_->[0] } @{ $self->globals };
134  
135 13 ▶ $args{in_package} ||= sprintf '%s::Commands', do {
Condition33%1/3
$args{"in_package"} ||= sprintf("%s::Commands", do { if (my $meta = Class::MOP::class_of($self)) { $meta->name; } else { ref $self; } })
l!l&&r!l&&!r
0130
MC/DC0%0/2
$args{"in_package"} ||= sprintf("%s::Commands", do { if (my $meta = Class::MOP::class_of($self)) { $meta->name; } else { ref $self; } })
$args{"in_package"} sprintf("%s::Commands", do { if (my $meta = Class::MOP::class_of($self)) { $meta->name; } else { ref $self; } })
Truth table33%1/3
A ||= B
A= $args{"in_package"}B= sprintf("%s::Commands", do { if (my $meta = Class::MOP::class_of($self)) { $meta->name; } else { ref $self; } })
ABresult
1X1
011
000
136 13 ▶ if (my $meta = Class::MOP::class_of($self)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $meta = Class::MOP::class_of($self)) { } 13 0 13
137 13 $meta->name;
138 } else {
139 0 ref $self;
140 }
141 } ;
142  
143 my $v = Data::Visitor::Callback->new(
144 10 ▶ 'Path::Class::Entity' => sub { blessed $_ ? $_->stringify : $_ },
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($_) ? : 10 0 10
145 13 );
146  
147 13 return $self->interp_class->new( $v->visit(%args) );
148 }
149  
150  
151 10 ▶sub render {
Subroutine render: called CC=3 SCAR=11.1
Pod documented
152 10 my ($self, $ctx, $comp, $args) = @_;
153 10 my $output = '';
154  
155 10 for (@{ $self->globals }) {
156 23 my ($decl, @values) = ($_->[0] => $_->[1]->($self, $ctx));
157 23 ▶ if (@values) {
Branch: 1 branch
BranchTrueFalseTotal
if (@values) { } 20 3 23
158 20 $self->interp->set_global($decl, @values);
159 } else {
160 # HTML::Mason::Interp->set_global would crash on empty lists
161 3 $self->_unset_interp_global($decl);
162 }
163 }
164  
165 10 ▶ try {
Subroutine __ANON__: called CC=2 SCAR=8.1
166 $self->interp->make_request(
167 comp => $self->_fetch_comp($comp),
168 0 ▶ args => [$args ? %{ $args } : %{ $ctx->stash }],
Branch: 1 branch
BranchTrueFalseTotal
$args ? : 0 10 10
169 out_method => \$output,
170 )->exec;
171 }
172 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
173 0 confess $_;
174 10 };
175  
176 10 return $output;
177 }
178  
179 10 ▶sub process {
Subroutine process: called CC=1 SCAR=0.0
Pod documented
180 10 my ($self, $ctx) = @_;
181  
182 10 my $comp = $self->_get_component($ctx);
183 10 my $output = $self->render($ctx, $comp);
184  
185 10 $ctx->response->body($output);
186 }
187  
188 10 ▶sub _fetch_comp {
Subroutine _fetch_comp: called CC=9 SCAR=26.0
189 10 my ($self, $comp) = @_;
190 10 my $method;
191  
192 10 ▶ $comp = $comp->stringify
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($comp) and $comp->isa("Path::Class") 0 10 10
Condition33%1/3
builtin::blessed($comp) and $comp->isa("Path::Class")
!ll&&!rl&&r
1000
MC/DC0%0/2
builtin::blessed($comp) and $comp->isa("Path::Class")
builtin::blessed($comp) $comp->isa("Path::Class")
Truth table33%1/3
A and B
A= builtin::blessed($comp)B= $comp->isa("Path::Class")
ABresult
0X0
100
111
193 if blessed $comp && $comp->isa( 'Path::Class' );
194  
195 10 ▶ return $comp
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($comp) 0 10 10
196 if blessed $comp;
197  
198 0 ▶ ($comp, $method) = @{ $comp }
Branch: 1 branch
BranchTrueFalseTotal
if ref $comp and ref $comp eq "ARRAY" 0 10 10
Condition33%1/3
ref $comp and ref $comp eq "ARRAY"
!ll&&!rl&&r
1000
MC/DC0%0/2
ref $comp and ref $comp eq "ARRAY"
ref $comp ref $comp eq "ARRAY"
Truth table33%1/3
A and B
A= ref $compB= ref $comp eq "ARRAY"
ABresult
0X0
100
111
199 if ref $comp && ref $comp eq 'ARRAY';
200  
201 10 ▶ $comp = "/$comp"
Branch: 1 branch
BranchTrueFalseTotal
unless $comp =~ m[^/] 10 0 10
202 unless $comp =~ m{^/};
203  
204 10 my $component = $self->interp->load($comp);
205 10 ▶ confess "Can't find component for path $comp"
Branch: 1 branch
BranchTrueFalseTotal
unless $component 0 10 10
206 unless $component;
207  
208 10 ▶ $component = $component->methods($method)
Branch: 1 branch
BranchTrueFalseTotal
if defined $method 0 10 10
209 if defined $method;
210  
211 10 return $component;
212 }
213  
214  
215 10 ▶sub _get_component {
Subroutine _get_component: called CC=4 SCAR=14.2
216 10 my ($self, $ctx) = @_;
217  
218 10 my $comp = $ctx->stash->{template};
219 10 my $extension = $self->template_extension;
220  
221 10 ▶ if (defined $comp) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $comp) 5 5 10
222 5 ▶ $comp .= $extension
Branch: 1 branch
BranchTrueFalseTotal
if not ref $comp and $self->always_append_template_extension 0 5 5
Condition33%1/3
not ref $comp and $self->always_append_template_extension
!ll&&!rl&&r
050
MC/DC0%0/2
not ref $comp and $self->always_append_template_extension
not ref $comp $self->always_append_template_extension
Truth table33%1/3
not(A) and B
A= not ref $compB= $self->always_append_template_extension
ABresult
0X0
100
111
223 if !ref $comp && $self->always_append_template_extension;
224  
225 5 return $comp;
226 }
227  
228 5 return $ctx->action->reverse . $extension;
229 }
230  
231 3 ▶sub _unset_interp_global {
Subroutine _unset_interp_global: called CC=3 SCAR=11.1
232 3 my ($self, $decl) = @_;
233 3 my ($prefix, $name) = split q//, $decl, 2;
234 3 my $package = $self->interp->compiler->in_package;
235 3 my $varname = sprintf "%s::%s", $package, $name;
236  
237 5 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
238 0 ▶ if ($prefix eq '$') { $$varname = undef }
Branch: 2 branches
BranchTrueFalseTotal
if ($prefix eq '$') { } 0 3 3
elsif ($prefix eq '@') { } 1 2 3
239 1 elsif ($prefix eq '@') { @$varname = () }
240 2 else { %$varname = () }
241 }
242  
243 5 __PACKAGE__->meta->make_immutable;
244  
245 5 1;
246  
247 __END__
↑ Summary blib/lib/Catalyst/Helper/View/HTML/Mason.pm »