File Coverage

blib/lib/Path/Dispatcher/Match.pm
Criterion Covered Total %
statement 29 32 90.6
branch 4 4 100.0
condition n/a
subroutine 9 10 90.0
pod 2 3 66.6
total 44 49 89.8


line stmt bran cond sub pod time code
1             package Path::Dispatcher::Match;
2             # ABSTRACT: the result of a successful rule match
3              
4             our $VERSION = '1.08';
5              
6 32     32   253 use Moo;
  32         77  
  32         308  
7 32     32   13562 use MooX::TypeTiny;
  32         84  
  32         263  
8 32     32   38521 use Type::Utils qw(class_type);
  32         168469  
  32         317  
9 32     32   18255 use Types::Standard qw(Str ArrayRef HashRef Undef);
  32         80  
  32         155  
10 32     32   47650 use Path::Dispatcher::Path;
  32         130  
  32         1249  
11 32     32   273 use Path::Dispatcher::Rule;
  32         73  
  32         10709  
12              
13             has path => (
14             is => 'ro',
15             isa => class_type('Path::Dispatcher::Path'),
16             required => 1,
17             );
18              
19             has leftover => (
20             is => 'ro',
21             isa => Str,
22             );
23              
24             has rule => (
25             is => 'ro',
26             isa => class_type('Path::Dispatcher::Rule'),
27             required => 1,
28             handles => ['payload'],
29             );
30              
31             has positional_captures => (
32             is => 'ro',
33             isa => ArrayRef[Str|Undef],
34             default => sub { [] },
35             );
36              
37             has named_captures => (
38             is => 'ro',
39             isa => HashRef[Str|Undef],
40             default => sub { {} },
41             );
42              
43             has parent => (
44             is => 'ro',
45             isa => class_type('Path::Dispatcher::Match'),
46             predicate => 'has_parent',
47             );
48              
49             sub run {
50 69     69 1 1445 my $self = shift;
51              
52 69         243 local $_ = $self->path;
53 69         407 return scalar $self->rule->run($self, @_);
54             }
55              
56             sub pos {
57 9     9 1 1742 my $self = shift;
58 9         13 my $index = shift;
59              
60 9 100       31 return undef if $index == 0;
61              
62 8 100       19 $index-- if $index > 0;
63              
64 8         45 return $self->positional_captures->[$index];
65             }
66              
67             sub named {
68 0     0 0   my $self = shift;
69 0           my $key = shift;
70 0           return $self->named_captures->{$key};
71             }
72              
73             __PACKAGE__->meta->make_immutable;
74 32     32   260 no Moo;
  32         73  
  32         233  
75              
76             1;
77              
78             __END__
79              
80             =pod
81              
82             =encoding UTF-8
83              
84             =head1 NAME
85              
86             Path::Dispatcher::Match - the result of a successful rule match
87              
88             =head1 VERSION
89              
90             version 1.08
91              
92             =head1 SYNOPSIS
93              
94             my $rule = Path::Dispatcher::Rule::Tokens->new(
95             tokens => [ 'attack', qr/^\w+$/ ],
96             block => sub {
97             my $match = shift;
98             attack($match->pos(2))
99             },
100             );
101              
102             my $match = $rule->match("attack dragon");
103              
104             # introspection
105             $match->path # "attack dragon"
106             $match->leftover # empty string (populated with prefix rules)
107             $match->rule # $rule
108             $match->positional_captures # ["attack", "dragon"] (decided by the rule)
109             $match->pos(1) # "attack"
110             $match->pos(2) # "dragon"
111              
112             $match->run # attack("dragon")
113              
114             =head1 DESCRIPTION
115              
116             If a L<Path::Dispatcher::Rule> successfully matches a path, it creates one or
117             more C<Path::Dispatcher::Match> objects.
118              
119             =head1 ATTRIBUTES
120              
121             =head2 rule
122              
123             The L<Path::Dispatcher::Rule> that created this match.
124              
125             =head2 path
126              
127             The path that the rule matched.
128              
129             =head2 leftover
130              
131             The rest of the path. This is populated when the rule matches a prefix of the
132             path.
133              
134             =head2 positional_captures
135              
136             Any positional captures generated by the rule. For example,
137             L<Path::Dispatcher::Rule::Regex> populates this with the capture variables.
138              
139             =head2 named_captures
140              
141             Any named captures generated by the rule. For example,
142             L<Path::Dispatcher::Rule::Regex> populates this with named captures.
143              
144             =head2 parent
145              
146             The parent match object, if applicable (which may be set if this match is the
147             child of, for exampl, a L<Path::Dispatcher::Rule::Under> prefix)
148              
149             =head1 METHODS
150              
151             =head2 run
152              
153             Executes the rule's codeblock with the same arguments.
154              
155             =head2 pos($i)
156              
157             Returns the C<$i>th positional capture, 1-indexed.
158              
159             =head1 SUPPORT
160              
161             Bugs may be submitted through L<the RT bug tracker|https://rt.cpan.org/Public/Dist/Display.html?Name=Path-Dispatcher>
162             (or L<bug-Path-Dispatcher@rt.cpan.org|mailto:bug-Path-Dispatcher@rt.cpan.org>).
163              
164             =head1 AUTHOR
165              
166             Shawn M Moore, C<< <sartak at bestpractical.com> >>
167              
168             =head1 COPYRIGHT AND LICENSE
169              
170             This software is copyright (c) 2020 by Shawn M Moore.
171              
172             This is free software; you can redistribute it and/or modify it under
173             the same terms as the Perl 5 programming language system itself.
174              
175             =cut