lib/Dancer/Route/Registry.pm

filter: Statementstmt 95.4% 84 / 88 Branchbran 83.3% 20 / 24 Conditioncond 87.5% 7 / 8 MC/DCmcdc 80.0% 4 / 5 Subroutinesub 93.3% 14 / 15 Podpod 12.5% 1 / 8 totaltotal 87.8% 130 / 148 CC 19 SCAR 13.7
CC
19
Cov
92%
find_route
12.0
any_add
6.0
add_route
4.0
CRAP 3.9
« lib/Dancer/Template/Abstract.pm ↑ Summary lib/Dancer/Session.pm »
1 package Dancer::Route::Registry;
2 194 our $AUTHORITY = 'cpan:SUKRIA';
3 # ABSTRACT: Route registry for Dancer
4 194 $Dancer::Route::Registry::VERSION = '1.3522';
5 194 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 194 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 194 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 194 ▶use Dancer::Route;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 194 ▶use base 'Dancer::Object';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 194 ▶use Dancer::Logger;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 194 ▶use Dancer::Exception qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 194 Dancer::Route::Registry->attributes(qw( id ));
14  
15 194 my $id = 1;
16  
17 115 ▶sub init {
Subroutine init: called CC=2 SCAR=7.0
Pod documented
18 115 my ($self) = @_;
19  
20 115 ▶ unless (defined $self->{id}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"id"}) 115 0 115
21 115 $self->id($id++);
22 }
23  
24 # Routes are stored here. Keys are route methods, and values are
25 # arrays of routes. Routes with options are stored in the
26 # beginning of the array while routes without options are stored
27 # at the end.
28 115 $self->{routes} = {};
29  
30 # Keep track of the border between routes with and without
31 # options, so that routes with options can be easily inserted into
32 # its routes array.
33 115 $self->{routes_border} = {};
34  
35 115 return $self;
36 }
37  
38 2 ▶sub is_empty {
Subroutine is_empty: called CC=3 SCAR=11.1
Pod undocumented
39 2 my ($self) = @_;
40 2 for my $method ( keys %{ $self->routes } ) {
41 1 ▶ return 0 if $self->routes($method);
Branch: 1 branch
BranchTrueFalseTotal
if $self->routes($method) 1 0 1
42 }
43 1 return 1;
44 }
45  
46 # replace any ':foo' by '(.+)' and stores all the named
47 # matches defined in $REG->{route_params}{$route}
48 3477 ▶sub routes {
Subroutine routes: called CC=3 SCAR=11.0
Pod undocumented
49 3477 my ($self, $method) = @_;
50  
51 3477 ▶ if ($method) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method) { } 3475 2 3477
52 3475 my $route = $self->{routes}{$method};
53 3475 ▶ return $route ? $route : [];
Branch: 1 branch
BranchTrueFalseTotal
$route ? : 3464 11 3475
54 }
55 else {
56 2 return $self->{routes};
57 }
58 }
59  
60 1442 ▶sub add_route {
Subroutine add_route: called CC=4 SCAR=13.9
Pod undocumented
61 1442 my ($self, $route) = @_;
62 1442 ▶ $self->{routes}{$route->method} ||= [];
Condition100%2/2
$self->{"routes"}{$route->method} ||= []
l!l
1178264
MC/DC100%1/1
$self->{"routes"}{$route->method} ||= []
$self->{"routes"}{$route->method}
Truth table100%2/2
A
A= $self->{"routes"}{$route->method}
Aresult
11
00
63 1442 my @registered = @{$self->routes($route->method)};
64 1442 my $last = $registered[-1];
65 1442 ▶ $route->set_previous($last) if defined $last;
Branch: 1 branch
BranchTrueFalseTotal
if defined $last 1177 265 1442
66  
67 # Routes are stored in the order they are declared. However,
68 # routes with options (such as ajax routes) are stored before
69 # routes without options. This way, we can have the following
70 # routes:
71 #
72 # get '/' => sub {};
73 # get qr{.*} => sub {};
74 # ajax '/' => sub {};
75 # ajax qr{.*} => sub {};
76 #
77 # And the user won't have to declare the ajax routes before the
78 # get routes.
79 1442 ▶ if (keys %{$route->{options}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %{$$route{'options'};}) { } 11 1431 1442
80 11 splice @{$self->routes($route->method)},
81 11 $self->{routes_border}{$route->method}++,
82 0,
83 $route;
84 }
85 else {
86 1431 push @{$self->routes($route->method)}, $route;
87 }
88  
89 1442 return $route;
90 }
91  
92 # sugar for add_route
93  
94 1443 ▶sub register_route {
Subroutine register_route: called CC=3 SCAR=11.0
Pod undocumented
95 1443 my ($self, %args) = @_;
96  
97 # look if the caller (where the route is declared) exists as a Dancer::App
98 # object
99 1443 my ($package) = caller(2);
100 1443 ▶ if ($package && Dancer::App->app_exists($package)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($package and 'Dancer::App'->app_exists($package)) { } 655 788 1443
Condition100%3/3
$package and "Dancer::App"->app_exists($package)
!ll&&!rl&&r
1787655
MC/DC100%2/2
$package and "Dancer::App"->app_exists($package)
$package "Dancer::App"->app_exists($package)
Truth table100%3/3
A and B
A= $packageB= "Dancer::App"->app_exists($package)
ABresult
0X0
100
111
101 655 my $app = Dancer::App->get($package);
102 655 my $route = Dancer::Route->new(prefix => $app->prefix, %args);
103 654 return $app->registry->add_route($route);
104 }
105 else {
106  
107 # FIXME maybe this code is useless, drop it later if so
108 788 my $route = Dancer::Route->new(%args);
109 788 return $self->add_route($route);
110 }
111 }
112  
113 # sugar for Dancer.pm
114 # class, any, ARRAY(0x9864818), '/path', CODE(0x990ac88)
115 # or
116 # class, any, '/path', CODE(0x990ac88)
117 24 ▶sub any_add {
Subroutine any_add: called CC=6 SCAR=17.9
Pod undocumented
118 24 my ($self, $pattern, @rest) = @_;
119  
120 24 my @methods = qw(get post put patch delete options head);
121  
122 24 ▶ if (ref($pattern) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $pattern eq "ARRAY") 19 5 24
123 19 @methods = @$pattern;
124 # 'get' defaults to 'get' and 'head'
125 38 push @methods, 'head' if ((grep { $_ eq 'get' } @methods) and
126 19 ▶ not (grep { $_ eq 'head' } @methods));
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ eq "get";} @methods and not grep({$_ eq "head";} @methods) 2 17 19
Condition66%2/3
grep {$_ eq "get";} @methods and not grep({$_ eq "head";} @methods)
!ll&&!rl&&r
1702
MC/DC50%1/2
grep {$_ eq "get";} @methods and not grep({$_ eq "head";} @methods)
grep {$_ eq "get";} @methods not grep({$_ eq "head";} @methods)
Truth table66%2/3
A and not(B)
A= grep {$_ eq "get";} @methodsB= not grep({$_ eq "head";} @methods)
ABresult
0X0
100
111
127 19 $pattern = shift @rest;
128 }
129  
130 raise core_route => "Syntax error, methods should be provided as an ARRAY ref"
131 24 ▶ if grep {$_ eq $pattern} @methods;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ eq $pattern;} @methods 1 23 24
132  
133 23 $self->universal_add($_, $pattern, @rest) for @methods;
134 23 return scalar(@methods);
135 }
136  
137 1443 ▶sub universal_add {
Subroutine universal_add: called CC=2 SCAR=6.9
Pod undocumented
138 1443 my ($self, $method, $pattern, @rest) = @_;
139  
140 1443 my %options;
141 my $code;
142  
143 1443 ▶ if (@rest == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@rest == 1) { } 1431 12 1443
144 1431 $code = $rest[0];
145 }
146 else {
147 12 %options = %{$rest[0]};
148 12 $code = $rest[1];
149 }
150  
151 1443 my %route_args = (
152 method => $method,
153 code => $code,
154 options => \%options,
155 pattern => $pattern,
156 );
157  
158 1443 return $self->register_route(%route_args);
159 }
160  
161 # look for a route in the given array
162 0 ▶sub find_route {
Subroutine find_route: not called CC=3 SCAR=24.8
Pod undocumented
163 0 my ($self, $r, $reg) = @_;
164 0 foreach my $route (@$reg) {
165 0 ▶ return $route if $r->equals($route);
Branch: 1 branch
BranchTrueFalseTotal
if $r->equals($route) 0 0 0
166 }
167 0 return;
168 }
169  
170 194 1;
171  
172 __END__
« lib/Dancer/Template/Abstract.pm ↑ Summary lib/Dancer/Session.pm »