File Coverage

lib/APISchema/Generator/Router/Simple.pm
Criterion Covered Total %
statement 31 31 100.0
branch n/a
condition 5 6 83.3
subroutine 8 8 100.0
pod 0 2 0.0
total 44 47 93.6


line stmt bran cond sub pod time code
1             package APISchema::Generator::Router::Simple;
2 4     4   2791 use strict;
  4         8  
  4         121  
3 4     4   24 use warnings;
  4         10  
  4         112  
4              
5 4     4   1706 use Hash::Merge::Simple qw(merge);
  4         1964  
  4         237  
6 4     4   1686 use Class::Load qw(load_class);
  4         57623  
  4         295  
7             use Class::Accessor::Lite (
8 4         37 new => 1,
9             ro => [qw(router_class)],
10 4     4   32 );
  4         6  
11              
12 4     4   392 use constant ROUTER_CLASS => 'Router::Simple';
  4         8  
  4         921  
13              
14             sub generate_router {
15 17     17 0 120 my ($self, $schema) = @_;
16              
17 17   100     55 my $router_class = $self->router_class // ROUTER_CLASS;
18 17         192 my $router = load_class($router_class)->new;
19              
20 17         11995 $self->inject_routes($schema, $router);
21             }
22              
23             sub inject_routes {
24 18     18 0 139 my ($self, $schema, $router) = @_;
25              
26 18         38 my $router_class = ref $router;
27              
28 18         29 for my $route (@{$schema->get_routes}) {
  18         64  
29 25   100     905 my $option = $route->option // {};
30 25   50     239 $option = merge $option, $option->{$router_class} // {};
31             $router->connect($route->title, $route->route => $route->destination, {
32             method => $route->method,
33 25         425 map { $_ => $option->{$_} } qw(host on_match),
  50         396  
34             });
35             }
36              
37 18         1200 $router;
38             }
39              
40             1;