File Coverage

blib/lib/Path/Dispatcher.pm
Criterion Covered Total %
statement 52 52 100.0
branch 2 2 100.0
condition 2 3 66.6
subroutine 14 14 100.0
pod 3 3 100.0
total 73 74 98.6


line stmt bran cond sub pod time code
1             package Path::Dispatcher; # git description: 1.04-27-g5a00454
2             # ABSTRACT: flexible and extensible dispatch
3              
4             our $VERSION = '1.07';
5              
6 31     31   2111373 use Moo;
  31         357766  
  31         192  
7 31     31   47188 use 5.008001;
  31         116  
8              
9             # VERSION
10 31     31   180 use Scalar::Util 'blessed';
  31         60  
  31         1537  
11 31     31   14485 use Path::Dispatcher::Rule;
  31         109  
  31         943  
12 31     31   14573 use Path::Dispatcher::Dispatch;
  31         95  
  31         950  
13 31     31   209 use Path::Dispatcher::Path;
  31         59  
  31         756  
14              
15 31     31   156 use constant dispatch_class => 'Path::Dispatcher::Dispatch';
  31         62  
  31         1834  
16 31     31   187 use constant path_class => 'Path::Dispatcher::Path';
  31         88  
  31         13631  
17              
18             with 'Path::Dispatcher::Role::Rules';
19              
20             sub dispatch {
21 89     89 1 6262 my $self = shift;
22 89         247 my $path = $self->_autobox_path(shift);
23              
24 89         1473 my $dispatch = $self->dispatch_class->new;
25              
26 89         2140 for my $rule ($self->rules) {
27 132         348 $self->_dispatch_rule(
28             rule => $rule,
29             dispatch => $dispatch,
30             path => $path,
31             );
32             }
33              
34 88         221 return $dispatch;
35             }
36              
37             sub _dispatch_rule {
38 132     132   237 my $self = shift;
39 132         437 my %args = @_;
40              
41 132         533 my @matches = $args{rule}->match($args{path});
42              
43 131         515 $args{dispatch}->add_matches(@matches);
44              
45 131         391 return @matches;
46             }
47              
48             sub run {
49 78     78 1 49222 my $self = shift;
50 78         163 my $path = shift;
51              
52 78         221 my $dispatch = $self->dispatch($path);
53              
54 78         272 return $dispatch->run(@_);
55             }
56              
57             sub complete {
58 41     41 1 1609 my $self = shift;
59 41         93 my $path = $self->_autobox_path(shift);
60              
61 41         66 my %seen;
62 41         124 return grep { !$seen{$_}++ } map { $_->complete($path) } $self->rules;
  46         334  
  41         115  
63             }
64              
65             sub _autobox_path {
66 130     130   223 my $self = shift;
67 130         211 my $path = shift;
68              
69 130 100 66     565 unless (blessed($path) && $path->isa('Path::Dispatcher::Path')) {
70 126         2781 $path = $self->path_class->new(
71             path => $path,
72             );
73             }
74              
75 130         7259 return $path;
76             }
77              
78             __PACKAGE__->meta->make_immutable;
79 31     31   254 no Moo;
  31         70  
  31         166  
80              
81             # don't require others to load our subclasses explicitly
82             require Path::Dispatcher::Rule::Alternation;
83             require Path::Dispatcher::Rule::Always;
84             require Path::Dispatcher::Rule::Chain;
85             require Path::Dispatcher::Rule::CodeRef;
86             require Path::Dispatcher::Rule::Dispatch;
87             require Path::Dispatcher::Rule::Empty;
88             require Path::Dispatcher::Rule::Enum;
89             require Path::Dispatcher::Rule::Eq;
90             require Path::Dispatcher::Rule::Intersection;
91             require Path::Dispatcher::Rule::Metadata;
92             require Path::Dispatcher::Rule::Regex;
93             require Path::Dispatcher::Rule::Sequence;
94             require Path::Dispatcher::Rule::Tokens;
95             require Path::Dispatcher::Rule::Under;
96              
97             1;
98              
99             __END__