blib/lib/OX/RouteBuilder/REST.pm

filter: Statementstmt 94.1% 48 / 51 Branchbran 64.2% 9 / 14 Conditioncond 33.3% 2 / 6 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 88.8% 8 / 9 Podpod 0.0% 0 / 2 totaltotal 79.0% 68 / 86 CC 10 SCAR 16.4
CC
10
Cov
83%
__ANON__
9.6
parse_action_spec
3.0
__ANON__
1.1
CRAP 5.2
↑ Summary
1 package OX::RouteBuilder::REST;
2  
3 # ABSTRACT: OX::RouteBuilder which routes to an action method in a controller class based on HTTP verbs
4 2 our $VERSION = '0.006'; # VERSION
5  
6 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 with 'OX::RouteBuilder';
11  
12 1 ▶sub import {
Subroutine import: called CC=1 SCAR=0.0
13 1 my $caller = caller;
14 1 my $meta = Moose::Util::find_meta($caller);
15 1 $meta->add_route_builder('OX::RouteBuilder::REST');
16 }
17  
18 6 ▶sub compile_routes {
Subroutine compile_routes: called CC=1 SCAR=0.1
Pod undocumented
19 6 my $self = shift;
20 6 my ($app) = @_;
21  
22 6 my $spec = $self->route_spec;
23 6 my $params = $self->params;
24 6 my ( $defaults, $validations ) =
25 $self->extract_defaults_and_validations($params);
26 6 $defaults = { %$spec, %$defaults };
27  
28 6 ▶ my $target = sub {
Subroutine __ANON__: called CC=8 SCAR=22.6
29 6 my ($req) = @_;
30  
31 6 my $match = $req->mapping;
32 6 my $c = $match->{controller};
33 6 my $a = $match->{action};
34  
35 6 my $err;
36 0 ▶ my $s = try { $app->fetch($c) } catch { ($err) = split "\n"; undef };
Subroutine __ANON__: called CC=1 SCAR=1.2
Subroutine __ANON__: not called CC=1 SCAR=1.2
37 return [
38 6 ▶ 500, [], [ "Cannot resolve $c in " . blessed($app) . ": $err" ]
Branch: 1 branch
BranchTrueFalseTotal
unless $s 0 6 6
39 ]
40 unless $s;
41  
42 6 my $component = $s->get;
43 6 my $method = uc( $req->method );
44 6 my $action = $a . '_' . $method;
45  
46 6 ▶ if ( $component->can($action) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($component->can($action)) { } 4 2 6
elsif ($method eq 'HEAD' and $component->can($a . '_GET')) { } 1 1 2
47 4 return $component->$action(@_);
48 }
49 ▶ elsif ($method eq 'HEAD' && $component->can($a . '_GET')) {
Condition66%2/3
$method eq "HEAD" and $component->can($a . '_GET')
!ll&&!rl&&r
101
MC/DC50%1/2
$method eq "HEAD" and $component->can($a . '_GET')
$method eq "HEAD" $component->can($a . '_GET')
Truth table66%2/3
A and B
A= $method eq "HEAD"B= $component->can($a . '_GET')
ABresult
0X0
100
111
50 1 $action = $a . '_GET';
51 1 my $res = $component->$action(@_);
52 1 ▶ if (ref($res) eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $res eq 'ARRAY') { } 1 0 1
elsif (builtin::blessed($res) and $res->can('content')) { } 0 0 0
53 1 $res->[2]=[''];
54 }
55 ▶ elsif (blessed($res) && $res->can('content')) {
Condition0%0/3
builtin::blessed($res) and $res->can("content")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($res) and $res->can("content")
builtin::blessed($res) $res->can("content")
Truth table0%0/3
A and B
A= builtin::blessed($res)B= $res->can("content")
ABresult
0X0
100
111
56 0 $res->content('');
57 }
58 # else don't mess with content..
59 1 return $res;
60 }
61 else {
62 1 return [ 501, [],
63 ["Controller '$c' (".blessed($component).") has no method '$action'"] ];
64 }
65 6 };
66  
67 return {
68 6 path => $self->path,
69 defaults => $defaults,
70 target => $target,
71 validations => $validations,
72 };
73 }
74  
75 4 ▶sub parse_action_spec {
Subroutine parse_action_spec: called CC=3 SCAR=11.0
Pod undocumented
76 4 my $class = shift;
77 4 my ($action_spec) = @_;
78  
79 4 ▶ return if ref($action_spec);
Branch: 1 branch
BranchTrueFalseTotal
if ref $action_spec 0 4 4
80 4 ▶ return unless $action_spec =~ /^REST\.(\w+)\.(\w+)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $action_spec =~ /^REST\.(\w+)\.(\w+)$/ 2 2 4
81  
82 return {
83 2 controller => $1,
84 action => $2,
85 name => $action_spec,
86 };
87 }
88  
89 2 __PACKAGE__->meta->make_immutable;
90  
91 2 1;
92  
93 __END__
↑ Summary