File Coverage

blib/lib/Path/Dispatcher/Rule/Enum.pm
Criterion Covered Total %
statement 24 43 55.8
branch 6 18 33.3
condition n/a
subroutine 4 5 80.0
pod 0 1 0.0
total 34 67 50.7


line stmt bran cond sub pod time code
1             package Path::Dispatcher::Rule::Enum;
2 32     32   129 use Any::Moose;
  32         772  
  32         789  
3             extends 'Path::Dispatcher::Rule';
4              
5             has enum => (
6             is => 'ro',
7             isa => 'ArrayRef[Str]',
8             required => 1,
9             );
10              
11             has case_sensitive => (
12             is => 'ro',
13             isa => 'Bool',
14             default => 1,
15             );
16              
17             sub _match {
18 41     41   35 my $self = shift;
19 41         35 my $path = shift;
20              
21 41 50       71 if ($self->case_sensitive) {
22 41         30 for my $value (@{ $self->enum }) {
  41         96  
23 116 100       270 return {} if $path->path eq $value;
24             }
25             }
26             else {
27 0         0 for my $value (@{ $self->enum }) {
  0         0  
28 0 0       0 return {} if lc($path->path) eq lc($value);
29             }
30             }
31              
32 17         34 return;
33             }
34              
35             sub _prefix_match {
36 0     0   0 my $self = shift;
37 0         0 my $path = shift;
38              
39 0         0 my $truncated = substr($path->path, 0, length($self->string));
40              
41 0 0       0 if ($self->case_sensitive) {
42 0         0 for my $value (@{ $self->enum }) {
  0         0  
43 0 0       0 next unless $truncated eq $value;
44              
45             return {
46 0         0 leftover => substr($path->path, length($self->string)),
47             };
48             }
49             }
50             else {
51 0         0 for my $value (@{ $self->enum }) {
  0         0  
52 0 0       0 next unless lc($truncated) eq lc($value);
53              
54             return {
55 0         0 leftover => substr($path->path, length($self->string)),
56             };
57             }
58             }
59              
60 0         0 return;
61             }
62              
63             sub complete {
64 18     18 0 160 my $self = shift;
65 18         27 my $path = shift->path;
66 18         14 my @completions;
67              
68             # by convention, complete does include the path itself if it
69             # is a complete match
70 18         13 my @enum = grep { length($path) < length($_) } @{ $self->enum };
  72         123  
  18         31  
71              
72 18 50       44 if ($self->case_sensitive) {
73 18         21 for my $value (@enum) {
74 72         63 my $partial = substr($value, 0, length($path));
75 72 100       116 push @completions, $value if $partial eq $path;
76             }
77             }
78             else {
79 0         0 for my $value (@enum) {
80 0         0 my $partial = substr($value, 0, length($path));
81 0 0       0 push @completions, $value if lc($partial) eq lc($path);
82             }
83             }
84              
85 18         70 return @completions;
86             }
87              
88             __PACKAGE__->meta->make_immutable;
89 32     32   24633 no Any::Moose;
  32         45  
  32         118  
90              
91             1;
92              
93             __END__