File Coverage

blib/lib/App/AutoCRUD/Controller.pm
Criterion Covered Total %
statement 30 31 96.7
branch 1 2 50.0
condition n/a
subroutine 8 9 88.8
pod 3 3 100.0
total 42 45 93.3


line stmt bran cond sub pod time code
1             package App::AutoCRUD::Controller;
2              
3 2     2   971 use 5.010;
  2         5  
4 2     2   7 use strict;
  2         3  
  2         35  
5 2     2   7 use warnings;
  2         2  
  2         40  
6              
7 2     2   6 use Moose;
  2         3  
  2         9  
8 2     2   9333 use Time::HiRes qw/time/;
  2         2219  
  2         8  
9 2     2   329 use namespace::clean -except => 'meta';
  2         4  
  2         17  
10              
11              
12             has 'context' => (is => 'ro', isa => 'App::AutoCRUD::Context',
13             required => 1,
14             handles => [qw/app config dir logger datasource/]);
15              
16              
17             sub respond {
18 24     24 1 35 my ($self) = @_;
19              
20             # compute response data
21 24         75 my $t0 = time;
22 24         83 my $data = $self->serve();
23 16         57 my $t1 = time;
24              
25             # record processing time
26 16         472 my $context = $self->context;
27 16         502 $context->set_process_time($t1-$t0);
28              
29             # render through view
30 16         411 my $view = $context->view;
31 16         59 return $view->render($data, $context);
32             }
33              
34              
35             sub serve {
36 0     0 1 0 die "attempt to serve() from abstract class Controller.pm";
37             }
38              
39              
40             sub redirect { # implemented through a view, which is a bit hacky, but fits
41             # nicely with the general "respond" method above
42 2     2 1 3 my ($self, $url) = @_;
43              
44 2         83 my $context = $self->context;
45 2 50       48 my $view_class = $context->app->find_class("View::Redirect")
46             or die "no Redirect view";
47 2         16 $context->set_view($view_class->new);
48 2         11 return $url;
49             }
50              
51              
52              
53             1;
54              
55             __END__
56              
57              
58             =head1 NAME
59              
60             App::AutoCRUD::Controller - parent class for controllers
61              
62             =head1 DESCRIPTION
63              
64             Parent class for all controllers
65              
66             =head1 METHODS
67              
68             =head2 respond
69              
70             Calls the L</serve> method to build response data; then calls the
71             L<App::AutoCRUD::View/render> method within the appropriate
72             view to build the Plack response.
73              
74             =head2 serve
75              
76             Abstract method (to be redefined in subclasses);
77             should return the datastructure to be passed to the
78             L<App::AutoCRUD::View> class.
79              
80             =head2 redirect
81              
82             $controller->redirect($url);
83              
84             Convenience method to redirect to another URL.
85             Sets the C<view> attribute in L<App::AutoCRUD::Context>
86             to an instance of L<App::AutoCRUD::View::Redirect>;
87             then returns the given URL.
88              
89             This is used by all POST methods, following the
90             L<http://en.wikipedia.org/wiki/Post/Redirect/Get> pattern
91             to avoid duplicate form submissions.
92