blib/lib/Dancer/Plugin/Controller.pm

filter: Statementstmt 98.0% 49 / 50 Branchbran 66.6% 4 / 6 Conditioncond 50.0% 8 / 16 MC/DCmcdc 22.2% 2 / 9 Subroutinesub 100% 9 / 9 Podpod n/a totaltotal 80.0% 72 / 90 CC 11 SCAR 20.0
CC
11
Cov
85%
__ANON__
12.1
BEGIN
1.0
BEGIN
1.0
CRAP 7.4
↑ Summary
1 package Dancer::Plugin::Controller;
2  
3 2 our $VERSION = '0.153';
4  
5 =head1 NAME
6  
7 Dancer::Plugin::Controller - interface between a model and view
8  
9 =cut
10  
11 =head1 SYNOPSIS
12  
13 # YourApp.pm
14  
15 use Dancer ':syntax';
16 use Dancer::Plugin::Controller '0.15';
17  
18 use YouApp::Action::Index;
19  
20  
21 get '/' => sub {
22 controller(
23 action => 'Index', # lib/<config:action_prefix>/Index.pm
24 template => 'index', # views/index.[tt|tpl|...]
25 layout => 'page_custom_layout', # if you need other than default layout
26 redirect_404 => '404.html' # redirect to if action method return undef
27 );
28 };
29  
30  
31 # YourApp::Action::Index.pm
32
33 sub main {
34 my ($self) = @_;
35
36 my $params = $self->params; # $params - contains Dancer::params() and Dancer::vars()
37  
38 ...
39  
40 return $template_params_hashref;
41 }
42  
43  
44 # config.yml
45  
46 plugins:
47 "Controller":
48 # this is prefiix for module with implementation of action
49 action_prefix: 'MyActionPrefix' # default: 'Action'
50  
51 =cut
52  
53  
54 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
55 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
56 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
57  
58 2 ▶use Dancer ':syntax';
Subroutine BEGIN: called CC=1 SCAR=0.0
59 2 ▶use Dancer::Plugin;
Subroutine BEGIN: called CC=1 SCAR=0.0
60  
61  
62 8 ▶register controller => sub {
Subroutine __ANON__: called CC=11 SCAR=24.9
63 8 my ($self, %params) = plugin_args(@_);
64  
65 8 ▶ my $template_name = $params{template} || '';
Condition50%1/2
$params{'template'} || ''
l!l
08
MC/DC0%0/1
$params{'template'} || ''
$params{'template'}
Truth table50%1/2
A
A= $params{'template'}
Aresult
11
00
66 8 ▶ my $custom_layout = $params{layout} || '';
Condition50%1/2
$params{'layout'} || ''
l!l
08
MC/DC0%0/1
$params{'layout'} || ''
$params{'layout'}
Truth table50%1/2
A
A= $params{'layout'}
Aresult
11
00
67 8 ▶ my $action_name = $params{action} || '';
Condition50%1/2
$params{'action'} || ''
l!l
80
MC/DC0%0/1
$params{'action'} || ''
$params{'action'}
Truth table50%1/2
A
A= $params{'action'}
Aresult
11
00
68 8 ▶ my $redirect_404 = $params{redirect_404} || '';
Condition100%2/2
$params{'redirect_404'} || ''
l!l
26
MC/DC100%1/1
$params{'redirect_404'} || ''
$params{'redirect_404'}
Truth table100%2/2
A
A= $params{'redirect_404'}
Aresult
11
00
69
70 8 my $conf = plugin_setting();
71 8 ▶ my $action_prefix = $conf->{action_prefix} || 'Action';
Condition50%1/2
$conf->{'action_prefix'} || 'Action'
l!l
80
MC/DC0%0/1
$conf->{'action_prefix'} || 'Action'
$conf->{'action_prefix'}
Truth table50%1/2
A
A= $conf->{'action_prefix'}
Aresult
11
00
72  
73 8 my $action_base_class = sprintf('%s::%s', Dancer::config->{appname}, $action_prefix);
74 8 my $action_class = sprintf('%s::%s', $action_base_class, $action_name);
75 my $action_params = {
76 Dancer::params(),
77 8 %{Dancer::vars()}
78 };
79
80 8 my $action_result = {};
81 8 ▶ if ($action_name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($action_name) 8 0 8
82 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
83 8 push @{$action_class. '::ISA'}, $action_base_class;
84 8 ▶ *{$action_class. '::params'} = sub { $_[0]->{params} };
Subroutine __ANON__: called CC=1 SCAR=0.0
85 2 ▶ use strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
86  
87 8 my $action_obj = bless { params => $action_params }, $action_class;
88 8 $action_result = $action_obj->main;
89 }
90  
91 8 ▶ if (not defined $action_result and $redirect_404) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $action_result and $redirect_404) { } 2 6 8
Condition66%2/3
not defined $action_result and $redirect_404
!ll&&!rl&&r
602
MC/DC50%1/2
not defined $action_result and $redirect_404
not defined $action_result $redirect_404
Truth table66%2/3
not(A) and B
A= not defined $action_resultB= $redirect_404
ABresult
0X0
100
111
92 2 return redirect $redirect_404;
93 }
94 else {
95 6 ▶ if ($template_name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($template_name) { } 0 6 6
96 return Dancer::template(
97 $template_name,
98 $action_result,
99 { layout => $custom_layout || Dancer::config->{layout} }
100 0 ▶ );
Condition0%0/3
$custom_layout || Dancer::config()->{'layout'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$custom_layout || Dancer::config()->{'layout'}
$custom_layout Dancer::config()->{'layout'}
Truth table0%0/3
A || B
A= $custom_layoutB= Dancer::config()->{'layout'}
ABresult
1X1
011
000
101 }
102 else {
103 6 return $action_result;
104 }
105 }
106 2 };
107  
108 2 register_plugin;
109  
110  
111 2 1;
112  
113 =head1 AUTHOR
114  
115 Mikhail N Bogdanov C<< <mbogdanov at cpan.org> >>
116  
117 =cut
↑ Summary