File Coverage

blib/lib/Kelp/Module/Routes.pm
Criterion Covered Total %
statement 14 14 100.0
branch 2 2 100.0
condition 2 3 66.6
subroutine 4 4 100.0
pod 1 1 100.0
total 23 24 95.8


line stmt bran cond sub pod time code
1              
2             use Kelp::Base 'Kelp::Module';
3 31     31   13031 use Plack::Util;
  31         57  
  31         156  
4 31     31   1914  
  31         56  
  31         6077  
5             my $DEFAULT_ROUTER = 'Kelp::Routes';
6              
7             my ( $self, %args ) = @_;
8              
9 40     40 1 99 my $router = delete($args{router}) // ('+' . $DEFAULT_ROUTER);
10              
11 40   66     198 # A module name with a leading + indicates it's already fully
12             # qualified (i.e., it does not need the Kelp::Routes:: prefix).
13             my $prefix = $router =~ s/^\+// ? undef : $DEFAULT_ROUTER;
14              
15 40 100       194 my $router_class = Plack::Util::load_class( $router, $prefix );
16             my $r = $router_class->new( %args );
17 40         119  
18 40         398 # Register two methods:
19             # * routes - contains the routes instance
20             # * add_route - a shortcut to the 'add' method
21             $self->register(
22             routes => $r,
23             add_route => sub {
24             my $app = shift;
25             return $r->add(@_);
26 93     93   310 }
27 93         267 );
28             }
29 40         304  
30             1;
31              
32              
33             =pod
34              
35             =head1 NAME
36              
37             Kelp::Module::Routes - Default router module for Kelp
38              
39             =head1 SYNOPSIS
40              
41             # config.pl
42             {
43             modules => ['Routes'], # included by default
44             modules_init => {
45             Routes => {
46             base => 'MyApp'
47             }
48             }
49             }
50              
51             # lib/MyApp.pm
52             sub build {
53             my $self = shift;
54             mt $self->add_route( '/', 'home' );
55             }
56              
57              
58             =head1 DESCRIPTION
59              
60             This module and L<Kelp::Module::Config> are automatically loaded into each Kelp
61             application. It initializes the routing table for the web application.
62              
63             =head1 REGISTERED METHODS
64              
65             This module registers the following methods into the underlying app:
66              
67             =head2 routes
68              
69             An instance to L<Kelp::Routes>, or whichever router was specified in the
70             configuration.
71              
72             # lib/MyApp.pm
73             sub build {
74             my $self = shift;
75             $self->routes->add( '/', 'home' );
76             }
77              
78             =head2 add_route
79              
80             A shortcut to the L<Kelp::Routes/add> method.
81              
82             =head2 CONFIGURATION
83              
84             The configuration for this module contains the following keys:
85              
86             =head3 router
87              
88             The router class to use. The default value is C<Kelp::Routes>, but any other
89             class can be specified. A normal string will be considered a subclass of
90             C<Kelp::Routes>, for example:
91              
92             router => 'Custom'
93              
94             will look for C<Kelp::Routes::Custom>. To specify a fully qualified class,
95             prefix it with a plus sign.
96              
97             router => '+My::Special::Router
98              
99             See L<Kelp::Routes::Controller> for a router class that reblesses the
100             application instance.
101              
102             =head3 base
103              
104             Specifies the base class of each route. This saves a lot of typing when writing
105             the routes definitions.
106              
107             base => 'MyApp'
108              
109             Now when defining a route you can only type C<myroute> to denote
110             C<MyApp::myroute>.
111              
112              
113             =cut