File Coverage

blib/lib/Router/Simple.pm
Criterion Covered Total %
statement 56 67 83.5
branch 10 14 71.4
condition 7 13 53.8
subroutine 14 15 93.3
pod 6 6 100.0
total 93 115 80.8


line stmt bran cond sub pod time code
1             package Router::Simple;
2 12     12   40180 use strict;
  12         15  
  12         663  
3 12     12   51 use warnings;
  12         13  
  12         299  
4 12     12   246 use 5.00800;
  12         36  
  12         600  
5             our $VERSION = '0.16';
6 12     12   4368 use Router::Simple::SubMapper;
  12         22  
  12         315  
7 12     12   4791 use Router::Simple::Route;
  12         23  
  12         374  
8 12     12   68 use List::Util qw/max/;
  12         21  
  12         1120  
9 12     12   61 use Carp ();
  12         15  
  12         312  
10              
11             use Class::Accessor::Lite 0.05 (
12 12         70 new => 1,
13             ro => [qw(routes directory_slash)],
14 12     12   46 );
  12         176  
15              
16             our $_METHOD_NOT_ALLOWED;
17              
18             sub connect {
19 40     40 1 350 my $self = shift;
20              
21 40 100       130 if ($self->{directory_slash}) {
22             # connect([$name, ]$pattern[, \%dest[, \%opt]])
23 4 100 100     17 if (@_ == 1 || ref $_[1]) {
24 3         5 unshift(@_, undef);
25             }
26              
27             # \%opt
28 4   100     9 $_[3] ||= {};
29 4         9 $_[3]->{directory_slash} = 1;
30             }
31              
32 40         155 my $route = Router::Simple::Route->new(@_);
33 40         43 push @{ $self->{routes} }, $route;
  40         80  
34 40         102 return $self;
35             }
36              
37             sub submapper {
38 3     3 1 33 my ($self, $pattern, $dest, $opt) = @_;
39 3   50     44 return Router::Simple::SubMapper->new(
      50        
40             parent => $self,
41             pattern => $pattern,
42             dest => $dest || +{},
43             opt => $opt || +{},
44             );
45             }
46              
47             sub _match {
48 43     43   54 my ($self, $env) = @_;
49              
50 43 100       102 if (ref $env) {
51             # "I think there was a discussion about that a while ago and it is up to apps to deal with empty PATH_INFO as root / iirc"
52             # -- by @miyagawa
53             #
54             # see http://blog.64p.org/entry/2012/10/05/132354
55 36 100       96 if ($env->{PATH_INFO} eq '') {
56 1         2 $env->{PATH_INFO} = '/';
57             }
58             } else {
59 7         16 $env = +{ PATH_INFO => $env }
60             }
61              
62 43         46 local $_METHOD_NOT_ALLOWED;
63 43         70 $self->{method_not_allowed} = 0;
64 43         38 for my $route (@{$self->{routes}}) {
  43         125  
65 119         249 my $match = $route->match($env);
66 119 100       254 return ($match, $route) if $match;
67             }
68 9         19 $self->{method_not_allowed} = $_METHOD_NOT_ALLOWED;
69 9         18 return undef; # not matched.
70             }
71              
72             sub method_not_allowed {
73 5     5 1 2959 my $self = shift;
74 5         33 $self->{method_not_allowed};
75             }
76              
77             sub match {
78 42     42 1 16966 my ($self, $req) = @_;
79 42         101 my ($match) = $self->_match($req);
80 42         118 return $match;
81             }
82              
83             sub routematch {
84 1     1 1 36 my ($self, $req) = @_;
85 1         4 return $self->_match($req);
86             }
87              
88             sub as_string {
89 0     0 1   my $self = shift;
90              
91 0 0         my $mn = max(map { $_->{name} ? length($_->{name}) : 0 } @{$self->{routes}});
  0            
  0            
92 0 0         my $nn = max(map { $_->{method} ? length(join(",",@{$_->{method}})) : 0 } @{$self->{routes}});
  0            
  0            
  0            
93              
94             return join('', map {
95 0   0       sprintf "%-${mn}s %-${nn}s %s\n", $_->{name}||'', join(',', @{$_->{method} || []}) || '', $_->{pattern}
  0   0        
96 0           } @{$self->{routes}}) . "\n";
97             }
98              
99             1;
100             __END__