File Coverage

blib/lib/Router/Simple/Reversible.pm
Criterion Covered Total %
statement 30 30 100.0
branch 8 8 100.0
condition 3 4 75.0
subroutine 5 5 100.0
pod 0 1 0.0
total 46 48 95.8


line stmt bran cond sub pod time code
1             package Router::Simple::Reversible;
2 2     2   1056 use 5.008001;
  2         5  
3 2     2   9 use strict;
  2         3  
  2         48  
4 2     2   19 use warnings;
  2         2  
  2         69  
5 2     2   1141 use parent 'Router::Simple';
  2         595  
  2         9  
6              
7             our $VERSION = "0.01";
8              
9             sub path_for {
10 5     5 0 573 my ($self, $dest, $args) = @_;
11              
12 5   50     10 $dest ||= {};
13 5   100     12 $args ||= {};
14              
15             ROUTE:
16 5         3 foreach my $route (@{ $self->routes }) {
  5         13  
17 14         42 foreach my $key (keys %$dest) {
18 20 100       44 if ($route->dest->{$key} ne $dest->{$key}) {
19 10         36 next ROUTE;
20             }
21             }
22              
23 4         15 my $splat_index = 0;
24 4         7 my $path = $route->pattern;
25 4         22 $path =~ s!
26             \{((?:\{[0-9,]+\}|[^{}]+)+)\} | # /blog/{year:\d{4}}
27             :([A-Za-z0-9_]+) | # /blog/:year
28             (\*) | # /blog/*/*
29             ([^{:*]+) # normal string
30             !
31 11 100       28 if ($1) {
    100          
    100          
32 2         5 my ($name) = split /:/, $1, 2;
33 2         5 $args->{$name};
34             } elsif ($2) {
35 1         3 $args->{$2};
36             } elsif ($3) {
37 2         8 $args->{splat}->[$splat_index++];
38             } else {
39 6         14 $4;
40             }
41             !gex;
42              
43 4         18 return $path;
44             }
45              
46 1         4 return undef;
47             }
48              
49             1;
50             __END__