Catalyst/View/Valiant.pm

filter: Statementstmt 12.3% 27 / 219 Branchbran 0.0% 0 / 130 Conditioncond 0.0% 0 / 64 MC/DCmcdc 0.0% 0 / 35 Subroutinesub 23.0% 9 / 39 Podpod n/a totaltotal 7.3% 36 / 487 CC 92 SCAR 45.6
CC
92
Cov
7%
path
380.0
import
90.0
__ANON__
90.0
CRAP 95.9
↑ Summary Valiant/Validator/Each.pm »
1 package Catalyst::View::Valiant;
2  
3 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Sub::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Valiant::HTML::SafeString ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Attribute::Handlers;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Module::Runtime;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Catalyst::View::Valiant::Form;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 extends 'Catalyst::View::BasePerRequest';
12  
13 ## Shared Form Object
14  
15 my $form;
16  
17 0 ▶sub form_args { return () }
Subroutine form_args: not called CC=1 SCAR=6.9
18  
19 sub _install_form {
20 0 ▶ my $class = shift;
Subroutine _install_form: not called CC=1 SCAR=6.9
21 0 my $target = shift;
22 0 my $form_class = shift;
23 0 my $view = Module::Runtime::use_module('Valiant::HTML::Util::View')->new; # Placeholder
24 0 $form = Catalyst::View::Valiant::Form->new(view=>$view, $class->form_args);
25 }
26  
27 ## Code Attributes
28  
29 sub Renders :ATTR(CODE) {
30 0 ▶ my ($package, $symbol, $referent, $attr, $data) = @_;
Subroutine Renders: not called CC=2 SCAR=17.9
31 0 my $name = *{$symbol}{NAME};
32 0 ▶ unless($package->can("__attr_${name}")) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($package->can("__attr_$name")) 0 0 0
33 my $wrapper = sub {
34 0 ▶ my ($self, @args) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
35 0 ▶ carp "View method called without correct self" unless $self->isa($package);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->isa($package) 0 0 0
36 0 local $form->{view} = $self; # Evil Hack lol
37 0 local $form->{context} = $self->ctx;
38 0 local $form->{controller} = $self->ctx->controller;
39 0 return $referent->(@_);
40 0 };
41 0 Moo::_Utils::_install_tracked($package, "__attr_${name}", $wrapper);
42 Moo::_Utils::_install_tracked($package, $name, sub {
43 0 ▶ return $package->can("__attr_${name}")->(@_);
Subroutine __ANON__: not called CC=1 SCAR=6.9
44 0 });
45 }
46 1 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
47  
48 sub import {
49 0 ▶ my $class = shift;
Subroutine import: not called CC=9 SCAR=45.0
50 0 my $target = caller;
51  
52 0 my (@tags, @views, @utils, $which) = ();
53 0 ▶ while(@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
54 0 my $next = shift;
55 0 ▶ if($next eq '-tags') {
Branch: 3 branches
BranchTrueFalseTotal
if ($next eq '-tags') { } 0 0 0
elsif ($next eq '-views') { } 0 0 0
elsif ($next eq '-util' or $next eq '-utils') { } 0 0 0
56 0 $which = 'tags';
57 0 next;
58 } elsif($next eq '-views') {
59 0 $which = 'views';
60 0 next;
61 ▶ } elsif(($next eq '-util') || ($next eq '-utils')) {
Condition0%0/3
$next eq "-util" or $next eq "-utils"
l!l&&r!l&&!r
000
MC/DC0%0/2
$next eq "-util" or $next eq "-utils"
$next eq "-util" $next eq "-utils"
Truth table0%0/3
A or B
A= $next eq "-util"B= $next eq "-utils"
ABresult
1X1
011
000
62 0 $which = 'util';
63 0 next;
64 }
65  
66 0 ▶ if($which eq 'tags') {
Branch: 3 branches
BranchTrueFalseTotal
if ($which eq 'tags') { } 0 0 0
elsif ($which eq 'views') { } 0 0 0
elsif ($which eq 'util') { } 0 0 0
67 0 push @tags, $next;
68 } elsif($which eq 'views') {
69 0 my $key = $next;
70 0 $next =~s/::/_/g;
71 0 $next =~s/(?<=[a-z])(?=[A-Z])/_/g;
72 0 push @views, lc($next) => $key;
73 } elsif($which eq 'util') {
74 0 push @utils, $next;
75 }
76 }
77  
78 0 Moo->_set_superclasses($target, $class);
79 0 Moo->_maybe_reset_handlemoose($target);
80  
81 0 $class->_install_form($target);
82 0 $class->_install_tags($target, @tags);
83 0 $class->_install_views($target, @views);
84 0 $class->_install_utils($target, @utils);
85 }
86  
87 0 ▶sub form { $form }
Subroutine form: not called CC=1 SCAR=6.9
88 0 ▶sub tags { $form->tags }
Subroutine tags: not called CC=1 SCAR=6.9
89  
90 sub _install_utils {
91 0 ▶ my $class = shift;
Subroutine _install_utils: not called CC=9 SCAR=43.6
92 0 my $target = shift;
93  
94 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
95 0 foreach my $util (@_) {
96 0 ▶ if($util eq '$sf') {
Branch: 4 branches
BranchTrueFalseTotal
if ($util eq '$sf') { } 0 0 0
elsif ($util eq 'content') { } 0 0 0
elsif ($util eq 'content_for' or $util eq 'content_append' or $util eq 'content_replace' or $util eq 'content_around') { } 0 0 0
elsif ($util eq 'path') { } 0 0 0
97 0 ▶ my $sf = sub { $form->sf(@_) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
98 0 *{"${target}::sf"} = \$sf;
99 } elsif($util eq 'content') {
100 0 Moo::_Utils::_install_tracked($target, "__content", \&{"Catalyst::View::BasePerRequest::content"});
101 my $content_sub = sub {
102 0 ▶ if(Scalar::Util::blessed($_[0])) {
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($_[0])) { } 0 0 0
103 0 return $target->can('__content')->(shift, shift), @_;
104 } else {
105 0 return $target->can('__content')->($form->view, shift), @_;
106 }
107 0 };
108 0 Moo::_Utils::_install_tracked($target, 'content', $content_sub);
109 ▶ } elsif( ($util eq 'content_for') || ($util eq 'content_append') || ($util eq 'content_replace') || ($util eq 'content_around') ) {
Condition0%0/9
$util eq "content_for" or $util eq "content_append" or $util eq "content_replace" or $util eq "content_around"
l!l&&r!l&&!r
000
$util eq "content_for" or $util eq "content_append" or $util eq "content_replace"
l!l&&r!l&&!r
000
$util eq "content_for" or $util eq "content_append"
l!l&&r!l&&!r
000
MC/DC0%0/4
$util eq "content_for" or $util eq "content_append" or $util eq "content_replace" or $util eq "content_around"
$util eq "content_for" $util eq "content_append" $util eq "content_replace" $util eq "content_around"
Truth table0%0/5
A or B or C or D
A= $util eq "content_for"B= $util eq "content_append"C= $util eq "content_replace"D= $util eq "content_around"
ABCDresult
1XXX1
01XX1
001X1
00011
00000
110 0 Moo::_Utils::_install_tracked($target, "__${util}", \&{"Catalyst::View::BasePerRequest::${util}"});
111 my $sub = sub {
112 0 ▶ if(Scalar::Util::blessed($_[0])) {
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($_[0])) { } 0 0 0
113 0 return $target->can("__${util}")->(shift, shift, shift), @_;
114 } else {
115 0 return $target->can("__${util}")->($form->view, shift, shift), @_;
116 }
117 0 };
118 0 Moo::_Utils::_install_tracked($target, $util, $sub);
119 } elsif($util eq 'path') {
120 0 Moo::_Utils::_install_tracked($target, "__path", $target->can('path'));
121 my $sub = sub {
122 0 ▶ if(Scalar::Util::blessed($_[0]) && $_[0]->isa('Catalyst::View::Valiant')) {
Subroutine __ANON__: not called CC=3 SCAR=24.8
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($_[0]) and $_[0]->isa('Catalyst::View::Valiant')) { } 0 0 0
Condition0%0/3
builtin::blessed($_[0]) and $_[0]->isa("Catalyst::View::Valiant")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($_[0]) and $_[0]->isa("Catalyst::View::Valiant")
builtin::blessed($_[0]) $_[0]->isa("Catalyst::View::Valiant")
Truth table0%0/3
A and B
A= builtin::blessed($_[0])B= $_[0]->isa("Catalyst::View::Valiant")
ABresult
0X0
100
111
123 0 return $target->can("__path")->(@_);
124 } else {
125 0 return $target->can("__path")->($form->view, @_);
126 }
127 0 };
128 0 Moo::_Utils::_install_tracked($target, 'path', $sub);
129 }
130  
131 }
132 }
133  
134 sub _install_tags {
135 0 ▶ my $class = shift;
Subroutine _install_tags: not called CC=6 SCAR=37.4
136 0 my $target = shift;
137 0 foreach my $tag (@_) {
138 0 my $method;
139 0 ▶ if($form->is_content_tag($tag)) {
Branch: 4 branches
BranchTrueFalseTotal
if ($form->is_content_tag($tag)) { } 0 0 0
elsif ($form->is_void_tag($tag)) { } 0 0 0
elsif ($tag eq 'trow') { } 0 0 0
elsif ($form->can($tag)) { } 0 0 0
140 #if($target->can($tag)) {
141 # $method = $target->can($tag);
142 # use Devel::Dwarn;
143 # Dwarn [1, $method] if $tag eq 'blockquote';
144 #} else {
145 $method = Sub::Util::set_subname "${target}::${tag}" => sub {
146 0 ▶ my ($args, $content) = (+{}, '');
Subroutine __ANON__: not called CC=9 SCAR=45.0
147 0 ▶ $args = shift if ref $_[0] eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] eq "HASH" 0 0 0
148 0 ▶ if(defined($_[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0]) 0 0 0
149 0 ▶ if(Scalar::Util::blessed($_[0]) && $_[0]->isa($class)) {
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::blessed($_[0]) and $_[0]->isa($class)) { } 0 0 0
elsif ((ref $_[0] || '') eq 'ARRAY') { } 0 0 0
Condition0%0/3
builtin::blessed($_[0]) and $_[0]->isa($class)
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($_[0]) and $_[0]->isa($class)
builtin::blessed($_[0]) $_[0]->isa($class)
Truth table0%0/3
A and B
A= builtin::blessed($_[0])B= $_[0]->isa($class)
ABresult
0X0
100
111
150 0 $content = shift->get_rendered;
151 ▶ } elsif((ref($_[0])||'') eq 'ARRAY') {
Condition0%0/2
ref $_[0] || ''
l!l
00
MC/DC0%0/1
ref $_[0] || ''
ref $_[0]
Truth table0%0/2
A
A= ref $_[0]
Aresult
11
00
152 0 my $inner = shift;
153 my @content = map {
154 0 ▶ (Scalar::Util::blessed($_) && $_->isa($class)) ? $_->get_rendered : $_;
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($_) && $_->isa($class) ? : 0 0 0
Condition0%0/3
builtin::blessed($_) and $_->isa($class)
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($_) and $_->isa($class)
builtin::blessed($_) $_->isa($class)
Truth table0%0/3
A and B
A= builtin::blessed($_)B= $_->isa($class)
ABresult
0X0
100
111
155 0 } @{$inner};
156 0 $content = $class->safe_concat(@content);
157 } else {
158 0 $content = shift;
159 }
160 }
161 0 return $form->tags->$tag($args, $content), @_;
162 0 };
163 # use Devel::Dwarn;
164 # Dwarn [2, $method] if $tag eq 'blockquote';
165 #}
166 } elsif($form->is_void_tag($tag)) {
167 #if($target->can($tag) && $tag ne 'meta') { # meta is a special case
168 # $method = $target->can($tag);
169 #} else {
170 $method = Sub::Util::set_subname "${target}::${tag}" => sub {
171 0 ▶ my $args = +{};
Subroutine __ANON__: not called CC=2 SCAR=17.9
172 0 ▶ $args = shift if ref $_[0] eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] eq "HASH" 0 0 0
173 0 return $form->tags->$tag($args), @_;
174 0 };
175 #}
176 } elsif($tag eq 'trow') {
177 #if($target->can('tr') && $tag ne 'meta') { # meta is a special case
178 # $method = $target->can('tr');
179 #} else {
180 $method = Sub::Util::set_subname "${target}::tr" => sub {
181 0 ▶ my ($args, $content) = (+{}, '');
Subroutine __ANON__: not called CC=9 SCAR=45.0
182 0 ▶ $args = shift if ref $_[0] eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] eq "HASH" 0 0 0
183 0 ▶ if(defined($_[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0]) 0 0 0
184 0 ▶ if(Scalar::Util::blessed($_[0]) && $_[0]->isa($class)) {
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::blessed($_[0]) and $_[0]->isa($class)) { } 0 0 0
elsif ((ref $_[0] || '') eq 'ARRAY') { } 0 0 0
Condition0%0/3
builtin::blessed($_[0]) and $_[0]->isa($class)
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($_[0]) and $_[0]->isa($class)
builtin::blessed($_[0]) $_[0]->isa($class)
Truth table0%0/3
A and B
A= builtin::blessed($_[0])B= $_[0]->isa($class)
ABresult
0X0
100
111
185 0 $content = shift->get_rendered;
186 ▶ } elsif((ref($_[0])||'') eq 'ARRAY') {
Condition0%0/2
ref $_[0] || ''
l!l
00
MC/DC0%0/1
ref $_[0] || ''
ref $_[0]
Truth table0%0/2
A
A= ref $_[0]
Aresult
11
00
187 0 my $inner = shift;
188 my @content = map {
189 0 ▶ (Scalar::Util::blessed($_) && $_->isa($class)) ? $_->get_rendered : $_;
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($_) && $_->isa($class) ? : 0 0 0
Condition0%0/3
builtin::blessed($_) and $_->isa($class)
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($_) and $_->isa($class)
builtin::blessed($_) $_->isa($class)
Truth table0%0/3
A and B
A= builtin::blessed($_)B= $_->isa($class)
ABresult
0X0
100
111
190 0 } @{$inner};
191 0 $content = $class->safe_concat(@content);
192 } else {
193 0 $content = shift;
194 }
195 }
196 0 return $form->content_tag('tr', $args, $content), @_;
197 0 };
198 #}
199 } elsif($form->can($tag)) {
200 #if($target->can($tag)) {
201 # $method = $target->can($tag);
202 #} else {
203 $method = Sub::Util::set_subname "${target}::${tag}" => sub {
204 ## return $form->safe_concat($form->$tag(@_));
205 ## Will ponder this, it seems to be a performance hit
206 0 ▶ my @args = ();
Subroutine __ANON__: not called CC=8 SCAR=42.8
207 0 ▶ while(@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
208 last if
209 0 ▶ !defined($_[0])
Branch: 1 branch
BranchTrueFalseTotal
if not defined $_[0] or (builtin::blessed($_[0]) || "") eq "Valiant::HTML::SafeString" 0 0 0
Condition0%0/5
not defined $_[0] or (builtin::blessed($_[0]) || "") eq "Valiant::HTML::SafeString"
l!l&&r!l&&!r
000
builtin::blessed($_[0]) || ''
l!l
00
MC/DC0%0/2
not defined $_[0] or (builtin::blessed($_[0]) || "") eq "Valiant::HTML::SafeString"
not defined $_[0] builtin::blessed($_[0])
Truth table0%0/3
not(A) or B
A= not defined $_[0]B= builtin::blessed($_[0])
ABresult
1X1
011
000
210 || (Scalar::Util::blessed($_[0])||'') eq 'Valiant::HTML::SafeString';
211 0 push @args, shift;
212 0 ▶ if(ref $_[0] eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq "ARRAY") 0 0 0
213 0 my $inner = shift;
214 my @content = map {
215 0 ▶ (Scalar::Util::blessed($_) && $_->isa($class)) ? $_->get_rendered : $_;
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($_) && $_->isa($class) ? : 0 0 0
Condition0%0/3
builtin::blessed($_) and $_->isa($class)
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($_) and $_->isa($class)
builtin::blessed($_) $_->isa($class)
Truth table0%0/3
A and B
A= builtin::blessed($_)B= $_->isa($class)
ABresult
0X0
100
111
216 0 } @{$inner};
217 0 push @args, $class->safe_concat(@content);
218 }
219 }
220 0 return $form->$tag(@args), @_;
221 0 };
222 #}
223 } else {
224 0 die "No such tag '$tag' for view";
225 }
226  
227 # I do this dance so that the exported methods can be called as both a function
228 # and as a method on the target instance.
229  
230 0 Moo::_Utils::_install_tracked($target, $tag, $method);
231 0 Moo::_Utils::_install_tracked($target, "_tag_${tag}", \&{"${target}::${tag}"});
232 Moo::_Utils::_install_tracked($target, $tag, sub {
233 0 ▶ my $view = shift if Scalar::Util::blessed($_[0]) && $_[0]->isa($target);
Subroutine __ANON__: not called CC=6 SCAR=37.4
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($_[0]) and $_[0]->isa($target) 0 0 0
Condition0%0/3
builtin::blessed($_[0]) and $_[0]->isa($target)
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($_[0]) and $_[0]->isa($target)
builtin::blessed($_[0]) $_[0]->isa($target)
Truth table0%0/3
A and B
A= builtin::blessed($_[0])B= $_[0]->isa($target)
ABresult
0X0
100
111
234 0 ▶ local $form->{view} = $view if $view;
Branch: 1 branch
BranchTrueFalseTotal
if $view 0 0 0
235 0 ▶ local $form->{context} = $view->ctx if $view;
Branch: 1 branch
BranchTrueFalseTotal
if $view 0 0 0
236 0 ▶ local $form->{controller} = $view->ctx->controller if $view;
Branch: 1 branch
BranchTrueFalseTotal
if $view 0 0 0
237 0 return $target->can("_tag_${tag}")->(@_);
238 0 });
239 }
240 }
241  
242 sub _install_views {
243 0 ▶ my $class = shift;
Subroutine _install_views: not called CC=2 SCAR=17.9
244 0 my $target = shift;
245 0 my %view_info = @_;
246  
247 0 foreach my $name (keys %view_info) {
248 my $method = Sub::Util::set_subname "${target}::${name}" => sub {
249 0 ▶ my @args = ();
Subroutine __ANON__: not called CC=8 SCAR=42.8
250 0 ▶ while(@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
251 last if
252 0 ▶ !defined($_[0])
Branch: 1 branch
BranchTrueFalseTotal
if not defined $_[0] or (builtin::blessed($_[0]) || "") eq "Valiant::HTML::SafeString" or builtin::blessed($_[0]) and $_[0]->isa($class) 0 0 0
Condition0%0/11
not defined $_[0] or (builtin::blessed($_[0]) || "") eq "Valiant::HTML::SafeString" or builtin::blessed($_[0]) and $_[0]->isa($class)
l!l&&r!l&&!r
000
not defined $_[0] or (builtin::blessed($_[0]) || "") eq "Valiant::HTML::SafeString"
l!l&&r!l&&!r
000
builtin::blessed($_[0]) || ''
l!l
00
builtin::blessed($_[0]) and $_[0]->isa($class)
!ll&&!rl&&r
000
MC/DC0%0/4
not defined $_[0] or (builtin::blessed($_[0]) || "") eq "Valiant::HTML::SafeString" or builtin::blessed($_[0]) and $_[0]->isa($class)
not defined $_[0] builtin::blessed($_[0]) builtin::blessed($_[0]) $_[0]->isa($class)
Truth table0%0/5
not(A) or B or C and D
A= not defined $_[0]B= builtin::blessed($_[0])C= builtin::blessed($_[0])D= $_[0]->isa($class)
ABCDresult
1XXX1
01XX1
00111
000X0
00100
253 || ((Scalar::Util::blessed($_[0])||'') eq 'Valiant::HTML::SafeString')
254 || (Scalar::Util::blessed($_[0]) && $_[0]->isa($class));
255 0 push @args, shift;
256 }
257 0 ▶ return $form->view->ctx->view($view_info{$name}, @args), @_ if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
258 0 return $form->view->ctx->view($view_info{$name}, @args);
259 0 };
260 0 Moo::_Utils::_install_tracked($target, $name, $method);
261 0 Moo::_Utils::_install_tracked($target, "_view_${name}", \&{"${target}::${name}"});
262 Moo::_Utils::_install_tracked($target, $name, sub {
263 0 ▶ my $view = shift if Scalar::Util::blessed($_[0]) && $_[0]->isa($target);
Subroutine __ANON__: not called CC=6 SCAR=37.4
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($_[0]) and $_[0]->isa($target) 0 0 0
Condition0%0/3
builtin::blessed($_[0]) and $_[0]->isa($target)
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($_[0]) and $_[0]->isa($target)
builtin::blessed($_[0]) $_[0]->isa($target)
Truth table0%0/3
A and B
A= builtin::blessed($_[0])B= $_[0]->isa($target)
ABresult
0X0
100
111
264 0 ▶ local $form->{view} = $view if $view;
Branch: 1 branch
BranchTrueFalseTotal
if $view 0 0 0
265 0 ▶ local $form->{context} = $view->ctx if $view;
Branch: 1 branch
BranchTrueFalseTotal
if $view 0 0 0
266 0 ▶ local $form->{controller} = $view->ctx->controller if $view;
Branch: 1 branch
BranchTrueFalseTotal
if $view 0 0 0
267 0 return $target->can("_view_${name}")->(@_);
268 0 });
269 }
270 }
271  
272 0 ▶sub safe { shift; return Valiant::HTML::SafeString::safe(@_) }
Subroutine safe: not called CC=1 SCAR=6.9
273 0 ▶sub raw { shift; return Valiant::HTML::SafeString::raw(@_) }
Subroutine raw: not called CC=1 SCAR=6.9
274 0 ▶sub safe_concat { shift; return Valiant::HTML::SafeString::safe_concat(@_) }
Subroutine safe_concat: not called CC=1 SCAR=6.9
275 0 ▶sub escape_html { shift; return Valiant::HTML::SafeString::escape_html(@_) }
Subroutine escape_html: not called CC=1 SCAR=6.9
276  
277 sub read_attribute_for_html {
278 0 ▶ my ($self, $attribute) = @_;
Subroutine read_attribute_for_html: not called CC=3 SCAR=24.8
279 0 ▶ return unless defined $attribute;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $attribute 0 0 0
280 0 ▶ return my $value = $self->$attribute if $self->can($attribute);
Branch: 1 branch
BranchTrueFalseTotal
if $self->can($attribute) 0 0 0
281 0 die "No such attribute '$attribute' for view";
282 }
283  
284 sub attribute_exists_for_html {
285 0 ▶ my ($self, $attribute) = @_;
Subroutine attribute_exists_for_html: not called CC=3 SCAR=24.8
286 0 ▶ return unless defined $attribute;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $attribute 0 0 0
287 0 ▶ return 1 if $self->can($attribute);
Branch: 1 branch
BranchTrueFalseTotal
if $self->can($attribute) 0 0 0
288 0 return;
289 }
290  
291 sub flatten_rendered {
292 0 ▶ my ($self, @rendered) = @_;
Subroutine flatten_rendered: not called CC=1 SCAR=6.9
293 0 return $self->safe_concat(@rendered);
294 }
295  
296 sub path {
297 0 ▶ my $self = shift;
Subroutine path: not called CC=19 SCAR=59.4
298 0 my $c = $self->ctx;
299 0 my $action_proto = shift;
300 0 my @args = @_;
301  
302 # already is an $action
303 0 ▶ if(Scalar::Util::blessed($action_proto) && $action_proto->isa('Catalyst::Action')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($action_proto) and $action_proto->isa("Catalyst::Action")) 0 0 0
Condition0%0/3
builtin::blessed($action_proto) and $action_proto->isa("Catalyst::Action")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($action_proto) and $action_proto->isa("Catalyst::Action")
builtin::blessed($action_proto) $action_proto->isa("Catalyst::Action")
Truth table0%0/3
A and B
A= builtin::blessed($action_proto)B= $action_proto->isa("Catalyst::Action")
ABresult
0X0
100
111
304 0 ▶ die "We can't create a URI from '$action_proto' with the given arguments"
Branch: 1 branch
BranchTrueFalseTotal
unless my $uri = $c->uri_for($action_proto, @args) 0 0 0
305 unless my $uri = $c->uri_for($action_proto, @args);
306 0 return $uri;
307 }
308  
309 0 ▶ return $action_proto if Scalar::Util::blessed($action_proto) && $action_proto->isa('URI'); # common error
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($action_proto) and $action_proto->isa("URI") 0 0 0
Condition0%0/3
builtin::blessed($action_proto) and $action_proto->isa("URI")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($action_proto) and $action_proto->isa("URI")
builtin::blessed($action_proto) $action_proto->isa("URI")
Truth table0%0/3
A and B
A= builtin::blessed($action_proto)B= $action_proto->isa("URI")
ABresult
0X0
100
111
310
311 # Hard error if the spec looks wrong...
312 0 ▶ die "$action_proto is not a string" unless ref \$action_proto eq 'SCALAR';
Branch: 1 branch
BranchTrueFalseTotal
unless ref \$action_proto eq "SCALAR" 0 0 0
313
314 0 my $action;
315 0 ▶ if($action_proto =~/^\/?\*/) {
Branch: 4 branches
BranchTrueFalseTotal
if ($action_proto =~ m[^/?\*]) { } 0 0 0
elsif ($action_proto =~ /^(.*)\:(.+)$/) { } 0 0 0
elsif ($action_proto =~ m[/]) { } 0 0 0
elsif ($action = $c->controller->action_for($action_proto)) { } 0 0 0
316 0 ▶ carp "$action_proto is not a named action"
Branch: 1 branch
BranchTrueFalseTotal
unless $action = $c->dispatcher->get_action_by_path($action_proto) 0 0 0
317 unless $action = $c->dispatcher->get_action_by_path($action_proto);
318 } elsif($action_proto=~m/^(.*)\:(.+)$/) {
319 0 ▶ carp "$1 is not a controller"
Branch: 1 branch
BranchTrueFalseTotal
unless my $controller = $c->controller($1 || "") 0 0 0
Condition0%0/2
$1 || ''
l!l
00
MC/DC0%0/1
$1 || ''
$1
Truth table0%0/2
A
A= $1
Aresult
11
00
320 unless my $controller = $c->controller($1||'');
321 0 ▶ carp "$2 is not an action for controller ${\$controller->component_name}"
Branch: 1 branch
BranchTrueFalseTotal
unless $action = $controller->action_for($2) 0 0 0
322 unless $action = $controller->action_for($2);
323 } elsif($action_proto =~/\//) {
324 0 ▶ my $path = $action_proto=~m/^\// ? $action_proto : $c->controller->action_for($action_proto)->private_path;
Branch: 1 branch
BranchTrueFalseTotal
$action_proto =~ m[^/] ? : 0 0 0
325 0 ▶ carp "$action_proto is not a full or relative private action path" unless $path;
Branch: 1 branch
BranchTrueFalseTotal
unless $path 0 0 0
326 0 ▶ carp "$path is not a private path" unless $action = $c->dispatcher->get_action_by_path($path);
Branch: 1 branch
BranchTrueFalseTotal
unless $action = $c->dispatcher->get_action_by_path($path) 0 0 0
327 } elsif($action = $c->controller->action_for($action_proto)) {
328 # Noop
329 } else {
330 # Fallback to static
331 0 $action = $action_proto;
332 }
333  
334 0 ▶ carp "We can't create a URI from $action with the given arguments: @{[ join ', ', @args ]}]}"
Branch: 1 branch
BranchTrueFalseTotal
unless my $uri = $c->uri_for($action, @args) 0 0 0
335 unless my $uri = $c->uri_for($action, @args);
336  
337 0 return $uri
338 }
339  
340 around 'get_rendered' => sub {
341 my ($orig, $self, @args) = @_;
342 $self->ctx->stash->{__view_for_code} = $self->form->view if $self->has_code;
343 local $form->{view} = $self; # Evil Hack lol
344 local $form->{context} = $self->ctx;
345 local $form->{controller} = $self->ctx->controller;
346 return $self->$orig(@args);
347 };
348  
349 around 'execute_code_callback' => sub {
350 my ($orig, $self, @args) = @_;
351 my $old_view = delete $self->ctx->stash->{__view_for_code};
352 local $form->{view} = $old_view; # Evil Hack lol
353 local $form->{context} = $old_view->ctx;
354 local $form->{controller} = $old_view->ctx->controller;
355 return $self->$orig(@args);
356 };
357  
358 __PACKAGE__->config(content_type=>'text/html');
359  
360 =head1 NAME
361  
362 Catalyst::View::Valiant - Per Request, strongly typed Views in code
363  
364 =head1 SYNOPSIS
365  
366 package Example::View::HTML::Home;
367  
368 use Moo;
369 use Catalyst::View::Valiant
370 -tags => qw(div blockquote form_for fieldset),
371 -utils => qw($sf),
372 -views => 'HTML::Layout', 'HTML::Navbar';
373  
374 has info => (is=>'rw', predicate=>'has_info');
375 has person => (is=>'ro', required=>1);
376  
377 sub render($self, $c) {
378 html_layout page_title => 'Sign In', sub($layout) {
379 html_navbar active_link=>'/',
380 blockquote +{ if=>$self->has_info,
381 class=>"alert alert-primary",
382 role=>"alert" }, $self->info,
383 div $self->person->$sf('Welcome {:first_name} {:last_name} to your Example Homepage');
384 div {if=>$self->person->profile_incomplete}, [
385 blockquote {class=>"alert alert-primary", role=>"alert"}, 'Please complete your profile',
386 form_for $self->person, sub($self, $fb, $person) {
387 fieldset [
388 $fb->legend,
389 div +{ class=>'form-group' },
390 $fb->model_errors(+{show_message_on_field_errors=>'Please fix validation errors'}),
391 div +{ class=>'form-group' }, [
392 $fb->label('username'),
393 $fb->input('username'),
394 $fb->errors_for('username'),
395 ],
396 div +{ class=>'form-group' }, [
397 $fb->label('password'),
398 $fb->password('password'),
399 $fb->errors_for('password'),
400 ],
401 div +{ class=>'form-group' }, [
402 $fb->label('password_confirmation'),
403 $fb->password('password_confirmation'),
404 $fb->errors_for('password_confirmation'),
405 ],
406 ],
407 fieldset $fb->submit('Complete Account Setup'),
408 ],
409 ],
410 };
411 }
412  
413 1;
414  
415 =head1 DESCRIPTION
416  
417 B<WARNINGS>: Experimental code that I might need to break back compatibility in order
418 to fix issues.
419  
420 This is a L<Catalyst::View> subclass that provides a way to write views in code
421 that are strongly typed and per request. It also integrates with several of L<Valiant>'s
422 HTML form generation code modules to make it easier to create HTML forms that properly
423 synchronize with your L<Valiant> models for displaying errors and performing validation.
424  
425 Unlike most Catalyst views, this view is 'per request' in that it is instantiated for
426 each request. This allows you to store per request state in the view object as well as
427 localize view specific logic to the view object. In particular it allows you to avoid or
428 reduce using the stash in order to pass values from the controller to the view. I think
429 this can make your views more robust and easier to support for the long term. It builds
430 upons L<Catalyst::View::BasePerRequest> which provides the per request behavior so you should
431 take a look at the documentation and example controller integration in that module in
432 order to get the idea.
433  
434 As a quick example here's a possible controller that might invoke the view given in the
435 SYNOPSIS:
436  
437 package Example::Controller::Home;
438  
439 use Moose;
440 use MooseX::MethodAttributes;
441  
442 extends 'Catalyst::Controller';
443  
444 sub index($self, $c) {
445 my $view = $c->view('HTML::Home', person=>$c->user);
446 if( # Some condition ) {
447 $view->info('You have been logged in');
448 }
449 }
450  
451 1;
452  
453 This will then work with the commonly used L<Catalyst::Action::RenderView> or my
454 L<Catalyst::ActionRole::RenderView> to produce a view response and set it as the
455 response body.
456  
457 Additionally, this view allows you to import HTML tags from L<Valiant::HTML::Util::TagBuilder>
458 as well as HTML tag helper methods from L<Valiant::HTML::Util::FormTags> and
459 L<Valiant::HTML::Util::Form> into your view code. You should take a look at the
460 documentation for those modules to see what is available. Since L<Valiant::HTML::Util::TagBuilder>
461 includes basic flow control and logic this gives you a bare minimum templating system
462 that is completely in code. You can import some utility methods as well as other views
463 into your view (please see the L</EXPORTS> section below for more details). This is currently
464 lightly documented so I recommend also looking at the test cases as well as the example
465 Catalyst application included in the distribution under the C<example/> directory.
466  
467 =head1 ATTRIBUTES
468  
469 This class inherits all of the attributes from L<Catalyst::View::BasePerRequest>
470  
471 =head1 METHODS
472  
473 This class inherits all of the methods from L<Catalyst::View::BasePerRequest> as well as:
474  
475 =head2 form
476  
477 Returns the current C<form> object.
478  
479 =head2 tags
480  
481 A convenience method to get the C<tags> object from the current C<form>.
482  
483 =head2 safe
484  
485 Marks a string as safe to render by first escaping it and then wrapping it in a L<Valiant::HTML::SafeString> object.
486  
487 =head2 raw
488  
489 Marks a string as safe to render by wrapping it in a L<Valiant::HTML::SafeString> object.
490  
491 =head2 safe_concat
492  
493 Given one or more strings and / or L<Valiant::HTML::SafeString> objects, returns
494 a new L<Valiant::HTML::SafeString> object that is the concatenation of all of the strings.
495  
496 =head2 escape_html
497  
498 Given a string, returns a new string that is the escaped version of the original string.
499  
500 =head2 read_attribute_for_html
501  
502 Given an attribute name, returns the value of that attribute if it exists. If the attribute does not exist, it will die.
503  
504 =head2 attribute_exists_for_html
505  
506 Given an attribute name, returns true if the attribute exists and false if it does notu.
507  
508 =head2 formbuilder_class
509  
510 sub formbuilder_class { 'Example::FormBuilder' }
511  
512 Provides an easy way to override the default formbuilder class. By default it will use
513 L<Valiant::HTML::FormBuilder>. You can override this method to return a different class
514 via a subclass of this view.
515  
516 =head1 EXPORTS
517  
518 =head2 -tags
519  
520 Export any HTML tag supported in L<Valiant::HTML::TagBuilder> as well as tag helpers from
521 L<Valiant::HTML::Util::FormTags> and L<Valiant::HTML::Util::Form>. Please note the C<tr> tag
522 must be imported by the C<trow> name since C<tr> is a reserved word in Perl.
523  
524 =head2 -utils
525  
526 Export the following functions:
527  
528 =over 4
529  
530 =item $sf
531  
532 $person->$sf('Hi there {:first_name} {:last_name} !!')
533  
534 Exports a coderef helper that wraps the C<sf> method in L<Valiant::HTML::TagBuilder>. Useful when
535 you have an object whos methods you want as values in your view.
536  
537 =item content
538  
539 =item content_for
540  
541 =item content_append
542  
543 =item content_replace
544  
545 =item content_around
546  
547 Wraps the named methods from L<Catalyst::View::BasePerRequest> for export. You can still call them
548 directly on the view object if you prefer.
549  
550 =item path
551  
552 Given an instance of L<Catalyst::Action> or the name of an action, returns the full path to that action
553 as a url. Basically a wrapper over C<uri_for> that will die if it can't find the action. It also
554 properly support relatively named actions.
555  
556 =back
557  
558 =head2 -views
559  
560 Create export wrappers for the named Catalyst views. Export names will be snake cased versions
561 of the given view names.
562  
563 =head1 SUBCLASSING
564  
565 You can subclass this view in order to provide your own default behavior and additional methods.
566  
567 package View::Example::View;
568  
569 use Moo;
570 use Catalyst::View::Valiant
571 -tags => qw(blockquote label_tag);
572  
573 sub formbuilder_class { 'Example::FormBuilder' }
574  
575 sub stuff2 {
576 my $self = shift;
577 $self->label_tag('test', sub {
578 my $view = shift;
579 die unless ref($view) eq ref($self);
580 });
581 return $self->tags->div('stuff2');
582 }
583  
584 sub stuff3 :Renders {
585 blockquote 'stuff3',
586 shift->div('stuff333')
587 }
588  
589 1;
590  
591 Then the view C<View::Example::View> can be used in exactly the same way as this view.
592  
593 =head1 TIPS & TRICKS
594  
595 =head2 Creating render methods
596  
597 Often you will want to break up your render method into smaller chunks. You can do this by
598 creating methods that return L<Valiant::HTML::SafeString> objects. You can then call these
599 methods from your render method. Here's an example:
600  
601 sub simple :Renders {
602 my $self = shift;
603 return div "Hey";
604 }
605  
606 You can then call this method from another render method:
607  
608 sub complex :Renders {
609 my $self = shift;
610 return $self->simple;
611 }
612  
613 Or use it directly in your main render method:
614  
615 sub render {
616 my $self = shift;
617 return $self->simple;
618 }
619  
620 Please note you need to add the ':Renders' attribute to your method in order for it to be
621 exported as a render method. You don't need to do that on the main render method in your
622 class because we handle that for you.
623  
624 =head2 Calling for view fragments
625  
626 You can call for the response of any view's method wish is marked as a render method.
627  
628 package Example::View::Fragments;
629  
630 use Moo;
631 use Catalyst::View::Valiant
632 -tags => qw(div);
633  
634 sub stuff4 :Renders { div 'stuff4' }
635  
636 1;
637  
638 Then in your main view:
639  
640 package Example::View::Hello;
641  
642 use Moo;
643 use Catalyst::View::Valiant
644 -views => qw(Fragments);
645  
646 sub render {
647 my $self = shift;
648 return fragment->stuff4;
649 }
650  
651 You can even call them in a controller:
652  
653 sub index :Path {
654 my ($self, $c) = @_;
655 $c->res->body($c->view('Fragments')->stuff4);
656 }
657  
658 =head1 SEE ALSO
659
660 L<Valiant>, L<Valiant::HTML::Util::Form>, L<Valiant::HTML::Util::FormTags>,
661 L<Valiant::HTML::Util::Tagbuilder>, L<Valiant::HTML::SafeString>.
662  
663 =head1 AUTHOR
664
665 See L<Valiant>
666  
667 =head1 COPYRIGHT & LICENSE
668
669 See L<Valiant>
670  
671 =cut
↑ Summary Valiant/Validator/Each.pm »