blib/lib/Plack/Builder.pm

filter: Statementstmt 98.7% 81 / 82 Branchbran 86.3% 19 / 22 Conditioncond 55.5% 5 / 9 MC/DCmcdc 40.0% 2 / 5 Subroutinesub 95.6% 22 / 23 Podpod 0.0% 0 / 9 totaltotal 86.0% 129 / 150 CC 16 SCAR 9.9
CC
16
Cov
93%
builder
6.2
to_app
4.0
wrap
4.0
CRAP 2.7
« blib/lib/Plack/Component.pm ↑ Summary blib/lib/Plack/Middleware/JSONP.pm »
1 package Plack::Builder;
2 31 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 31 ▶use parent qw( Exporter );
Subroutine BEGIN: called CC=1 SCAR=0.0
4 31 our @EXPORT = qw( builder add enable enable_if mount );
5  
6 31 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 31 ▶use Plack::App::URLMap;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 31 ▶use Plack::Middleware::Conditional; # TODO delayed load?
Subroutine BEGIN: called CC=1 SCAR=0.0
9 31 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 61 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
12 61 my $class = shift;
13 61 bless { middlewares => [ ] }, $class;
14 }
15  
16 72 ▶sub add_middleware {
Subroutine add_middleware: called CC=2 SCAR=6.9
Pod undocumented
17 72 my($self, $mw, @args) = @_;
18  
19 72 ▶ if (ref $mw ne 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $mw ne "CODE") 70 2 72
20 70 my $mw_class = Plack::Util::load_class($mw, 'Plack::Middleware');
21 70 ▶ $mw = sub { $mw_class->wrap($_[0], @args) };
Subroutine __ANON__: called CC=1 SCAR=0.0
22 }
23  
24 72 push @{$self->{middlewares}}, $mw;
25 }
26  
27 3 ▶sub add_middleware_if {
Subroutine add_middleware_if: called CC=2 SCAR=6.9
Pod undocumented
28 3 my($self, $cond, $mw, @args) = @_;
29  
30 3 ▶ if (ref $mw ne 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $mw ne "CODE") 2 1 3
31 2 my $mw_class = Plack::Util::load_class($mw, 'Plack::Middleware');
32 2 ▶ $mw = sub { $mw_class->wrap($_[0], @args) };
Subroutine __ANON__: called CC=1 SCAR=0.0
33 }
34  
35 3 ▶ push @{$self->{middlewares}}, sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
36 3 Plack::Middleware::Conditional->wrap($_[0], condition => $cond, builder => $mw);
37 3 };
38 }
39  
40 # do you want remove_middleware() etc.?
41  
42 5 ▶sub _mount {
Subroutine _mount: called CC=2 SCAR=7.0
43 5 my ($self, $location, $app) = @_;
44  
45 5 ▶ if (!$self->{_urlmap}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_urlmap"}) 5 0 5
46 5 $self->{_urlmap} = Plack::App::URLMap->new;
47 }
48  
49 5 $self->{_urlmap}->map($location => $app);
50 5 $self->{_urlmap}; # for backward compat.
51 }
52  
53 60 ▶sub to_app {
Subroutine to_app: called CC=4 SCAR=13.9
Pod undocumented
54 60 my($self, $app) = @_;
55  
56 60 ▶ if ($app) {
Branch: 2 branches
BranchTrueFalseTotal
if ($app) { } 56 4 60
elsif ($self->{'_urlmap'}) { } 3 1 4
57 56 $self->wrap($app);
58 } elsif ($self->{_urlmap}) {
59 $self->{_urlmap} = $self->{_urlmap}->to_app
60 3 ▶ if Scalar::Util::blessed($self->{_urlmap});
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($self->{"_urlmap"}) 3 0 3
61 3 $self->wrap($self->{_urlmap});
62 } else {
63 1 Carp::croak("to_app() is called without mount(). No application to build.");
64 }
65 }
66  
67 60 ▶sub wrap {
Subroutine wrap: called CC=4 SCAR=13.9
Pod undocumented
68 60 my($self, $app) = @_;
69  
70 60 ▶ if ($self->{_urlmap} && $app ne $self->{_urlmap}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_urlmap"} and $app ne $self->{"_urlmap"}) 1 59 60
Condition100%3/3
$self->{"_urlmap"} and $app ne $self->{"_urlmap"}
!ll&&!rl&&r
5541
MC/DC100%2/2
$self->{"_urlmap"} and $app ne $self->{"_urlmap"}
$self->{"_urlmap"} $app ne $self->{"_urlmap"}
Truth table100%3/3
A and B
A= $self->{"_urlmap"}B= $app ne $self->{"_urlmap"}
ABresult
0X0
100
111
71 1 Carp::carp("WARNING: wrap() and mount() can't be used altogether in Plack::Builder.\n" .
72 "WARNING: This causes all previous mount() mappings to be ignored.");
73 }
74  
75 60 for my $mw (reverse @{$self->{middlewares}}) {
76 74 $app = $mw->($app);
77 }
78  
79 60 $app;
80 }
81  
82 # DSL goes here
83 0 ▶our $_add = our $_add_if = our $_mount = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
84 0 Carp::croak("enable/mount should be called inside builder {} block");
85 31 };
86  
87 64 ▶sub enable { $_add->(@_) }
Subroutine enable: called CC=1 SCAR=0.0
Pod undocumented
88 2 ▶sub enable_if(&$@) { $_add_if->(@_) }
Subroutine enable_if: called CC=1 SCAR=0.0
Pod undocumented
89  
90 13 ▶sub mount {
Subroutine mount: called CC=2 SCAR=6.9
Pod undocumented
91 13 my $self = shift;
92 13 ▶ if (Scalar::Util::blessed($self)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($self)) { } 5 8 13
93 5 $self->_mount(@_);
94 }else{
95 8 $_mount->($self, @_);
96 }
97 }
98  
99 55 ▶sub builder(&) {
Subroutine builder: called CC=6 SCAR=18.2
Pod undocumented
100 55 my $block = shift;
101  
102 55 my $self = __PACKAGE__->new;
103  
104 55 my $mount_is_called;
105 55 my $urlmap = Plack::App::URLMap->new;
106 8 ▶ local $_mount = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
107 8 $mount_is_called++;
108 8 $urlmap->map(@_);
109 8 $urlmap;
110 55 };
111 64 ▶ local $_add = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
112 64 $self->add_middleware(@_);
113 55 };
114 2 ▶ local $_add_if = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
115 2 $self->add_middleware_if(@_);
116 55 };
117  
118 55 my $app = $block->();
119  
120 55 ▶ if ($mount_is_called) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mount_is_called) 4 51 55
121 4 ▶ if ($app ne $urlmap) {
Branch: 1 branch
BranchTrueFalseTotal
if ($app ne $urlmap) { } 1 3 4
122 1 Carp::carp("WARNING: You used mount() in a builder block, but the last line (app) isn't using mount().\n" .
123 "WARNING: This causes all mount() mappings to be ignored.\n");
124 } else {
125 3 $app = $app->to_app;
126 }
127 }
128  
129 55 ▶ $app = $app->to_app if $app and Scalar::Util::blessed($app) and $app->can('to_app');
Branch: 1 branch
BranchTrueFalseTotal
if $app and builtin::blessed($app) and $app->can("to_app") 0 55 55
Condition33%2/6
$app and builtin::blessed($app) and $app->can("to_app")
!ll&&!rl&&r
5500
$app and builtin::blessed($app)
!ll&&!rl&&r
0550
MC/DC0%0/3
$app and builtin::blessed($app) and $app->can("to_app")
$app builtin::blessed($app) $app->can("to_app")
Truth table25%1/4
A and B and C
A= $appB= builtin::blessed($app)C= $app->can("to_app")
ABCresult
0XX0
10X0
1100
1111
130  
131 55 $self->to_app($app);
132 }
133  
134 31 1;
135  
136 __END__
« blib/lib/Plack/Component.pm ↑ Summary blib/lib/Plack/Middleware/JSONP.pm »