File Coverage

blib/lib/Kelp/Module/Routes.pm
Criterion Covered Total %
statement 16 16 100.0
branch n/a
condition 1 3 33.3
subroutine 5 5 100.0
pod 1 1 100.0
total 23 25 92.0


line stmt bran cond sub pod time code
1             package Kelp::Module::Routes;
2              
3 37     37   23599 use Carp;
  37         124  
  37         3077  
4 37     37   326 use Kelp::Base 'Kelp::Module';
  37         79  
  37         218  
5 37     37   338 use Plack::Util;
  37         129  
  37         9146  
6              
7             our @CARP_NOT = qw(Kelp::Module Kelp);
8              
9             my $DEFAULT_ROUTER = 'Kelp::Routes';
10              
11             sub build
12             {
13 51     51 1 153 my ($self, %args) = @_;
14              
15 51   33     381 my $router = delete($args{router}) // ('+' . $DEFAULT_ROUTER);
16              
17 51         294 my $router_class = Plack::Util::load_class($router, $DEFAULT_ROUTER);
18 51         925 my $r = $router_class->new(%args);
19              
20             # Register two methods:
21             # * routes - contains the routes instance
22             # * add_route - a shortcut to the 'add' method
23             $self->register(
24             routes => $r,
25             add_route => sub {
26 110     110   745 my $app = shift;
27 110         569 return $r->add(@_);
28             }
29 51         711 );
30             }
31              
32             1;
33              
34             __END__
35              
36             =head1 NAME
37              
38             Kelp::Module::Routes - Default router module for Kelp
39              
40             =head1 SYNOPSIS
41              
42             # config.pl
43             {
44             # This module is included by default
45             # modules => ['Routes'],
46             modules_init => {
47             Routes => {
48             base => 'MyApp'
49             }
50             }
51             }
52              
53             # lib/MyApp.pm
54             sub build {
55             my $self = shift;
56             mt $self->add_route('/', 'home');
57             }
58              
59              
60             =head1 DESCRIPTION
61              
62             This module and L<Kelp::Module::Config> are automatically loaded into each Kelp
63             application. It initializes the routing table for the web application.
64              
65             =head1 REGISTERED METHODS
66              
67             This module registers the following methods into the underlying app:
68              
69             =head2 routes
70              
71             An instance to L<Kelp::Routes>, or whichever router was specified in the
72             configuration.
73              
74             =head2 add_route
75              
76             A shortcut to the L<Kelp::Routes/add> method.
77              
78             =head2 CONFIGURATION
79              
80             The configuration for this module contains the following keys:
81              
82             =head3 router
83              
84             The router class to use. The default value is C<Kelp::Routes>, but any other
85             class can be specified. A normal string will be considered a subclass of
86             C<Kelp::Routes>, for example:
87              
88             router => 'Custom'
89              
90             will look for C<Kelp::Routes::Custom>. To specify a fully qualified class,
91             prefix it with a plus sign.
92              
93             router => '+My::Special::Router'
94              
95             =head3 configuration of the router
96              
97             All other configuration is passed to the router. For the configuration of the
98             default router, see L<Kelp::Routes/ATTRIBUTES>.
99