File Coverage

blib/lib/Path/Dispatcher/Rule/Enum.pm
Criterion Covered Total %
statement 27 46 58.7
branch 6 18 33.3
condition n/a
subroutine 5 6 83.3
pod 0 1 0.0
total 38 71 53.5


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.07';
5              
6 31     31   222 use Moo;
  31         67  
  31         182  
7 31     31   10038 use Types::Standard qw(Str ArrayRef Bool);
  31         109  
  31         286  
8              
9             extends 'Path::Dispatcher::Rule';
10              
11             has enum => (
12             is => 'ro',
13             isa => ArrayRef[Str],
14             required => 1,
15             );
16              
17             has case_sensitive => (
18             is => 'ro',
19             isa => Bool,
20             default => 1,
21             );
22              
23             sub _match {
24 41     41   61 my $self = shift;
25 41         65 my $path = shift;
26              
27 41 50       121 if ($self->case_sensitive) {
28 41         67 for my $value (@{ $self->enum }) {
  41         96  
29 116 100       328 return {} if $path->path eq $value;
30             }
31             }
32             else {
33 0         0 for my $value (@{ $self->enum }) {
  0         0  
34 0 0       0 return {} if lc($path->path) eq lc($value);
35             }
36             }
37              
38 17         36 return;
39             }
40              
41             sub _prefix_match {
42 0     0   0 my $self = shift;
43 0         0 my $path = shift;
44              
45 0         0 my $truncated = substr($path->path, 0, length($self->string));
46              
47 0 0       0 if ($self->case_sensitive) {
48 0         0 for my $value (@{ $self->enum }) {
  0         0  
49 0 0       0 next unless $truncated eq $value;
50              
51             return {
52 0         0 leftover => substr($path->path, length($self->string)),
53             };
54             }
55             }
56             else {
57 0         0 for my $value (@{ $self->enum }) {
  0         0  
58 0 0       0 next unless lc($truncated) eq lc($value);
59              
60             return {
61 0         0 leftover => substr($path->path, length($self->string)),
62             };
63             }
64             }
65              
66 0         0 return;
67             }
68              
69             sub complete {
70 18     18 0 877 my $self = shift;
71 18         41 my $path = shift->path;
72 18         26 my @completions;
73              
74             # by convention, complete does include the path itself if it
75             # is a complete match
76 18         30 my @enum = grep { length($path) < length($_) } @{ $self->enum };
  72         157  
  18         40  
77              
78 18 50       45 if ($self->case_sensitive) {
79 18         34 for my $value (@enum) {
80 72         115 my $partial = substr($value, 0, length($path));
81 72 100       152 push @completions, $value if $partial eq $path;
82             }
83             }
84             else {
85 0         0 for my $value (@enum) {
86 0         0 my $partial = substr($value, 0, length($path));
87 0 0       0 push @completions, $value if lc($partial) eq lc($path);
88             }
89             }
90              
91 18         79 return @completions;
92             }
93              
94             __PACKAGE__->meta->make_immutable;
95 31     31   39867 no Moo;
  31         84  
  31         186  
96              
97             1;
98              
99             __END__