File Coverage

blib/lib/Path/Dispatcher.pm
Criterion Covered Total %
statement 50 50 100.0
branch 2 2 100.0
condition 2 3 66.6
subroutine 13 13 100.0
pod 3 3 100.0
total 70 71 98.5


line stmt bran cond sub pod time code
1             package Path::Dispatcher;
2 30     30   537976 use Any::Moose;
  30         763451  
  30         193  
3 30     30   14951 use 5.008001;
  30         90  
  30         1361  
4              
5             our $VERSION = '1.06'; # VERSION
6              
7 30     30   11708 use Path::Dispatcher::Rule;
  30         93  
  30         1003  
8 30     30   10642 use Path::Dispatcher::Dispatch;
  30         88  
  30         882  
9 30     30   171 use Path::Dispatcher::Path;
  30         37  
  30         678  
10              
11 30     30   116 use constant dispatch_class => 'Path::Dispatcher::Dispatch';
  30         35  
  30         1473  
12 30     30   122 use constant path_class => 'Path::Dispatcher::Path';
  30         39  
  30         8899  
13              
14             with 'Path::Dispatcher::Role::Rules';
15              
16             sub dispatch {
17 89     89 1 547 my $self = shift;
18 89         185 my $path = $self->_autobox_path(shift);
19              
20 89         756 my $dispatch = $self->dispatch_class->new;
21              
22 89         295 for my $rule ($self->rules) {
23 132         269 $self->_dispatch_rule(
24             rule => $rule,
25             dispatch => $dispatch,
26             path => $path,
27             );
28             }
29              
30 88         200 return $dispatch;
31             }
32              
33             sub _dispatch_rule {
34 132     132   124 my $self = shift;
35 132         299 my %args = @_;
36              
37 132         476 my @matches = $args{rule}->match($args{path});
38              
39 131         416 $args{dispatch}->add_matches(@matches);
40              
41 131         294 return @matches;
42             }
43              
44             sub run {
45 78     78 1 15989 my $self = shift;
46 78         108 my $path = shift;
47              
48 78         158 my $dispatch = $self->dispatch($path);
49              
50 78         241 return $dispatch->run(@_);
51             }
52              
53             sub complete {
54 41     41 1 1515 my $self = shift;
55 41         91 my $path = $self->_autobox_path(shift);
56              
57 41         41 my %seen;
58 41         94 return grep { !$seen{$_}++ } map { $_->complete($path) } $self->rules;
  46         247  
  41         97  
59             }
60              
61             sub _autobox_path {
62 130     130   167 my $self = shift;
63 130         148 my $path = shift;
64              
65 130 100 66     544 unless (blessed($path) && $path->isa('Path::Dispatcher::Path')) {
66 126         885 $path = $self->path_class->new(
67             path => $path,
68             );
69             }
70              
71 130         278 return $path;
72             }
73              
74             __PACKAGE__->meta->make_immutable;
75 30     30   192 no Any::Moose;
  30         49  
  30         421  
76              
77             1;
78              
79             __END__