File Coverage

blib/lib/Dancer/Plugin/Controller.pm
Criterion Covered Total %
statement 15 31 48.3
branch 0 6 0.0
condition 0 16 0.0
subroutine 5 6 83.3
pod n/a
total 20 59 33.9


line stmt bran cond sub pod time code
1             package Dancer::Plugin::Controller;
2              
3             our $VERSION = '0.05';
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;
17              
18             get '/' => sub {
19             controller(
20             action => 'Index', # lib//Index.pm
21             template => 'index', # views/index.[tt|tpl|...]
22             layout => 'page_custom_layout', # if you need other than default layout
23             redirect_404 => '404.html' # redirect to if action method return undef
24             );
25             };
26              
27              
28             # YourApp::Action::Index.pm
29            
30             sub main {
31             my ($class, $params) = @_; # $params - contains Dancer::params() and Dancer::vars()
32              
33             ...
34              
35             return $template_params_hashref;
36             }
37              
38              
39             # config.yml
40              
41             plugins:
42             "Controller":
43             # this is prefiix for module with implementation of action
44             action_prefix: 'MyActionPrefix' # default: 'Action'
45              
46             =cut
47              
48              
49 1     1   24421 use strict;
  1         3  
  1         38  
50 1     1   5 use warnings;
  1         2  
  1         29  
51 1     1   1791 use utf8;
  1         17  
  1         6  
52              
53 1     1   1049 use Dancer ':syntax';
  1         297797  
  1         6  
54 1     1   1405 use Dancer::Plugin;
  1         1554  
  1         448  
55              
56              
57             register controller => sub {
58 0     0     my ($self, %params) = plugin_args(@_);
59              
60 0   0       my $template_name = $params{template} || '';
61 0   0       my $custom_layout = $params{layout} || '';
62 0   0       my $action_name = $params{action} || '';
63 0   0       my $redirect_404 = $params{redirect_404} || '';
64            
65 0           my $conf = plugin_setting();
66 0   0       my $action_prefix = $conf->{action_prefix} || 'Action';
67              
68 0           my $action_class = sprintf('%s::%s::%s', Dancer::config->{appname}, $action_prefix, $action_name);
69 0           my $action_params = {
70             Dancer::params(),
71 0           %{Dancer::vars()}
72             };
73              
74 0 0         my $action_result = $action_name ? $action_class->main($action_params) : {};
75              
76 0 0 0       if (not defined $action_result and $redirect_404) {
77 0           return redirect $redirect_404;
78             }
79             else {
80 0 0         if ($template_name) {
81 0   0       return Dancer::template(
82             $template_name,
83             $action_result,
84             { layout => $custom_layout || Dancer::config->{layout} }
85             );
86             }
87             else {
88 0           return $action_result;
89             }
90             }
91             };
92              
93             register_plugin;
94              
95              
96             1;
97              
98             =head1 AUTHOR
99              
100             Mikhail N Bogdanov C<< >>
101              
102             =cut