File Coverage

blib/lib/Minima/Router.pm
Criterion Covered Total %
statement 14 14 100.0
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 19 19 100.0


line stmt bran cond sub pod time code
1 11     11   189249 use v5.40;
  11         63  
2 11     11   653 use experimental 'class';
  11         5293  
  11         67  
3              
4             class Minima::Router;
5              
6 11     11   2577 use Carp;
  11         23  
  11         814  
7 11     11   6376 use Path::Tiny;
  11         108172  
  11         875  
8 11     11   6735 use Router::Simple;
  11         71015  
  11         9951  
9              
10             field $router = Router::Simple->new;
11             field %special;
12             field $prefix = 'Controller';
13              
14             method match ($env)
15             {
16             $router->match($env) // $special{not_found};
17             }
18              
19             method read_file ($file)
20             {
21             $file = path($file);
22             croak "Routes file `$file` does not exist.\n"
23             unless -e $file->absolute;
24              
25             # Parse routes
26             for ($file->lines_utf8) {
27             # Skip blank or comment lines
28             next if /^\s*#|^\s*$/;
29              
30             # Extract data
31             my ($method, $pattern, $controller, $action) = split;
32              
33             # Fix controller prefix
34             $controller =~ s/^:+/${prefix}::/;
35              
36             # Build destination and options
37             my %dest = ( controller => $controller, action => $action );
38             my %opt;
39             if ($method eq '*') {
40             # Do nothing -- don't add a constraint
41             } elsif ($method eq 'GET') {
42             $opt{method} = [ qw/ GET HEAD / ];
43             } elsif ($method eq '_GET') {
44             $opt{method} = 'GET';
45             } else {
46             $opt{method} = $method;
47             }
48              
49             # Test the nature of the route
50             if ($method eq '@') {
51             # Special
52             $special{$pattern} = \%dest;
53             } else {
54             # Regular
55             $router->connect($pattern, \%dest, \%opt);
56             }
57             }
58             }
59              
60             method _connect
61             {
62             $router->connect(@_);
63             }
64              
65             method error_route
66             {
67             $special{server_error}
68             }
69              
70             method clear_routes
71             {
72             $router = Router::Simple->new;
73             %special = ( );
74             }
75              
76             method set_prefix ($new)
77             {
78             $prefix = $new;
79             }
80              
81             __END__