File Coverage

blib/lib/Path/Dispatcher/Rule/Enum.pm
Criterion Covered Total %
statement 30 49 61.2
branch 6 18 33.3
condition n/a
subroutine 6 7 85.7
pod 0 1 0.0
total 42 75 56.0


line stmt bran cond sub pod time code
1             package Path::Dispatcher::Rule::Enum;
2             # ABSTRACT: one of a list of strings must match
3              
4             our $VERSION = '1.08';
5              
6 31     31   226 use Moo;
  31         80  
  31         179  
7 31     31   10728 use MooX::TypeTiny;
  31         99  
  31         212  
8 31     31   22539 use Types::Standard qw(Str ArrayRef Bool);
  31         83  
  31         236  
9              
10             extends 'Path::Dispatcher::Rule';
11              
12             has enum => (
13             is => 'ro',
14             isa => ArrayRef[Str],
15             required => 1,
16             );
17              
18             has case_sensitive => (
19             is => 'ro',
20             isa => Bool,
21             default => 1,
22             );
23              
24             sub _match {
25 41     41   70 my $self = shift;
26 41         54 my $path = shift;
27              
28 41 50       99 if ($self->case_sensitive) {
29 41         62 for my $value (@{ $self->enum }) {
  41         107  
30 116 100       296 return {} if $path->path eq $value;
31             }
32             }
33             else {
34 0         0 for my $value (@{ $self->enum }) {
  0         0  
35 0 0       0 return {} if lc($path->path) eq lc($value);
36             }
37             }
38              
39 17         43 return;
40             }
41              
42             sub _prefix_match {
43 0     0   0 my $self = shift;
44 0         0 my $path = shift;
45              
46 0         0 my $truncated = substr($path->path, 0, length($self->string));
47              
48 0 0       0 if ($self->case_sensitive) {
49 0         0 for my $value (@{ $self->enum }) {
  0         0  
50 0 0       0 next unless $truncated eq $value;
51              
52             return {
53 0         0 leftover => substr($path->path, length($self->string)),
54             };
55             }
56             }
57             else {
58 0         0 for my $value (@{ $self->enum }) {
  0         0  
59 0 0       0 next unless lc($truncated) eq lc($value);
60              
61             return {
62 0         0 leftover => substr($path->path, length($self->string)),
63             };
64             }
65             }
66              
67 0         0 return;
68             }
69              
70             sub complete {
71 18     18 0 692 my $self = shift;
72 18         54 my $path = shift->path;
73 18         31 my @completions;
74              
75             # by convention, complete does include the path itself if it
76             # is a complete match
77 18         27 my @enum = grep { length($path) < length($_) } @{ $self->enum };
  72         160  
  18         58  
78              
79 18 50       56 if ($self->case_sensitive) {
80 18         43 for my $value (@enum) {
81 72         116 my $partial = substr($value, 0, length($path));
82 72 100       152 push @completions, $value if $partial eq $path;
83             }
84             }
85             else {
86 0         0 for my $value (@enum) {
87 0         0 my $partial = substr($value, 0, length($path));
88 0 0       0 push @completions, $value if lc($partial) eq lc($path);
89             }
90             }
91              
92 18         78 return @completions;
93             }
94              
95             __PACKAGE__->meta->make_immutable;
96 31     31   40832 no Moo;
  31         78  
  31         176  
97              
98             1;
99              
100             __END__
101              
102             =pod
103              
104             =encoding UTF-8
105              
106             =head1 NAME
107              
108             Path::Dispatcher::Rule::Enum - one of a list of strings must match
109              
110             =head1 VERSION
111              
112             version 1.08
113              
114             =head1 SYNOPSIS
115              
116             my $rule = Path::Dispatcher::Rule::Enum->new(
117             enum => [qw(perl ruby python php)],
118             block => sub { warn "I love " . shift->pos(1) },
119             );
120              
121             =head1 DESCRIPTION
122              
123             Rules of this class check whether the path matches any of its
124             L</enum> strings.
125              
126             =head1 ATTRIBUTES
127              
128             =head2 enum
129              
130             =head2 case_sensitive
131              
132             =head1 SUPPORT
133              
134             Bugs may be submitted through L<the RT bug tracker|https://rt.cpan.org/Public/Dist/Display.html?Name=Path-Dispatcher>
135             (or L<bug-Path-Dispatcher@rt.cpan.org|mailto:bug-Path-Dispatcher@rt.cpan.org>).
136              
137             =head1 AUTHOR
138              
139             Shawn M Moore, C<< <sartak at bestpractical.com> >>
140              
141             =head1 COPYRIGHT AND LICENSE
142              
143             This software is copyright (c) 2020 by Shawn M Moore.
144              
145             This is free software; you can redistribute it and/or modify it under
146             the same terms as the Perl 5 programming language system itself.
147              
148             =cut