blib/lib/Thunderhorse/Router.pm

filter: Statementstmt 93.6% 44 / 47 Branchbran 85.7% 12 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 93.3% 14 / 15 Podpod 100% 4 / 4 totaltotal 92.5% 74 / 80 CC 10 SCAR 8.1
CC
10
Cov
92%
clear
6.0
_match_level
3.0
_maybe_cache
3.0
CRAP 2.2
« blib/lib/Gears/Logger/Handler.pm ↑ Summary blib/lib/Thunderhorse/AppController.pm »
1 package Thunderhorse::Router;
2 24 $Thunderhorse::Router::VERSION = '0.107';
3 24 ▶use v5.40;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 24 ▶use Mooish::Base -standard;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 24 ▶use Thunderhorse::Router::Location;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 24 ▶use Gears::X::Thunderhorse;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 24 extends 'Gears::Router';
10  
11 24 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 24 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 42 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
27 24 );
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 24 has field '_last_route_id' => (
32 isa => PositiveOrZeroInt,
33 default => 0,
34 writer => 1,
35 );
36  
37 2 ▶sub _trigger_cache ($self, $new)
Subroutine _trigger_cache: called CC=2 SCAR=6.9
38 {
39 2 ▶ if ($new->DOES('Thunderhorse::Router::SpecializedCache')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new->DOES("Thunderhorse::Router::SpecializedCache")) 1 1 2
40 1 $new->set_router($self);
41 }
42 }
43  
44 179 ▶sub _build_location ($self, %args)
Subroutine _build_location: called CC=1 SCAR=0.0
45 {
46 179 return Thunderhorse::Router::Location->new(
47 %args,
48 controller => $self->controller,
49 );
50 }
51  
52 145 ▶sub _match_level ($self, $locations, @args)
Subroutine _match_level: called CC=3 SCAR=11.0
53 {
54 145 my @result = $self->SUPER::_match_level($locations, @args);
55  
56 # optimization - very common to have just one match on a single level
57 145 ▶ return @result unless @result > 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @result > 1 139 6 145
58  
59 return
60 20 map { $_->[0] }
61 24 sort { $a->[1] <=> $b->[1] }
62 6 ▶ map { [$_, (ref eq 'ARRAY' ? $_->[0] : $_)->location->order] }
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq 'ARRAY' ? : 4 16 20
63 @result;
64 }
65  
66 137 ▶sub _maybe_cache ($self, $type, $path, $method)
Subroutine _maybe_cache: called CC=3 SCAR=11.0
67 {
68 137 ▶ return $self->$type($path, $method)
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_cache 125 12 137
69 unless $self->has_cache;
70  
71 12 my $key = "$type;$method;$path";
72 12 my $result = $self->cache->get($key);
73 12 ▶ return $result->@* if $result;
Branch: 1 branch
BranchTrueFalseTotal
if $result 4 8 12
74  
75 8 $result = [$self->$type($path, $method)];
76 8 $self->cache->set($key, $result);
77 8 return $result->@*;
78 }
79  
80 106 ▶sub match ($self, $path, $method //= '')
Subroutine match: called CC=2 SCAR=6.9
Pod documented
81 {
82 106 return ($self->_maybe_cache('SUPER::match', $path, $method))[0];
83 }
84  
85 31 ▶sub flat_match ($self, $path, $method //= '')
Subroutine flat_match: called CC=2 SCAR=6.9
Pod documented
86 {
87 31 return $self->_maybe_cache('SUPER::flat_match', $path, $method);
88 }
89  
90 0 ▶sub clear ($self)
Subroutine clear: not called CC=2 SCAR=17.9
Pod documented
91 {
92 0 ▶ $self->cache->clear
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_cache 0 0 0
93 if $self->has_cache;
94  
95 0 $self->_registered->%* = ();
96  
97 0 return $self->SUPER::clear;
98 }
99  
100 172 ▶sub _get_next_route_id ($self)
Subroutine _get_next_route_id: called CC=1 SCAR=0.0
101 {
102 172 my $last = $self->_last_route_id;
103 172 $self->_set_last_route_id(++$last);
104  
105 172 return $last;
106 }
107  
108 179 ▶sub _register_location ($self, $name, $location)
Subroutine _register_location: called CC=2 SCAR=6.9
109 {
110 Gears::X::Thunderhorse->raise("duplicate location $name - location names must be unique")
111 179 ▶ if $self->_registered->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if $self->_registered->{$name} 1 178 179
112  
113 178 $self->_registered->{$name} = $location;
114 }
115  
116 18 ▶sub find ($self, $name)
Subroutine find: called CC=1 SCAR=0.0
Pod documented
117 {
118 18 return $self->_registered->{$name};
119 }
120  
121 __END__
« blib/lib/Gears/Logger/Handler.pm ↑ Summary blib/lib/Thunderhorse/AppController.pm »