File Coverage

lib/Web/API/Mapper/RuleSet.pm
Criterion Covered Total %
statement 34 35 97.1
branch 5 8 62.5
condition n/a
subroutine 9 9 100.0
pod 1 5 20.0
total 49 57 85.9


line stmt bran cond sub pod time code
1             package Web::API::Mapper::RuleSet;
2 2     2   13 use warnings;
  2         3  
  2         62  
3 2     2   11 use strict;
  2         5  
  2         59  
4 2     2   12 use Any::Moose;
  2         5  
  2         12  
5 2     2   2819 use Path::Dispatcher;
  2         152359  
  2         1601  
6              
7             has disp => (
8             is => 'rw' ,
9             default => sub {
10             return Path::Dispatcher->new;
11             } );
12              
13             has rules => ( is => 'rw', isa => 'ArrayRef' , default => sub { [ ] } );
14              
15             has fallback => ( is => 'rw' , isa => 'CodeRef' , default => sub { sub { } } );
16              
17             around BUILDARGS => sub {
18             my $orig = shift;
19             my $class = shift;
20             if ( ! ref $_[0] && ref $_[1] eq 'ARRAY') {
21             my $base = shift @_;
22             my $handlers = shift @_;
23             my @rules;
24             while (my($path, $code) = splice @$handlers, 0, 2) {
25             $path = $base . $path;
26             $path = qr@^/$@ if $path eq '/';
27             $path = qr/^$path/ unless ref $path eq 'RegExp';
28             push @rules, { path => $path, code => $code };
29             }
30             $class->$orig( base => $base , rules => \@rules, @_);
31             } else {
32             $class->$orig(@_);
33             }
34             };
35              
36             sub BUILD {
37 14     14 1 62 my $self = shift;
38 14         38 $self->{_hits} = 0;
39 14         36 $self->load();
40             }
41              
42             sub mount {
43 3     3 0 7 my ($self,$base,$routes) = @_;
44 3         12 while (my($path, $code) = splice @$routes, 0, 2) {
45 3         6 $path = $base . $path;
46 3 50       8 $path = qr@^/$@ if $path eq '/';
47 3 50       71 $path = qr/^$path/ unless ref $path eq 'RegExp';
48 3         5 push @{ $self->rules } , { path => $path, code => $code };
  3         18  
49 3         10 $self->add_rule( $path , $code );
50             }
51             }
52              
53             sub add_rule {
54 7     7 0 13 my ($self,$path,$code) = @_;
55 7         68 $self->disp->add_rule( Path::Dispatcher::Rule::Regex->new( regex => $path , block => $code ));
56 7         531 return $self;
57             }
58              
59             sub load {
60 14     14 0 20 my $self = shift;
61 14         17 $self->add_rule( $_->{path} , $_->{code} ) for @{ $self->rules };
  14         68  
62 14         140 return $self;
63             }
64              
65             sub dispatch {
66 12     12 0 5611 my ($self,$path,$args) = @_;
67             # $self->{_hits}++;
68             # my $base = $self->base;
69             # $path =~ s{^/$base/}{} if $base;
70 12         85 my $dispatch = $self->disp->dispatch( $path );
71 12 100       2306 return $dispatch->run( $args ) if $dispatch->has_matches;
72 4 50       50 return $self->fallback->( $args ) if $self->fallback;
73 0           return;
74             }
75              
76              
77             1;