File Coverage

blib/lib/Thunderhorse/Router/Location.pm
Criterion Covered Total %
statement 55 55 100.0
branch 18 20 90.0
condition 11 13 84.6
subroutine 10 10 100.0
pod 2 3 66.6
total 96 101 95.0


line stmt bran cond sub pod time code
1             package Thunderhorse::Router::Location;
2             $Thunderhorse::Router::Location::VERSION = '0.102';
3 21     21   357 use v5.40;
  21         89  
4 21     21   215 use Mooish::Base -standard;
  21         47  
  21         253  
5              
6 21     21   360333 use Gears::X::Thunderhorse;
  21         52  
  21         1007  
7 21     21   126 use Thunderhorse qw(build_handler adapt_pagi);
  21         42  
  21         37051  
8              
9             extends 'Gears::Router::Location::SigilMatch';
10              
11             has option 'action' => (
12             isa => Str,
13             );
14              
15             has field '_action_re' => (
16             isa => RegexpRef,
17             lazy => 1,
18             );
19              
20             has param 'name' => (
21             isa => Str,
22             lazy => 1,
23             );
24              
25             has param 'to' => (
26             isa => Str | CodeRef,
27             required => 0,
28             );
29              
30             has param 'order' => (
31             isa => Int,
32             default => 0,
33             );
34              
35             has param 'pagi' => (
36             isa => Bool,
37             default => false,
38             );
39              
40             has param 'pagi_middleware' => (
41             isa => CodeRef,
42             required => 0,
43             );
44              
45             has param 'controller' => (
46             isa => InstanceOf ['Thunderhorse::Controller'],
47             );
48              
49             has field 'pagi_app' => (
50             isa => CodeRef,
51             lazy => 1,
52             );
53              
54 169         332 sub BUILD ($self, $)
55 169     169 0 249453 {
  169         276  
56 169 50 66     1017 Gears::X::Thunderhorse->raise('controller has no method ' . $self->to)
57             if defined $self->to && !$self->get_destination;
58              
59             # register the route in the router
60 169         5096 $self->router->_register_location($self->name, $self);
61             }
62              
63             sub _build_action_re ($self)
64 16     16   193 {
  16         31  
  16         26  
65 16         98 my ($scope, $method) = split /\./, $self->action;
66 16 100       80 $scope = $scope eq '*' ? qr{[^.]+} : quotemeta lc $scope;
67              
68 16 100 100     100 if (($method // '*') eq '*') {
    100          
69 11         52 $method = qr{(\.[^.]+)?};
70             }
71             elsif (lc $method eq 'get') {
72 2         5 $method = '[.](get|head)';
73             }
74             else {
75 3         9 $method = quotemeta lc ".$method";
76             }
77              
78 16         1007 return qr{^$scope$method$};
79             }
80              
81             sub _build_name ($self)
82 165     165   1719 {
  165         336  
  165         216  
83 165   100     929 my $action = $self->action // 'any';
84 165         547 my $pattern = $self->pattern;
85 165         770 my $id = $self->router->_get_next_route_id;
86              
87             # autogenerated location name should be readable, but unique
88 165         3683 return "${id}_${action}_${pattern}";
89             }
90              
91             sub _build_pagi_app ($self)
92 92     92   1010 {
  92         184  
  92         166  
93             # TODO: add router sealing when the app is started (prevent it from
94             # changing and breaking assumptions)
95              
96             # this needs to be checked lazily, because we don't know if this is a
97             # bridge in BUILD
98 92 50 66     542 Gears::X::Thunderhorse->raise('PAGI apps cannot be bridges')
99             if $self->pagi && $self->is_bridge;
100              
101 92 100       674 my $pagi = $self->pagi
102             ? adapt_pagi($self->get_destination)
103             : build_handler($self->controller, $self->get_destination)
104             ;
105              
106 92 100       464 if (my $mw = $self->pagi_middleware) {
107 3         18 $pagi = $mw->($pagi);
108             }
109              
110 92         7105 return $pagi;
111             }
112              
113             sub get_destination ($self)
114 245     245 1 406 {
  245         407  
  245         440  
115 245         710 my $to = $self->to;
116 245 100       776 return undef unless defined $to;
117              
118 243 100       1145 return $to if ref $to eq 'CODE';
119 123         807 return $self->controller->can($self->to);
120             }
121              
122 790         1427 sub compare ($self, $path, $action)
  790         1353  
123 790     790 1 179722 {
  790         1141  
  790         1065  
124 790 100 100     8593 return undef if $self->has_action && $action !~ $self->_action_re;
125 667         4473 return $self->SUPER::compare($path);
126             }
127              
128             __END__