File Coverage

blib/lib/Kelp/Routes/Controller.pm
Criterion Covered Total %
statement 15 15 100.0
branch 6 10 60.0
condition n/a
subroutine 3 3 100.0
pod 1 1 100.0
total 25 29 86.2


line stmt bran cond sub pod time code
1             package Kelp::Routes::Controller;
2              
3 1     1   505 use Kelp::Base 'Kelp::Routes';
  1         3  
  1         5  
4 1     1   65 use Carp;
  1         2  
  1         217  
5              
6             sub dispatch {
7 6     6 1 11 my $self = shift;
8 6 50       16 my $app = shift or croak "no app supplied";
9 6 50       13 my $match = shift or croak "no route pattern instance supplied";
10              
11 6 50       14 my $to = $match->to or croak 'No destination defined';
12              
13 6 100       32 return $self->SUPER::dispatch($app, $match) if ref $to;
14              
15 5 50       76 my ($controller_class, $action) = ($to =~ /^(.+)::(\w+)$/)
16             or croak "Invalid controller '$to'";
17              
18 5         26 my $controller = $app->_clone($controller_class);
19 5         11 return $controller->$action(@{ $match->param });
  5         12  
20             }
21              
22              
23             1;
24              
25             __END__
26              
27             =pod
28              
29             =head1 NAME
30              
31             Kelp::Routes::Controller - Routes and controller for Kelp
32              
33             =head1 SYNOPSIS
34              
35             # config.pl
36             # ---------
37             {
38             modules_init => {
39             Routes => {
40             base => 'MyApp::Controller',
41             router => 'Controller'
42             }
43             }
44             }
45              
46             # MyApp/Controller.pm
47             # -------------------
48             package MyApp::Controller;
49             use Kelp::Base 'MyApp';
50              
51             sub shared_method {
52             my $self = shift; # $self is an instance of 'MyApp::Controller'
53             ...
54             }
55              
56              
57             # MyApp/Controller/Users.pm
58             # -------------------------
59             package MyApp::Controller::Users;
60             use Kelp::Base 'MyApp::Controller';
61              
62             sub read {
63             my $self = shift; # $self is an instance of 'MyApp::Controller::Users'
64             ...
65             }
66              
67              
68             =head1 DESCRIPTION
69              
70             This router module reblesses a Kelp application into its own controller class.
71             This allows you to structure your web application in a classic object oriented
72             fashion, having C<$self> an instance to the current class rather than the main
73             web application.
74              
75             You must create a main I<controller> class which inherits from Kelp. Each
76             subsequent class can inherit from this class, taking advantage of any common
77             functionality.
78              
79              
80             =cut