blib/lib/Minima/Router.pm

filter: Statementstmt 100% 48 / 48 Branchbran 100% 20 / 20 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 15 / 15 Podpod 100% 6 / 6 totaltotal 100% 89 / 89 CC 12 SCAR 9.6
CC
12
Cov
100%
read_file
5.0
_build_options
4.0
_build_destination
3.0
CRAP 2.6
« blib/lib/Minima/View/HTML.pm ↑ Summary blib/lib/Minima/View/JSON.pm »
1 11 ▶use v5.40;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 11 ▶use experimental 'class';
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 class Minima::Router;
5  
6 11 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 11 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 11 ▶use Router::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 11 field $router = Router::Simple->new;
11 11 field %special;
12 field $prefix = 'Controller';
13  
14 42 ▶method match ($env)
Subroutine match: called CC=2 SCAR=6.9
Pod documented
15 {
16 42 my $match = $router->match($env);
17 42 ▶ return $match if defined $match;
Branch: 1 branch
BranchTrueFalseTotal
if defined $match 34 8 42
18  
19 8 $self->not_found_route;
20 }
21  
22 39 ▶method read_file ($file)
Subroutine read_file: called CC=5 SCAR=16.1
Pod documented
23 {
24 39 $file = path($file);
25 39 ▶ croak "Routes file `$file` does not exist.\n"
Branch: 1 branch
BranchTrueFalseTotal
unless -e $file->absolute 2 37 39
26 unless -e $file->absolute;
27  
28 # Parse routes
29 37 for ($file->lines_utf8) {
30 # Skip blank or comment lines
31 105 ▶ next if /^\s*#|^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if /^\s*#|^\s*$/u 20 85 105
32  
33 # Extract data
34 85 $_ = trim $_;
35 85 my ($method, $pattern, $controller, $action) = split;
36  
37 # Build destination and options
38 85 my %dest = $self->_build_destination($controller, $action);
39 84 my %opt = $self->_build_options($method);
40  
41 # Test the nature of the route
42 84 ▶ if ($method eq '@') {
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq '@') { } 21 63 84
43 # Special
44 21 $special{$pattern} = \%dest;
45 } else {
46 # Regular
47 63 $router->connect($pattern, \%dest, \%opt);
48 }
49 }
50 }
51  
52 7 ▶method _build_command ($command, $action)
Subroutine _build_command: called CC=2 SCAR=6.9
53 {
54 7 state %redirects = (
55 redirect => 302,
56 r => 302,
57 redirect_permanent => 301,
58 rp => 301,
59 );
60  
61 # Remove initial `@`
62 7 $command = substr($command, 1);
63  
64 croak "Unknown route command `$command`.\n"
65 7 ▶ unless exists $redirects{$command};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $redirects{$command} 1 6 7
66  
67 (
68 redirect => $action,
69 6 redirect_status => $redirects{$command}
70 )
71 }
72  
73 85 ▶method _build_destination ($controller, $action)
Subroutine _build_destination: called CC=3 SCAR=11.0
74 {
75 85 ▶ if (defined $controller) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $controller) 83 2 85
76 # Check if it is a command
77 83 ▶ return $self->_build_command($controller, $action)
Branch: 1 branch
BranchTrueFalseTotal
if $controller =~ /^@/u 7 76 83
78 if $controller =~ /^@/;
79  
80 # Fix controller prefix
81 76 $controller =~ s/^:+/${prefix}::/;
82 }
83  
84 78 ( controller => $controller, action => $action )
85 }
86  
87 84 ▶method _build_options ($method)
Subroutine _build_options: called CC=4 SCAR=13.9
88 {
89 84 ▶ return () if ($method eq '*'); # Don't add a constraint
Branch: 1 branch
BranchTrueFalseTotal
if $method eq "*" 18 66 84
90 66 ▶ return ( method => [ qw/ GET HEAD / ] ) if $method eq 'GET';
Branch: 1 branch
BranchTrueFalseTotal
if $method eq "GET" 44 22 66
91 22 ▶ return ( method => 'GET' ) if $method eq '_GET';
Branch: 1 branch
BranchTrueFalseTotal
if $method eq "_GET" 1 21 22
92 21 return ( method => $method );
93 }
94  
95 method _connect
96 21 ▶{
Subroutine _connect: called CC=1 SCAR=0.0
97 21 $router->connect(@_);
98 }
99  
100 method clear_routes
101 57 ▶{
Subroutine clear_routes: called CC=1 SCAR=0.0
Pod documented
102 57 $router = Router::Simple->new;
103 57 %special = ( );
104 }
105  
106 2 ▶method set_prefix ($new)
Subroutine set_prefix: called CC=1 SCAR=0.0
Pod documented
107 {
108 2 $prefix = $new;
109 }
110  
111 10 ▶method not_found_route { $special{not_found} }
Subroutine not_found_route: called CC=1 SCAR=0.0
Pod documented
112 11 ▶method error_route { $special{server_error} }
Subroutine error_route: called CC=1 SCAR=0.0
Pod documented
113  
114 __END__
« blib/lib/Minima/View/HTML.pm ↑ Summary blib/lib/Minima/View/JSON.pm »