File Coverage

blib/lib/Path/Dispatcher/Dispatch.pm
Criterion Covered Total %
statement 37 37 100.0
branch 6 6 100.0
condition 1 3 33.3
subroutine 11 11 100.0
pod 2 5 40.0
total 57 62 91.9


line stmt bran cond sub pod time code
1             package Path::Dispatcher::Dispatch;
2 30     30   118 use Any::Moose;
  30         33  
  30         136  
3 30     30   23763 use Try::Tiny;
  30         28762  
  30         1703  
4              
5 30     30   154 use Path::Dispatcher::Match;
  30         34  
  30         11019  
6              
7             has _matches => (
8             is => 'ro',
9             isa => 'ArrayRef',
10             default => sub { [] },
11             );
12              
13             sub add_match {
14 131     131 0 146 my $self = shift;
15              
16             $_->isa('Path::Dispatcher::Match')
17             or confess "$_ is not a Path::Dispatcher::Match"
18 131   33     579 for @_;
19              
20 131         134 push @{ $self->{_matches} }, @_;
  131         411  
21             }
22              
23 91     91 1 1075 sub matches { @{ shift->{_matches} } }
  91         183  
24 1     1 0 12 sub has_match { scalar @{ shift->{_matches} } }
  1         7  
25 1     1 0 4 sub first_match { shift->{_matches}[0] }
26              
27             # aliases
28             __PACKAGE__->meta->add_method(add_matches => __PACKAGE__->can('add_match'));
29             __PACKAGE__->meta->add_method(has_matches => __PACKAGE__->can('has_match'));
30              
31             sub run {
32 90     90 1 5572 my $self = shift;
33 90         122 my @args = @_;
34 90         169 my @matches = $self->matches;
35 90         99 my @results;
36              
37 90         274 while (my $match = shift @matches) {
38 67         74 my $exception;
39              
40             try {
41 67     67   2209 local $SIG{__DIE__} = 'DEFAULT';
42              
43 67         232 push @results, $match->run(@args);
44              
45             # we always stop after the first match UNLESS they explicitly
46             # ask to continue on to the next rule
47 58         492 die "Path::Dispatcher abort\n";
48             }
49             catch {
50 67     67   666 $exception = $_;
51 67         532 };
52              
53 67 100       654 last if $exception =~ /^Path::Dispatcher abort\n/;
54 8 100       50 next if $exception =~ /^Path::Dispatcher next rule\n/;
55              
56 5         49 die $exception;
57             }
58              
59 85 100       228 return @results if wantarray;
60 78         488 return $results[0];
61             }
62              
63             __PACKAGE__->meta->make_immutable;
64 30     30   182 no Any::Moose;
  30         39  
  30         136  
65              
66             1;
67              
68             __END__