File Coverage

blib/lib/REST/Application/Routes.pm
Criterion Covered Total %
statement 36 40 90.0
branch 6 6 100.0
condition 3 3 100.0
subroutine 7 9 77.7
pod 3 4 75.0
total 55 62 88.7


line stmt bran cond sub pod time code
1             package REST::Application::Routes;
2 1     1   24029 use strict;
  1         2  
  1         27  
3 1     1   5 use warnings;
  1         2  
  1         24  
4 1     1   4 use base 'REST::Application';
  1         2  
  1         859  
5              
6             our $VERSION = $REST::Application::VERSION;
7              
8             sub loadResource {
9 29     29 1 107 my ($self, $path, @extraArgs) = @_;
10 29   100     112 $path ||= $self->getMatchText();
11 29     4   2463 my $handler = sub { $self->defaultResourceHandler(@_) };
  4         21  
12 29         41 my %vars;
13              
14             # Loop through the keys of the hash returned by resourceHooks(). Each of
15             # the keys is a URI template, see if the current path info matches that
16             # template. Save the parent matches for passing into the handler.
17 29         34 for my $template (keys %{ $self->resourceHooks() }) {
  29         79  
18 156 100       515 my $regex = join "\\/",
19 42         609 map {/^:/ ? '([^\/]+)' : quotemeta $_}
20             split m{/}, $template;
21 42         224 $regex = "^(?:$regex)\\/?\$";
22 42 100       133 if ($self->checkMatch($path, $regex)) {
23 25         42 $self->{__last_match_pattern} = $template;
24 25         59 %vars = $self->getTemplateVars($template);
25 25         99 $handler = $self->_getHandlerFromHook($template);
26 25         86 last;
27             }
28             }
29              
30 29         134 return $self->callHandler($handler, \%vars, @extraArgs);
31             }
32              
33             sub getHandlerArgs {
34 29     29 0 45 my ($self, @extraArgs) = @_;
35 29         89 my @args = ($self, @extraArgs, $self->extraHandlerArgs());
36              
37             # Don't make $self the first argument if the handler is a method on $self,
38             # because in that case it'd be redundant. Also see _getHandlerFromHook().
39 29 100       68 shift @args if $self->{__handlerIsOurMethod};
40              
41 29         87 return @args;
42             }
43              
44             sub _get_template_vars {
45 0     0   0 my $self = shift;
46 0         0 return $self->getTemplateVars(@_);
47             }
48              
49             sub getTemplateVars {
50 25     25 1 39 my ($self, $route) = @_;
51 25         88 my @matches = $self->_getLastRegexMatches();
52 25         144 my @vars = map {s/^://; $_} grep /^:/, split m{/}, $route;
  30         75  
  30         70  
53 25         68 return map { $vars[$_] => $matches[$_] } (0 .. scalar(@matches)-1);
  30         122  
54             }
55              
56             sub getLastMatchTemplate {
57 0     0 1   my $self = shift;
58 0           return $self->getLastMatchPattern();
59             }
60              
61             1;
62             __END__