File Coverage

blib/lib/Pickles/Dispatcher.pm
Criterion Covered Total %
statement 56 58 96.5
branch 9 12 75.0
condition 2 6 33.3
subroutine 11 11 100.0
pod 3 4 75.0
total 81 91 89.0


line stmt bran cond sub pod time code
1             package Pickles::Dispatcher;
2 5     5   54428 use strict;
  5         12  
  5         226  
3 5     5   29 use Carp ();
  5         8  
  5         146  
4 4     4   22 use Cwd();
  4         8  
  4         73  
5 4     4   940 use Plack::Util;
  4         17661  
  4         103  
6 4     4   5443 use Router::Simple;
  4         40964  
  4         213  
7 4     4   1799 use String::CamelCase qw(camelize);
  4         1428  
  4         2418  
8              
9             sub new {
10 6     6 1 34 my ($class, %args) = @_;
11              
12 6   33     41 my $file = $args{file} || Carp::croak("No file given to $class->new");
13 6         16 my $pkg = $file;
14 6         42 $pkg =~ s/([^A-Za-z0-9_])/sprintf("_%2x", unpack("C", $1))/eg;
  64         347  
15              
16 6 100       55 if (! File::Spec->file_name_is_absolute( $file ) ) {
17 1         71 $file = Cwd::abs_path( $file );
18             }
19 6         26 my $fqname = sprintf '%s::%s', $class, $pkg;
20 6         19 my $router_pkg = sprintf <<'SANDBOX', $fqname;
21             package %s;
22             use Router::Simple::Declare;
23             {
24             delete $INC{$file};
25             my $conf = require $file or die $!;
26             $conf;
27             }
28             SANDBOX
29 4     4   4000 my $router = eval $router_pkg;
  4         1836  
  4         371  
  6         434  
30 6 50 33     15 if (! eval { $router->isa( 'Router::Simple' ) } || $@ ) {
  6         81  
31 0         0 Carp::croak("file $args{file} returned something other than Router::Simple");
32             }
33 6         52 bless { router => $router }, $class;
34             }
35              
36             sub router {
37 11     11 1 21 my $self = shift;
38 11         58 $self->{router};
39             }
40              
41             sub match {
42 6     6 1 27 my( $self, $req ) = @_;
43 6         31 my $match = $self->router->match( $req->env );
44 6         661 my %args;
45 6         11 for my $key( keys %{$match} ) {
  6         23  
46 15 100       74 next if $key =~ m{^(controller|action)$};
47 3         16 $args{$key} = delete $match->{$key};
48             }
49 6         19 $match->{args} = \%args;
50 6         31 $match;
51             }
52              
53             sub load_controllers {
54 5     5 0 79 my( $self, $prefix ) = @_;
55 5         23 my $routes = $self->router->{routes};
56 5 50       20 if ($routes) {
57 5         11 my %seen;
58 5         11 foreach my $route (@$routes) {
59 110         37231 my $dest = $route->dest;
60 110         477 my $controller = $dest->{controller};
61 110 50       197 if (! $controller) {
62 0         0 warn "No controller specified for path " . $route->pattern;
63             }
64 110 100       274 next if $seen{ $controller }++;
65 20         72 Plack::Util::load_class( "Controller::" . camelize($controller), $prefix );
66             }
67             }
68 5         2154 1;
69             }
70              
71             1;
72              
73             __END__