File Coverage

blib/lib/WebNano/Controller.pm
Criterion Covered Total %
statement 45 45 100.0
branch 17 18 94.4
condition 2 3 66.6
subroutine 11 11 100.0
pod 5 5 100.0
total 80 82 97.5


line stmt bran cond sub pod time code
1 5     5   8444 use strict;
  5         10  
  5         184  
2 5     5   25 use warnings;
  5         8  
  5         167  
3              
4             package WebNano::Controller;
5              
6 5     5   776 use Try::Tiny;
  5         2405  
  5         276  
7 5     5   664 use URI::Escape 'uri_unescape';
  5         2601  
  5         252  
8 5     5   3705 use Plack::Request;
  5         275281  
  5         193  
9              
10 5     5   41 use Object::Tiny::RW qw/ app env self_url url_map _req /;
  5         10  
  5         51  
11              
12             sub req {
13 2     2 1 162 my $self = shift;
14 2 50       65 return $self->_req if defined $self->_req;
15 2         67 my $req = Plack::Request->new( $self->env );
16 2         80 $self->_req( $req );
17 2         21 return $req;
18             }
19              
20 7     7 1 28 sub template_search_path { [] }
21              
22             sub render {
23 18     18 1 96 my $self = shift;
24 18         424 return $self->app->renderer->render( c => $self, @_ );
25             }
26              
27             sub local_dispatch {
28 61     61 1 122 my ( $self, $path, @args ) = @_;
29 61         159 my @parts = split /\//, $path;
30 61         192 my $name = uri_unescape( shift @parts );
31 61 100 66     554 $name = 'index' if !defined( $name ) || !length( $name );
32 61         73 my $action;
33 61 100       1405 if( my $map = $self->url_map ){
34 42 100       301 if( ref $map eq 'HASH' ){
35 35 100       98 $action = $self->can( $map->{$name} ) if $map->{$name};
36             }
37 42 100       100 if( ref $map eq 'ARRAY' ){
38 7 100       15 $action = $self->can( $name ) if grep { $_ eq $name } @$map;
  7         32  
39             }
40             }
41 61         234 my $method = $name . '_action';
42 61 100       460 $action = $self->can( $method ) if !$action;
43 61 100       199 return if !$action;
44 31         92 return $action->( $self, @args, @parts );
45             }
46              
47             sub handle {
48 25     25 1 102 my ( $class, %args ) = @_;
49 25         53 my $path = delete $args{path};
50 25         128 my $self = $class->new( %args );
51 25         460 return $self->local_dispatch( $path );
52             };
53              
54             1;
55              
56              
57              
58             =pod
59              
60             =head1 NAME
61              
62             WebNano::Controller - WebNano Controller
63              
64             =head1 VERSION
65              
66             version 0.001
67              
68             =head1 DESCRIPTION
69              
70             This is the WebNano base controller. It's handle method dispatches the request
71             to appropriate action method or to a next controller.
72              
73             The action method should return a string containing the HTML page,
74             a Plack::Response object or a code ref.
75              
76             =head1 SYNOPSIS
77             With Moose:
78              
79             package MyApp::Controller;
80            
81             use Moose;
82             use MooseX::NonMoose;
83              
84             extends 'WebNano::Controller';
85            
86             has '+url_map' => ( default => sub { { 'Mapped Url' => 'mapped_url' } } );
87            
88             sub index_action {
89             my $self = shift;
90             return $self->render( 'index.tt' );
91             }
92            
93             sub mapped_url { 'This is the mapped url page' }
94            
95             1;
96              
97             =head1 METHODS
98              
99             =head2 handle
100              
101             This is a class method - it receives the arguments, creates the controller
102             object and then uses it's L<local_dispatch> method.
103              
104             Should return a Plack::Response object, a string containing the HTML page, a code ref
105             or undef (which is later interpreted as 404).
106              
107             =head2 render
108              
109             Renders a template.
110              
111             =head2 local_dispatch
112              
113             Finds the method to be called for a given path and dispatches to it.
114              
115             =head2 req
116              
117             Plack::Reqest made from env
118              
119             =head2 template_search_path
120              
121             =head1 ATTRIBUTES
122              
123             =head2 url_map
124              
125             A hash that is used as path part to method map.
126              
127             =head2 app
128              
129             Links back to the application object.
130              
131             =head2 env
132              
133             L<PSGI environment|http://search.cpan.org/~miyagawa/PSGI/PSGI.pod#The_Environment>
134              
135             =head2 self_url
136              
137             =head1 AUTHOR
138              
139             Zbigniew Lukasiak <zby@cpan.org>
140              
141             =head1 COPYRIGHT AND LICENSE
142              
143             This software is copyright (c) 2010 by Zbigniew Lukasiak <zby@cpan.org>.
144              
145             This is free software; you can redistribute it and/or modify it under
146             the same terms as the Perl 5 programming language system itself.
147              
148             =cut
149              
150              
151             __END__
152              
153             # ABSTRACT: WebNano Controller
154