File Coverage

blib/lib/Path/Dispatcher/Dispatch.pm
Criterion Covered Total %
statement 45 45 100.0
branch 6 6 100.0
condition 1 3 33.3
subroutine 15 15 100.0
pod 2 7 28.5
total 69 76 90.7


line stmt bran cond sub pod time code
1             package Path::Dispatcher::Dispatch;
2             # ABSTRACT: a list of matches
3              
4             our $VERSION = '1.07';
5              
6 31     31   243 use Moo;
  31         64  
  31         198  
7 31     31   24393 use Try::Tiny;
  31         35035  
  31         1920  
8 31     31   221 use Carp qw(confess);
  31         67  
  31         1387  
9 31     31   186 use Types::Standard qw(ArrayRef);
  31         65  
  31         258  
10              
11 31     31   17996 use Path::Dispatcher::Match;
  31         67  
  31         15273  
12              
13             has _matches => (
14             is => 'ro',
15             isa => ArrayRef,
16             default => sub { [] },
17             );
18              
19             sub add_match {
20 131     131 0 243 my $self = shift;
21              
22             $_->isa('Path::Dispatcher::Match')
23             or confess "$_ is not a Path::Dispatcher::Match"
24 131   33     618 for @_;
25              
26 131         210 push @{ $self->{_matches} }, @_;
  131         440  
27             }
28              
29 91     91 1 1345 sub matches { @{ shift->{_matches} } }
  91         280  
30 1     1 0 2 sub has_match { scalar @{ shift->{_matches} } }
  1         10  
31 1     1 0 4 sub first_match { shift->{_matches}[0] }
32              
33             # aliases
34 131     131 0 413 sub add_matches { goto \&add_match }
35 1     1 0 6 sub has_matches { goto \&has_match }
36              
37             sub run {
38 90     90 1 6537 my $self = shift;
39 90         224 my @args = @_;
40 90         230 my @matches = $self->matches;
41 90         157 my @results;
42              
43 90         274 while (my $match = shift @matches) {
44 67         152 my $exception;
45              
46             try {
47 67     67   3749 local $SIG{__DIE__} = 'DEFAULT';
48              
49 67         279 push @results, $match->run(@args);
50              
51             # we always stop after the first match UNLESS they explicitly
52             # ask to continue on to the next rule
53 58         680 die "Path::Dispatcher abort\n";
54             }
55             catch {
56 67     67   1180 $exception = $_;
57 67         1075 };
58              
59 67 100       888 last if $exception =~ /^Path::Dispatcher abort\n/;
60 8 100       41 next if $exception =~ /^Path::Dispatcher next rule\n/;
61              
62 5         89 die $exception;
63             }
64              
65 85 100       296 return @results if wantarray;
66 78         519 return $results[0];
67             }
68              
69             __PACKAGE__->meta->make_immutable;
70              
71 31     31   250 no Moo;
  31         64  
  31         185  
72              
73             1;
74              
75             __END__