File Coverage

blib/lib/App/AutoCRUD/Controller.pm
Criterion Covered Total %
statement 31 32 96.8
branch 1 2 50.0
condition n/a
subroutine 8 9 88.8
pod 3 3 100.0
total 43 46 93.4


line stmt bran cond sub pod time code
1             package App::AutoCRUD::Controller;
2              
3 1     1   587 use 5.010;
  1         3  
  1         31  
4 1     1   3 use strict;
  1         1  
  1         27  
5 1     1   4 use warnings;
  1         1  
  1         35  
6              
7 1     1   3 use Moose;
  1         2  
  1         5  
8 1     1   5473 use Time::HiRes qw/time/;
  1         1434  
  1         8  
9 1     1   165 use namespace::clean -except => 'meta';
  1         2  
  1         11  
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 17     17 1 32 my ($self) = @_;
19              
20             # compute response data
21 17         59 my $t0 = time;
22 17         85 my $data = $self->serve();
23 13         57 my $t1 = time;
24              
25             # record processing time
26 13         416 my $context = $self->context;
27 13         390 $context->set_process_time($t1-$t0);
28              
29             # render through view
30 13         337 my $view = $context->view;
31 13         56 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 5 my ($self, $url) = @_;
43              
44 2         65 my $context = $self->context;
45 2 50       50 my $view_class = $context->app->find_class("View::Redirect")
46             or die "no Redirect view";
47 2         22 $context->set_view($view_class->new);
48 2         17 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