File Coverage

blib/lib/Path/Dispatcher/Rule.pm
Criterion Covered Total %
statement 32 35 91.4
branch 12 12 100.0
condition n/a
subroutine 7 10 70.0
pod 3 5 60.0
total 54 62 87.1


line stmt bran cond sub pod time code
1             package Path::Dispatcher::Rule;
2 32     32   19241 use Any::Moose;
  32         29866  
  32         147  
3              
4 32     32   22466 use Path::Dispatcher::Match;
  32         61  
  32         1104  
5              
6 32     32   184 use constant match_class => "Path::Dispatcher::Match";
  32         39  
  32         10595  
7              
8             has payload => (
9             is => 'ro',
10             predicate => 'has_payload',
11             );
12              
13             has prefix => (
14             is => 'ro',
15             isa => 'Bool',
16             default => 0,
17             );
18              
19             # support for deprecated "block" attribute
20 0     0 1 0 sub block { shift->payload(@_) }
21 0     0 0 0 sub has_block { shift->has_payload(@_) }
22             override BUILDARGS => sub {
23             my $self = shift;
24             my $args = super;
25             $args->{payload} ||= delete $args->{block};
26             return $args;
27             };
28              
29             sub match {
30 381     381 1 1923 my $self = shift;
31 381         323 my $path = shift;
32 381         426 my %args = @_;
33              
34 381         283 my $result;
35              
36 381 100       912 if ($self->prefix) {
37 42         88 $result = $self->_prefix_match($path);
38             }
39             else {
40 339         759 $result = $self->_match($path);
41             }
42              
43 381 100       929 return if !$result;
44              
45 241 100       479 if (ref($result) ne 'HASH') {
46 1         21 die "Results returned from _match must be a hashref";
47             }
48              
49 240 100       2901 my $match = $self->match_class->new(
50             path => $path,
51             rule => $self,
52 240         739 %{ $args{extra_constructor_args} || {} },
53             %$result,
54             );
55              
56 240         2177 return $match;
57             }
58              
59             sub complete {
60 0     0 0 0 return (); # no completions
61             }
62              
63             sub _prefix_match {
64 42     42   34 my $self = shift;
65 42         112 return $self->_match(@_);
66             }
67              
68             sub run {
69 71     71 1 1232 my $self = shift;
70              
71 71         200 my $payload = $self->payload;
72              
73 71 100       190 die "No codeblock to run" if !$payload;
74 69 100       206 die "Payload is not a coderef" if ref($payload) ne 'CODE';
75              
76 67         200 $self->payload->(@_);
77             }
78              
79             __PACKAGE__->meta->make_immutable;
80 32     32   167 no Any::Moose;
  32         41  
  32         164  
81              
82             # don't require others to load our subclasses explicitly
83             require Path::Dispatcher::Rule::Alternation;
84             require Path::Dispatcher::Rule::Always;
85             require Path::Dispatcher::Rule::Chain;
86             require Path::Dispatcher::Rule::CodeRef;
87             require Path::Dispatcher::Rule::Dispatch;
88             require Path::Dispatcher::Rule::Empty;
89             require Path::Dispatcher::Rule::Enum;
90             require Path::Dispatcher::Rule::Eq;
91             require Path::Dispatcher::Rule::Intersection;
92             require Path::Dispatcher::Rule::Metadata;
93             require Path::Dispatcher::Rule::Regex;
94             require Path::Dispatcher::Rule::Sequence;
95             require Path::Dispatcher::Rule::Tokens;
96             require Path::Dispatcher::Rule::Under;
97              
98             1;
99              
100             __END__