blib/lib/Dancer/Route/Registry.pm

filter: Statementstmt 95.4% 83 / 87 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.7% 129 / 147 CC 19 SCAR 13.7
CC
19
Cov
92%
find_route
12.0
any_add
6.0
add_route
4.0
CRAP 3.9
« blib/lib/Dancer/Template/Abstract.pm ↑ Summary blib/lib/Dancer/Session.pm »
1 package Dancer::Route::Registry;
2 183 our $AUTHORITY = 'cpan:SUKRIA';
3 # ABSTRACT: Route registry for Dancer
4 183 $Dancer::Route::Registry::VERSION = '1.3513';
5 184 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 184 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 184 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 184 ▶use Dancer::Route;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 183 ▶use base 'Dancer::Object';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 183 ▶use Dancer::Logger;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 183 ▶use Dancer::Exception qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 183 Dancer::Route::Registry->attributes(qw( id ));
14  
15 183 my $id = 1;
16  
17 sub init {
18 113 ▶ my ($self) = @_;
Subroutine init: called CC=2 SCAR=7.0
Pod documented
19  
20 113 ▶ unless (defined $self->{id}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"id"}) 113 0 113
21 113 $self->id($id++);
22 }
23 113 $self->{routes} = {};
24  
25 113 return $self;
26 }
27  
28 sub is_empty {
29 2 ▶ my ($self) = @_;
Subroutine is_empty: called CC=3 SCAR=11.1
Pod undocumented
30 2 for my $method ( keys %{ $self->routes } ) {
31 1 ▶ return 0 if $self->routes($method);
Branch: 1 branch
BranchTrueFalseTotal
if $self->routes($method) 1 0 1
32 }
33 1 return 1;
34 }
35  
36 # replace any ':foo' by '(.+)' and stores all the named
37 # matches defined in $REG->{route_params}{$route}
38 sub routes {
39 3238 ▶ my ($self, $method) = @_;
Subroutine routes: called CC=3 SCAR=11.0
Pod undocumented
40  
41 3238 ▶ if ($method) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method) { } 3236 2 3238
42 3236 my $route = $self->{routes}{$method};
43 3236 ▶ return $route ? $route : [];
Branch: 1 branch
BranchTrueFalseTotal
$route ? : 3228 8 3236
44 }
45 else {
46 2 return $self->{routes};
47 }
48 }
49  
50 sub add_route {
51 1326 ▶ my ($self, $route) = @_;
Subroutine add_route: called CC=4 SCAR=13.9
Pod undocumented
52 1326 ▶ $self->{routes}{$route->method} ||= [];
Condition100%2/2
$self->{"routes"}{$route->method} ||= []
l!l
1068258
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
53 1326 my @registered = @{$self->routes($route->method)};
54 1326 my $last = $registered[-1];
55 1326 ▶ $route->set_previous($last) if defined $last;
Branch: 1 branch
BranchTrueFalseTotal
if defined $last 1067 259 1326
56  
57 # if the route have options, we store the route at the beginning
58 # of the routes. This way, we can have the following routes:
59 # get '/' => sub {} and ajax '/' => sub {}
60 # and the user won't have to declare the ajax route before the get
61 1326 ▶ if (keys %{$route->{options}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %{$$route{'options'};}) { } 11 1315 1326
62 11 unshift @{$self->routes($route->method)}, $route;
63 }
64 else {
65 1315 push @{$self->routes($route->method)}, $route;
66 }
67 1326 return $route;
68 }
69  
70 # sugar for add_route
71  
72 sub register_route {
73 1327 ▶ my ($self, %args) = @_;
Subroutine register_route: called CC=3 SCAR=11.0
Pod undocumented
74  
75 # look if the caller (where the route is declared) exists as a Dancer::App
76 # object
77 1327 my ($package) = caller(2);
78 1327 ▶ if ($package && Dancer::App->app_exists($package)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($package and 'Dancer::App'->app_exists($package)) { } 641 686 1327
Condition100%3/3
$package and "Dancer::App"->app_exists($package)
!ll&&!rl&&r
1685641
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
79 641 my $app = Dancer::App->get($package);
80 641 my $route = Dancer::Route->new(prefix => $app->prefix, %args);
81 640 return $app->registry->add_route($route);
82 }
83 else {
84  
85 # FIXME maybe this code is useless, drop it later if so
86 686 my $route = Dancer::Route->new(%args);
87 686 return $self->add_route($route);
88 }
89 }
90  
91 # sugar for Dancer.pm
92 # class, any, ARRAY(0x9864818), '/path', CODE(0x990ac88)
93 # or
94 # class, any, '/path', CODE(0x990ac88)
95 sub any_add {
96 23 ▶ my ($self, $pattern, @rest) = @_;
Subroutine any_add: called CC=6 SCAR=17.9
Pod undocumented
97  
98 23 my @methods = qw(get post put patch delete options head);
99  
100 23 ▶ if (ref($pattern) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $pattern eq "ARRAY") 18 5 23
101 18 @methods = @$pattern;
102 # 'get' defaults to 'get' and 'head'
103 36 push @methods, 'head' if ((grep { $_ eq 'get' } @methods) and
104 18 ▶ not (grep { $_ eq 'head' } @methods));
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ eq "get";} @methods and not grep({$_ eq "head";} @methods) 2 16 18
Condition66%2/3
grep {$_ eq "get";} @methods and not grep({$_ eq "head";} @methods)
!ll&&!rl&&r
1602
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
105 18 $pattern = shift @rest;
106 }
107  
108 raise core_route => "Syntax error, methods should be provided as an ARRAY ref"
109 23 ▶ if grep {$_ eq $pattern} @methods;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ eq $pattern;} @methods 1 22 23
110  
111 22 $self->universal_add($_, $pattern, @rest) for @methods;
112 22 return scalar(@methods);
113 }
114  
115 sub universal_add {
116 1327 ▶ my ($self, $method, $pattern, @rest) = @_;
Subroutine universal_add: called CC=2 SCAR=6.9
Pod undocumented
117  
118 1327 my %options;
119 my $code;
120  
121 1327 ▶ if (@rest == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@rest == 1) { } 1315 12 1327
122 1315 $code = $rest[0];
123 }
124 else {
125 12 %options = %{$rest[0]};
126 12 $code = $rest[1];
127 }
128  
129 1327 my %route_args = (
130 method => $method,
131 code => $code,
132 options => \%options,
133 pattern => $pattern,
134 );
135  
136 1327 return $self->register_route(%route_args);
137 }
138  
139 # look for a route in the given array
140 sub find_route {
141 0 ▶ my ($self, $r, $reg) = @_;
Subroutine find_route: not called CC=3 SCAR=24.8
Pod undocumented
142 0 foreach my $route (@$reg) {
143 0 ▶ return $route if $r->equals($route);
Branch: 1 branch
BranchTrueFalseTotal
if $r->equals($route) 0 0 0
144 }
145 0 return;
146 }
147  
148 183 1;
149  
150 __END__
« blib/lib/Dancer/Template/Abstract.pm ↑ Summary blib/lib/Dancer/Session.pm »