blib/lib/Yukki/Web/View.pm

filter: Statementstmt 39.0% 50 / 128 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 24 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 53.8% 14 / 26 Podpod 100% 7 / 7 totaltotal 32.4% 71 / 219 CC 24 SCAR 37.4
CC
24
Cov
29%
render_page
132.0
page_template
56.0
available_menu_items
30.0
CRAP 42.0
« blib/lib/Yukki/Web/Controller/Login.pm ↑ Summary blib/lib/Yukki/Web/Controller/Page.pm »
1 package Yukki::Web::View;
2 22 $Yukki::Web::View::VERSION = '0.991_007'; # TRIAL
3 22 $Yukki::Web::View::VERSION = '0.991007';
4 22 ▶use v5.24;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 22 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 22 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 22 ▶use Encode qw( encode );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 22 ▶use Type::Params qw( validate );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 22 ▶use Scalar::Util qw( blessed reftype );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 22 ▶use Spreadsheet::Engine;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 22 ▶use Template::Pure;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 22 ▶use Text::MultiMarkdown;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 22 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 22 ▶use Type::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 22 ▶use Types::Standard qw( Bool Dict Str ArrayRef HashRef Optional slurpy );
Subroutine BEGIN: called CC=1 SCAR=0.0
17 22 ▶use Types::URI qw( Uri );
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 22 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 # ABSTRACT: base class for Yukki::Web views
22  
23  
24 22 has app => (
25 is => 'ro',
26 isa => class_type('Yukki::Web'),
27 required => 1,
28 weak_ref => 1,
29 handles => 'Yukki::Role::App',
30 );
31  
32  
33 22 has markdown => (
34 is => 'ro',
35 isa => class_type('Text::MultiMarkdown'),
36 required => 1,
37 lazy => 1,
38 builder => '_build_markdown',
39 handles => {
40 'format_markdown' => 'markdown',
41 },
42 );
43  
44 0 ▶sub _build_markdown {
Subroutine _build_markdown: not called CC=1 SCAR=6.9
45 0 Text::MultiMarkdown->new(
46 markdown_in_html_blocks => 1,
47 heading_ids => 0,
48 );
49 }
50  
51  
52 22 has messages_template => (
53 is => 'ro',
54 isa => class_type('Template::Pure'),
55 lazy => 1,
56 builder => '_build_messages_template',
57 );
58  
59 0 ▶sub _build_messages_template {
Subroutine _build_messages_template: not called CC=1 SCAR=6.9
60 0 my $self = shift;
61 0 return $self->prepare_template(
62 template => 'messages.html',
63 directives => [
64 '.error' => {
65 'error<-errors' => [
66 '.' => 'error',
67 ],
68 },
69 '.warning' => {
70 'warning<-warnings' => [
71 '.' => 'warning',
72 ],
73 },
74 '.info' => {
75 'one_info<-info' => [
76 '.' => 'one_info',
77 ],
78 },
79 ],
80 );
81 }
82  
83 has _page_templates => (
84 is => 'ro',
85 isa => HashRef,
86 required => 1,
87 0 ▶ default => sub { +{} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
88 22 );
89  
90  
91 22 has links_template => (
92 is => 'ro',
93 isa => class_type('Template::Pure'),
94 lazy => 1,
95 builder => '_build_links_template',
96 );
97  
98 0 ▶sub _build_links_template {
Subroutine _build_links_template: not called CC=1 SCAR=6.9
99 0 my $self = shift;
100 0 $self->prepare_template(
101 template => 'links.html',
102 directives => [
103 '.links li' => {
104 'link<-links' => [
105 'a' => 'link.label',
106 'a@href' => 'link.href',
107 'a@class' => 'link.class',
108 ],
109 },
110 ],
111 );
112 }
113  
114  
115 22 has confirm_template => (
116 is => 'ro',
117 isa => class_type('Template::Pure'),
118 lazy => 1,
119 builder => '_build_confirm_template',
120 );
121  
122 0 ▶sub _build_confirm_template {
Subroutine _build_confirm_template: not called CC=1 SCAR=6.9
123 0 my $self = shift;
124  
125 0 $self->prepare_template(
126 template => 'confirm.html',
127 directives => [
128 'h1.title' => 'title',
129 '.question' => 'question | encoded_string',
130 'form@class' => 'double_confirm',
131 '#submit' => 'yes_label',
132 '#cancel_confirmation@href' => 'no_link',
133 ],
134 );
135 }
136  
137  
138 0 ▶sub page_template {
Subroutine page_template: not called CC=7 SCAR=40.3
Pod documented
139 0 my ($self, $which) = @_;
140  
141 return $self->_page_templates->{ $which }
142 0 ▶ if $self->_page_templates->{ $which };
Branch: 1 branch
BranchTrueFalseTotal
if $self->_page_templates->{$which} 0 0 0
143  
144 0 ▶ my $view = $which // 'default';
Condition0%0/2
$which // "default"
l!l
00
MC/DC0%0/1
$which // "default"
$which
Truth table0%0/2
A
A= $which
Aresult
11
00
145 0 ▶ my $view_args = $self->app->settings->page_views->{ $view }
Condition0%0/2
$self->app->settings->page_views->{$view} // {"template", "shell.html"}
l!l
00
MC/DC0%0/1
$self->app->settings->page_views->{$view} // {"template", "shell.html"}
$self->app->settings->page_views->{$view}
Truth table0%0/2
A
A= $self->app->settings->page_views->{$view}
Aresult
11
00
146 // { template => 'shell.html' };
147 0 ▶ $view_args->{directives} //= [];
Condition0%0/2
$view_args->{"directives"} //= []
l!l
00
MC/DC0%0/1
$view_args->{"directives"} //= []
$view_args->{"directives"}
Truth table0%0/2
A
A= $view_args->{"directives"}
Aresult
11
00
148  
149 my %menu_vars = map {
150 0 my $menu_name = $_;
151 0 "#nav-$menu_name .navigation" => {
152 "menu_item<-$menu_name" => [
153 'a' => 'menu_item.label',
154 'a@href' => 'menu_item.href',
155 'a@class' => 'menu_item.class',
156 'a@id' => 'menu_item.id',
157 ],
158 },
159 0 } @{ $self->app->settings->menu_names };
160  
161 # Page styles may omit bits they don't care about. Check to see if the
162 # template will support them and leave them off if not. Template::Pure dies
163 # if a directive asks for an element that isn't part of the page. :(
164 my @optional_directives = (
165 $view_args->{directives}->@*,
166 0 'head script.local' => {
167 'script<-scripts' => [
168 '@src' => 'script',
169 ],
170 },
171 'head link.local' => {
172 'link<-links' => [
173 '@href' => 'link',
174 ],
175 },
176 '#messages' => 'messages | encoded_string',
177 'title' => 'main_title',
178 '.masthead-title' => 'title',
179 %menu_vars,
180 '#breadcrumb li' => {
181 'crumb<-breadcrumb' => [
182 'a' => 'crumb.label',
183 'a@href' => 'crumb.href',
184 ],
185 },
186 '#content' => 'content | encoded_string',
187 );
188  
189 my $template_content =
190 0 $self->app->locate_dir('template_path', $view_args->{template})->slurp_utf8;
191 0 my $template_dom = Mojo::DOM58->new($template_content);
192  
193 0 my @directives;
194 0 ▶ while (my ($css, $lookup) = splice(@optional_directives, 0, 2)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@optional_directives, 0, 2)) 0 0 0
195 0 ▶ if (defined $template_dom->at($css)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $template_dom->at($css)) 0 0 0
196 0 push @directives, $css, $lookup;
197 }
198 }
199  
200 return $self->_page_templates->{ $which } = $self->prepare_template(
201 template => $view_args->{template},
202 0 directives => \@directives,
203 );
204 }
205  
206  
207 0 ▶sub prepare_template {
Subroutine prepare_template: not called CC=1 SCAR=6.9
Pod documented
208 0 my ($self, $opt)
209 = validate(\@_, class_type(__PACKAGE__),
210 slurpy Dict[
211 template => Str,
212 directives => ArrayRef,
213 ]);
214 0 my ($template, $directives) = @{$opt}{qw( template directives )};
215  
216 0 my $template_content =
217 $self->app->locate_dir('template_path', $template)->slurp_utf8;
218  
219 0 return Template::Pure->new(
220 template => $template_content,
221 directives => $directives,
222 );
223 }
224  
225  
226 0 ▶sub render_confirmation {
Subroutine render_confirmation: not called CC=2 SCAR=17.9
Pod documented
227 0 my ($self, $opt)
228 = validate(\@_, class_type(__PACKAGE__),
229 slurpy Dict[
230 context => class_type('Yukki::Web::Context'),
231 title => Str,
232 question => Str,
233 yes_label => Str,
234 no_link => Str|Uri,
235 double_confirm => Optional[Bool],
236 ]
237 );
238 my (
239 $ctx,
240 $title,
241 $question,
242 $yes_label,
243 $no_link,
244 $double_confirm,
245 0 ) = @{$opt}{qw(
246 context
247 title
248 question
249 yes_label
250 no_link
251 double_confirm
252 )};
253  
254 0 ▶ $double_confirm = $double_confirm ? 'double-confirm' : '';
Branch: 1 branch
BranchTrueFalseTotal
$double_confirm ? : 0 0 0
255  
256 0 return $self->render_page(
257 template => $self->confirm_template,
258 context => $ctx,
259 vars => {
260 title => $title,
261 question => $question,
262 yes_label => $yes_label,
263 no_link => $no_link,
264 double_confirm => $double_confirm,
265 },
266 );
267 }
268  
269  
270 0 ▶sub render_page {
Subroutine render_page: not called CC=11 SCAR=48.8
Pod documented
271 0 my ($self, $opt)
272 = validate(\@_, class_type(__PACKAGE__),
273 slurpy Dict[
274 template => class_type('Template::Pure'),
275 context => class_type('Yukki::Web::Context'),
276 vars => HashRef,
277 ]);
278 0 my ($template, $ctx, $vars) = @{$opt}{qw( template context vars )};
279 0 ▶ $vars //= {};
Condition0%0/2
$vars //= {}
l!l
00
MC/DC0%0/1
$vars //= {}
$vars
Truth table0%0/2
A
A= $vars
Aresult
11
00
280  
281 0 ▶ my $messages = $self->render(
Branch: 3 branches
BranchTrueFalseTotal
$ctx->has_errors ? : 0 0 0
$ctx->has_warnings ? : 0 0 0
$ctx->has_info ? : 0 0 0
282 template => $self->messages_template,
283 context => $ctx,
284 vars => {
285 errors => $ctx->has_errors ? [ $ctx->list_errors ] : undef,
286 warnings => $ctx->has_warnings ? [ $ctx->list_warnings ] : undef,
287 info => $ctx->has_info ? [ $ctx->list_info ] : undef,
288 },
289 );
290  
291 0 my ($main_title, $title);
292 0 ▶ if ($ctx->response->has_page_title) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ctx->response->has_page_title) { } 0 0 0
293 0 $title = $ctx->response->page_title;
294 0 $main_title = $ctx->response->page_title . ' - Yukki';
295 }
296 else {
297 0 $title = $main_title = 'Yukki';
298 }
299  
300 my %menu_vars = map {
301 0 ▶ $_ => ($self->available_menu_items($ctx, $_) || [])
Condition0%0/2
$self->available_menu_items($ctx, $_) || []
l!l
00
MC/DC0%0/1
$self->available_menu_items($ctx, $_) || []
$self->available_menu_items($ctx, $_)
Truth table0%0/2
A
A= $self->available_menu_items($ctx, $_)
Aresult
11
00
302 0 } @{ $self->app->settings->menu_names };
303  
304 0 my @scripts = $self->app->settings->all_scripts;
305 0 my @styles = $self->app->settings->all_styles;
306  
307 0 ▶ my $view = $ctx->request->parameters->{view} // 'default';
Condition0%0/2
$ctx->request->parameters->{"view"} // "default"
l!l
00
MC/DC0%0/1
$ctx->request->parameters->{"view"} // "default"
$ctx->request->parameters->{"view"}
Truth table0%0/2
A
A= $ctx->request->parameters->{"view"}
Aresult
11
00
308  
309 0 ▶ $vars->{'head script.local'} //= [];
Condition0%0/2
$vars->{"head script.local"} //= []
l!l
00
MC/DC0%0/1
$vars->{"head script.local"} //= []
$vars->{"head script.local"}
Truth table0%0/2
A
A= $vars->{"head script.local"}
Aresult
11
00
310 0 ▶ $vars->{'head link.local'} //= [];
Condition0%0/2
$vars->{"head link.local"} //= []
l!l
00
MC/DC0%0/1
$vars->{"head link.local"} //= []
$vars->{"head link.local"}
Truth table0%0/2
A
A= $vars->{"head link.local"}
Aresult
11
00
311  
312 return $self->render(
313 template => $self->page_template($view),
314 context => $ctx,
315 vars => {
316 $vars->%*,
317 scripts => [
318 0 map { $ctx->rebase_url($_) }
319 @scripts,
320 $vars->{'head script.local'}->@*,
321 ],
322 links => [
323 0 ▶ map { $ctx->rebase_url($_) }
Branch: 1 branch
BranchTrueFalseTotal
$ctx->response->has_breadcrumb ? : 0 0 0
324 @styles,
325 $vars->{'head link.local'}->@*,
326 ],
327 'messages' => $messages,
328 'main_title' => $main_title,
329 'title' => $title,
330 %menu_vars,
331 'breadcrumb' => $ctx->response->has_breadcrumb ? [
332 map {
333 0 +{
334 %$_,
335 0 href => $ctx->rebase_url($_->{href}),
336 }
337 } $ctx->response->breadcrumb_links
338 ] : undef,
339 'content' => $self->render(
340 template => $template,
341 context => $ctx,
342 vars => $vars,
343 ),
344 },
345 );
346 }
347  
348  
349 0 ▶sub available_menu_items {
Subroutine available_menu_items: not called CC=5 SCAR=34.0
Pod documented
350 0 my ($self, $ctx, $name) = @_;
351  
352 my @items = map {
353 +{
354 %$_,
355 id => $_->{id},
356 class => $_->{class},
357 0 href => $ctx->rebase_url($_->{href}),
358 },
359 } grep {
360 0 my $url = $_->{href}; $url =~ s{\?.*$}{};
361  
362 0 my $match = $self->app->router->match($url);
363 0 ▶ return unless $match;
Branch: 1 branch
BranchTrueFalseTotal
unless $match 0 0 0
364 0 my $access_level_needed = $match->access_level;
365 $self->check_access(
366 user => $ctx->session->{user},
367 repository => ($match->mapping->{repository} // '-'),
368 0 ▶ special => ($match->mapping->{special} // '-'),
Condition0%0/4
$match->mapping->{"repository"} // "-"
l!l
00
$match->mapping->{"special"} // "-"
l!l
00
MC/DC0%0/1
$match->mapping->{"repository"} // "-"
$match->mapping->{"repository"}
MC/DC0%0/1
$match->mapping->{"special"} // "-"
$match->mapping->{"special"}
Truth table0%0/2
A
A= $match->mapping->{"repository"}
Aresult
11
00
Truth table0%0/2
A
A= $match->mapping->{"special"}
Aresult
11
00
369 needs => $access_level_needed,
370 );
371 } $ctx->response->navigation_menu($name);
372  
373 0 ▶ return @items ? \@items : undef;
Branch: 1 branch
BranchTrueFalseTotal
@items ? : 0 0 0
374 }
375  
376  
377 0 ▶sub render_links {
Subroutine render_links: not called CC=2 SCAR=17.9
Pod documented
378 0 my ($self, $opt)
379 = validate(\@_, class_type(__PACKAGE__),
380 slurpy Dict[
381 context => class_type('Yukki::Web::Context'),
382 links => ArrayRef[HashRef],
383 ]);
384 0 my ($ctx, $links) = @{$opt}{qw( context links )};
385  
386 return $self->render(
387 template => $self->links_template,
388 context => $ctx,
389 vars => {
390 links => [ map {
391 0 +{
392 label => $_->{label},
393 href => $ctx->rebase_url($_->{href}),
394 0 ▶ class => $_->{class} // '',
Condition0%0/2
$_->{"class"} // ""
l!l
00
MC/DC0%0/1
$_->{"class"} // ""
$_->{"class"}
Truth table0%0/2
A
A= $_->{"class"}
Aresult
11
00
395 }
396 } @$links ],
397 },
398 );
399 }
400  
401  
402 0 ▶sub render {
Subroutine render: not called CC=2 SCAR=17.9
Pod documented
403 0 my ($self, $opt)
404 = validate(\@_, class_type(__PACKAGE__),
405 slurpy Dict[
406 template => class_type('Template::Pure'),
407 context => class_type('Yukki::Web::Context'),
408 vars => HashRef,
409 ]);
410 0 my ($template, $ctx, $vars) = @{$opt}{qw( template context vars )};
411 0 ▶ $vars //= {};
Condition0%0/2
$vars //= {}
l!l
00
MC/DC0%0/1
$vars //= {}
$vars
Truth table0%0/2
A
A= $vars
Aresult
11
00
412  
413 0 my %vars = (
414 %$vars,
415 ctx => $ctx,
416 view => $self,
417 );
418  
419 0 return encode('UTF-8', $template->render($vars));
420 }
421  
422 22 1;
423  
424 __END__
« blib/lib/Yukki/Web/Controller/Login.pm ↑ Summary blib/lib/Yukki/Web/Controller/Page.pm »