File Coverage

blib/lib/Thunderhorse/Router.pm
Criterion Covered Total %
statement 76 82 92.6
branch 12 14 85.7
condition n/a
subroutine 13 14 92.8
pod 4 4 100.0
total 105 114 92.1


line stmt bran cond sub pod time code
1             package Thunderhorse::Router;
2             $Thunderhorse::Router::VERSION = '0.102';
3 21     21   391 use v5.40;
  21         93  
4 21     21   216 use Mooish::Base -standard;
  21         64  
  21         262  
5              
6 21     21   372175 use Thunderhorse::Router::Location;
  21         48689  
  21         1294  
7 21     21   229 use Gears::X::Thunderhorse;
  21         47  
  21         25827  
8              
9             extends 'Gears::Router';
10              
11             has field 'controller' => (
12             isa => InstanceOf ['Thunderhorse::Controller'],
13             writer => 1,
14             );
15              
16             # external caches should have a L1 cache in front of it to map location names
17             # to location objects
18             has option 'cache' => (
19             isa => HasMethods ['get', 'set', 'clear'],
20             writer => 1,
21             trigger => 1,
22             );
23              
24             has field '_registered' => (
25             isa => HashRef,
26             default => sub { {} },
27             );
28              
29             # id of the last route. All route building must be in deterministic order, so
30             # that multiple forked processes can use the same external cached table
31             has field '_last_route_id' => (
32             isa => PositiveOrZeroInt,
33             default => 0,
34             writer => 1,
35             );
36              
37 2         4 sub _trigger_cache ($self, $new)
38 2     2   65 {
  2         2  
  2         3  
39 2 100       23 if ($new->DOES('Thunderhorse::Router::SpecializedCache')) {
40 1         39 $new->set_router($self);
41             }
42             }
43              
44 169         310 sub _build_location ($self, %args)
45 169     169   4894 {
  169         568  
  169         252  
46 169         4305 return Thunderhorse::Router::Location->new(
47             %args,
48             controller => $self->controller,
49             );
50             }
51              
52 140         275 sub _match_level ($self, $locations, @args)
  140         230  
53 140     140   17950 {
  140         322  
  140         219  
54 140         584 my @result = $self->SUPER::_match_level($locations, @args);
55              
56             # optimization - very common to have just one match on a single level
57 140 100       58168 return @result unless @result > 1;
58              
59             return
60 20         61 map { $_->[0] }
61 24         39 sort { $a->[1] <=> $b->[1] }
62 6 100       15 map { [$_, (ref eq 'ARRAY' ? $_->[0] : $_)->location->order] }
  20         113  
63             @result;
64             }
65              
66 132         319 sub _maybe_cache ($self, $type, $path, $method)
  132         246  
  132         237  
67 132     132   229 {
  132         239  
  132         231  
68 132 100       1441 return $self->$type($path, $method)
69             unless $self->has_cache;
70              
71 12         21 my $key = "$type;$method;$path";
72 12         160 my $result = $self->cache->get($key);
73 12 100       68 return $result->@* if $result;
74              
75 8         36 $result = [$self->$type($path, $method)];
76 8         109 $self->cache->set($key, $result);
77 8         68 return $result->@*;
78             }
79              
80 101         200 sub match ($self, $path, $method //= '')
  101         196  
  101         193  
81 101     101 1 198 {
  101         162  
82 101         413 return ($self->_maybe_cache('SUPER::match', $path, $method))[0];
83             }
84              
85 31         87 sub flat_match ($self, $path, $method //= '')
  31         139  
  31         89  
86 31     31 1 40466 {
  31         49  
87 31         103 return $self->_maybe_cache('SUPER::flat_match', $path, $method);
88             }
89              
90             sub clear ($self)
91 0     0 1 0 {
  0         0  
  0         0  
92 0 0       0 $self->cache->clear
93             if $self->has_cache;
94              
95 0         0 $self->_registered->%* = ();
96              
97 0         0 return $self->SUPER::clear;
98             }
99              
100             sub _get_next_route_id ($self)
101 165     165   239 {
  165         280  
  165         238  
102 165         463 my $last = $self->_last_route_id;
103 165         3820 $self->_set_last_route_id(++$last);
104              
105 165         4986 return $last;
106             }
107              
108 169         298 sub _register_location ($self, $name, $location)
  169         286  
109 169     169   4044 {
  169         266  
  169         247  
110             Gears::X::Thunderhorse->raise("duplicate location $name - location names must be unique")
111 169 100       870 if $self->_registered->{$name};
112              
113 168         1690 $self->_registered->{$name} = $location;
114             }
115              
116 17         22 sub find ($self, $name)
117 17     17 1 22 {
  17         23  
  17         21  
118 17         66 return $self->_registered->{$name};
119             }
120              
121             __END__