File Coverage

blib/lib/Path/Dispatcher/Rule/CodeRef.pm
Criterion Covered Total %
statement 16 16 100.0
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 21 21 100.0


line stmt bran cond sub pod time code
1             package Path::Dispatcher::Rule::CodeRef;
2             # ABSTRACT: predicate is any subroutine
3              
4             our $VERSION = '1.08';
5              
6 31     31   222 use Moo;
  31         64  
  31         188  
7 31     31   10858 use MooX::TypeTiny;
  31         87  
  31         229  
8 31     31   21972 use Types::Standard qw(CodeRef);
  31         77  
  31         244  
9             extends 'Path::Dispatcher::Rule';
10              
11             has matcher => (
12             is => 'ro',
13             isa => CodeRef,
14             required => 1,
15             );
16              
17             sub _match {
18 3     3   7 my $self = shift;
19 3         6 my $path = shift;
20              
21 3         7 local $_ = $path;
22 3         16 return $self->matcher->($path);
23             }
24              
25             __PACKAGE__->meta->make_immutable;
26 31     31   19111 no Moo;
  31         76  
  31         171  
27              
28             1;
29              
30             __END__
31              
32             =pod
33              
34             =encoding UTF-8
35              
36             =head1 NAME
37              
38             Path::Dispatcher::Rule::CodeRef - predicate is any subroutine
39              
40             =head1 VERSION
41              
42             version 1.08
43              
44             =head1 SYNOPSIS
45              
46             my $rule = Path::Dispatcher::Rule::CodeRef->new(
47             matcher => sub { time % 2 },
48             block => sub { warn "Odd time!" },
49             );
50              
51             my $undef = $rule->match("foo"); # even time; no match :)
52              
53             my $match = $rule->match("foo"); # odd time; creates a Path::Dispatcher::Match
54              
55             $rule->run; # warns "Odd time!"
56              
57             =head1 DESCRIPTION
58              
59             Rules of this class can match arbitrarily complex values. This should be used
60             only when there is no other recourse, because there's no way we can inspect
61             how things match.
62              
63             You're much better off creating a custom subclass of L<Path::Dispatcher::Rule>
64             if at all possible.
65              
66             =head1 ATTRIBUTES
67              
68             =head2 matcher
69              
70             A coderef that returns C<undef> if there's no match, otherwise a list of
71             strings (the results).
72              
73             The coderef receives the path object as its argument, and the path string as
74             C<$_>.
75              
76             =head1 SUPPORT
77              
78             Bugs may be submitted through L<the RT bug tracker|https://rt.cpan.org/Public/Dist/Display.html?Name=Path-Dispatcher>
79             (or L<bug-Path-Dispatcher@rt.cpan.org|mailto:bug-Path-Dispatcher@rt.cpan.org>).
80              
81             =head1 AUTHOR
82              
83             Shawn M Moore, C<< <sartak at bestpractical.com> >>
84              
85             =head1 COPYRIGHT AND LICENSE
86              
87             This software is copyright (c) 2020 by Shawn M Moore.
88              
89             This is free software; you can redistribute it and/or modify it under
90             the same terms as the Perl 5 programming language system itself.
91              
92             =cut