File Coverage

blib/lib/Path/Dispatcher/Rule/Metadata.pm
Criterion Covered Total %
statement 21 21 100.0
branch 2 2 100.0
condition n/a
subroutine 6 6 100.0
pod n/a
total 29 29 100.0


line stmt bran cond sub pod time code
1             package Path::Dispatcher::Rule::Metadata;
2             # ABSTRACT: match path's metadata
3              
4             our $VERSION = '1.08';
5              
6 31     31   229 use Moo;
  31         73  
  31         463  
7 31     31   10870 use MooX::TypeTiny;
  31         95  
  31         204  
8 31     31   23397 use Type::Utils qw(class_type);
  31         79  
  31         234  
9 31     31   17934 use Types::Standard qw(Str);
  31         73  
  31         235  
10             extends 'Path::Dispatcher::Rule';
11              
12             has field => (
13             is => 'ro',
14             isa => Str,
15             required => 1,
16             );
17              
18             has matcher => (
19             is => 'ro',
20             isa => class_type("Path::Dispatcher::Rule"),
21             required => 1,
22             );
23              
24             sub _match {
25 3     3   7 my $self = shift;
26 3         6 my $path = shift;
27 3         18 my $got = $path->get_metadata($self->field);
28              
29             # wow, offensive.. but powerful
30 3         13 my $metadata_path = $path->clone_path($got);
31 3 100       163 return unless $self->matcher->match($metadata_path);
32              
33             return {
34 2         28 leftover => $path->path,
35             };
36             }
37              
38             __PACKAGE__->meta->make_immutable;
39 31     31   18974 no Moo;
  31         73  
  31         139  
40              
41             1;
42              
43             __END__
44              
45             =pod
46              
47             =encoding UTF-8
48              
49             =head1 NAME
50              
51             Path::Dispatcher::Rule::Metadata - match path's metadata
52              
53             =head1 VERSION
54              
55             version 1.08
56              
57             =head1 SYNOPSIS
58              
59             my $path = Path::Dispatcher::Path->new(
60             path => '/REST/Ticket'
61             metadata => {
62             http_method => 'POST',
63             },
64             );
65              
66             my $rule = Path::Dispatcher::Rule::Metadata->new(
67             field => 'http_method',
68             matcher => Path::Dispatcher::Rule::Eq->new(string => 'POST'),
69             );
70              
71             $rule->run($path);
72              
73             =head1 DESCRIPTION
74              
75             Rules of this class match the metadata portion of a path.
76              
77             =head1 ATTRIBUTES
78              
79             =head2 field
80              
81             The metadata field/key name.
82              
83             =head2 matcher
84              
85             A L<Path::Dispatcher::Rule> object for matching against the value of the field.
86              
87             =head1 SUPPORT
88              
89             Bugs may be submitted through L<the RT bug tracker|https://rt.cpan.org/Public/Dist/Display.html?Name=Path-Dispatcher>
90             (or L<bug-Path-Dispatcher@rt.cpan.org|mailto:bug-Path-Dispatcher@rt.cpan.org>).
91              
92             =head1 AUTHOR
93              
94             Shawn M Moore, C<< <sartak at bestpractical.com> >>
95              
96             =head1 COPYRIGHT AND LICENSE
97              
98             This software is copyright (c) 2020 by Shawn M Moore.
99              
100             This is free software; you can redistribute it and/or modify it under
101             the same terms as the Perl 5 programming language system itself.
102              
103             =cut