File Coverage

blib/lib/Pickles/Dispatcher/Auto.pm
Criterion Covered Total %
statement 48 68 70.5
branch 7 12 58.3
condition 4 8 50.0
subroutine 9 12 75.0
pod 1 2 50.0
total 69 102 67.6


line stmt bran cond sub pod time code
1             package Pickles::Dispatcher::Auto;
2 2     2   26583 use parent qw/Pickles::Dispatcher/;
  2         287  
  2         11  
3 2     2   59661 use strict;
  2         4  
  2         55  
4 2     2   9 use warnings;
  2         9  
  2         49  
5 2     2   1801 use Class::Load ":all";
  2         66797  
  2         335  
6 2     2   1651 use Module::Pluggable::Object;
  2         18590  
  2         86  
7 2     2   2293 use Pickles::Context;
  2         120242  
  2         79  
8 2     2   20 use String::CamelCase qw/camelize decamelize/;
  2         4  
  2         1662  
9              
10             our $VERSION = '0.03';
11              
12             sub match {
13 11     11 1 28447 my $self = shift;
14 11         18 my ($req) = @_;
15              
16 11         50 my $match = $self->SUPER::match(@_);
17              
18 11 100 66     926 if (exists $match->{controller} && exists $match->{action}) {
19 4         18 return $match;
20             }
21              
22 7   33     21 my $path_info = $req->env->{PATH_INFO} || $req->path_info;
23 7         64 $path_info =~ s{^/}{};
24              
25 7         17 my $is_index = $path_info =~ m{/$};
26              
27 7         25 my @parts = split "/", $path_info;
28 7 100 50     22 my $action = $is_index ? "index" : pop @parts || "index";
29              
30 7         10 my $controller = "";
31 7         8 my %args;
32 7 100       23 if (@parts) {
33 6         9 my @camelized_parts = map { camelize $_ } @parts;
  12         85  
34 6         76 $controller .= join "::", @camelized_parts;
35 6         13 $controller .= "::Root";
36             }
37             else {
38 1         2 $controller = "Root";
39             }
40              
41 7         24 $match = +{
42             controller => $controller,
43             action => $action,
44             };
45 7         16 for my $key( keys %{$match} ) {
  7         20  
46 14 50       64 next if $key =~ m/^(controller|action)$/;
47 0         0 $args{$key} = delete $match->{$key};
48             }
49 7         17 $match->{args} = \%args;
50              
51 7         44 return $match;
52             }
53              
54             sub load_controllers {
55 0     0 0   my ($self, $prefix) = @_;
56              
57 0           my @controllers = Module::Pluggable::Object->new(
58             require => 1,
59             search_path => ["$prefix\::Controller"],
60             )->plugins;
61              
62 0           for my $controller (@controllers) {
63 0           load_class($controller);
64             }
65              
66 0           1;
67             }
68              
69             my $_sub_controller_class = sub {
70 0     0     my ($self) = @_;
71 0           my $match = $self->match;
72 0 0         my $controller = $match->{controller} or return;
73 0           (my $class = sprintf "%s::Controller::%s", $self->appname, camelize($controller)) =~ s{/}{::}g;
74 0           return $class;
75             };
76              
77             my $_sub__prepare = sub {
78 0     0     my $self = shift;
79 0           my $match = $self->dispatcher->match($self->req);
80 0           my @paths = split '::', decamelize($match->{controller});
81 0           my $controller = $match->{controller};
82 0           my $view_template;
83 0 0         $controller =~ /Root$/ and pop @paths;
84 0           $view_template = join("/", map { $_ } @paths) . "/" . decamelize($match->{action});
  0            
85 0           $self->stash->{'VIEW_TEMPLATE'} = $view_template;
86             };
87              
88 2     2   16 no warnings "redefine";
  2         5  
  2         150  
89             *Pickles::Context::_prepare = $_sub__prepare;
90             *Pickles::Context::controller_class = $_sub_controller_class;
91              
92             1;
93             __END__