File Coverage

blib/lib/Gears/Router.pm
Criterion Covered Total %
statement 58 63 92.0
branch 6 6 100.0
condition n/a
subroutine 11 12 91.6
pod 5 5 100.0
total 80 86 93.0


line stmt bran cond sub pod time code
1             package Gears::Router;
2             $Gears::Router::VERSION = '0.101';
3 4     4   37597 use v5.40;
  4         15  
4 4     4   20 use Mooish::Base -standard;
  4         6  
  4         41  
5              
6 4     4   56784 use Gears::Router::Match;
  4         3740  
  4         3017  
7              
8             with qw(Gears::Router::Proto);
9              
10             # we are the router
11             has extended 'router' => (
12             init_arg => undef,
13             default => sub ($self) { $self },
14             );
15              
16             # base route is an empty string
17             sub pattern ($self)
18 43     43 1 69 {
  43         63  
  43         65  
19 43         365 return '';
20             }
21              
22             # builds a new location - see Gears::Router::Proto
23             # - must create a new subclass of Gears::Router::Location, where %args are
24             # constructor arguments
25             # - must be reimplemented
26 0         0 sub _build_location ($self, %args)
27 0     0   0 {
  0         0  
  0         0  
28 0         0 ...;
29             }
30              
31             # builds a new match
32             # can be reimplemented
33 308         508 sub _build_match ($self, %args)
34 308     308   407 {
  308         716  
  308         466  
35 308         6341 return Gears::Router::Match->new(%args);
36             }
37              
38 360         484 sub _match_level ($self, $locations, @args)
  360         442  
39 360     360   460 {
  360         590  
  360         413  
40 360         412 my @matched;
41 360         680 foreach my $loc ($locations->@*) {
42 387 100       1126 next unless my $match_data = $loc->compare(@args);
43 308         856 my $match = $self->_build_match(
44             location => $loc,
45             matched => $match_data,
46             );
47              
48 308         22243 my $children = $loc->locations;
49 308 100       727 if ($children->@* > 0) {
50 199         567 push @matched, [$match, $self->_match_level($children, @args)];
51             }
52             else {
53 109         302 push @matched, $match;
54             }
55             }
56              
57 360         1716 return @matched;
58             }
59              
60 161         313 sub _match ($self, @args)
61 161     161   302 {
  161         287  
  161         179  
62 161         536 return [$self->_match_level($self->locations, @args)];
63             }
64              
65             sub match
66             {
67 159     159 1 96006 goto \&_match;
68             }
69              
70 2         6 sub flat_match ($self, @args)
71 2     2 1 45334 {
  2         6  
  2         4  
72 2         11 my $matches = $self->_match(@args);
73 2         17 return $self->flatten($matches);
74             }
75              
76 100         163 sub flatten ($self, $matches)
77 100     100 1 147 {
  100         132  
  100         141  
78 100         131 my @flat_matches;
79 100         175 foreach my $match ($matches->@*) {
80 199 100       413 if (ref $match eq 'ARRAY') {
81 97         331 push @flat_matches, $self->flatten($match);
82             }
83             else {
84 102         202 push @flat_matches, $match;
85             }
86             }
87              
88 100         1179 return @flat_matches;
89             }
90              
91             sub clear ($self)
92 38     38 1 76501 {
  38         74  
  38         58  
93 38         566 $self->locations->@* = ();
94 38         243 return $self;
95             }
96              
97             __END__