Tatsumaki/Application.pm

filter: Statementstmt 61.7% 63 / 102 Branchbran 42.3% 11 / 26 Conditioncond 40.0% 2 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 66.6% 16 / 24 Podpod 0.0% 0 / 9 totaltotal 54.4% 92 / 169 CC 17 SCAR 13.4
CC
17
Cov
57%
template_path
12.0
add_handlers
6.0
route
6.0
CRAP 3.8
« Tatsumaki/Request.pm ↑ Summary Tatsumaki/Template/Micro.pm »
1 package Tatsumaki::Application;
2 4 ▶use AnyEvent;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶use Any::Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use Tatsumaki::Handler;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use Tatsumaki::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 4 ▶use Plack::Middleware::Static;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 4 ▶use overload q(&{}) => sub { shift->psgi_app }, fallback => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
11  
12 4 has _rules => (is => 'rw', isa => 'ArrayRef');
13 4 has template => (is => 'rw', isa => 'Tatsumaki::Template', lazy_build => 1, handles => [ 'render_file' ]);
14  
15 4 has static_path => (is => 'rw', isa => 'Str', default => 'static');
16 4 ▶has _services => (is => 'rw', isa => 'HashRef', default => sub { +{} });
Subroutine __ANON__: called CC=1 SCAR=0.0
17  
18 4 ▶around BUILDARGS => sub {
Subroutine __ANON__: called CC=5 SCAR=16.3
19 4 my $orig = shift;
20 4 my $class = shift;
21 4 ▶ if (ref $_[0] eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq 'ARRAY') { } 4 0 4
22 4 my $handlers = shift @_;
23 4 my @rules;
24 4 ▶ while (my($path, $handler) = splice @$handlers, 0, 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@$handlers, 0, 2)) 6 4 10
25 6 ▶ $path = qr@^/$@ if $path eq '/';
Branch: 1 branch
BranchTrueFalseTotal
if $path eq "/" 1 5 6
26 6 ▶ $path = qr/^$path/ unless ref $path eq 'RegExp';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $path eq "RegExp" 6 0 6
27 6 push @rules, { path => $path, handler => $handler };
28 }
29 4 $class->$orig(_rules => \@rules, @_);
30 } else {
31 0 $class->$orig(@_);
32 }
33 4 };
34  
35 0 ▶sub add_handlers {
Subroutine add_handlers: not called CC=2 SCAR=17.9
Pod undocumented
36 0 my $self = shift;
37 0 ▶ while (my($path, $handler) = splice @_, 0, 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@_, 0, 2)) 0 0 0
38 0 $self->route($path, $handler);
39 }
40 }
41  
42 0 ▶sub route {
Subroutine route: not called CC=2 SCAR=17.9
Pod undocumented
43 0 my($self, $path, $handler) = @_;
44 0 ▶ $path = qr/^$path/ unless ref $path eq 'RegExp';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $path eq "RegExp" 0 0 0
45 0 push @{$self->_rules}, { path => $path, handler => $handler };
46 }
47  
48 6 ▶sub dispatch {
Subroutine dispatch: called CC=3 SCAR=11.0
Pod undocumented
49 6 my($self, $req) = @_;
50  
51 6 my $path = $req->path_info;
52 6 for my $rule (@{$self->_rules}) {
53 6 ▶ if ($path =~ $rule->{path}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path =~ /$rule->{"path"}/) 4 2 6
54 4 my $args = [ $1, $2, $3, $4, $5, $6, $7, $8, $9 ];
55 4 return $rule->{handler}->new(application => $self, request => $req, args => $args);
56 }
57 }
58  
59 2 return;
60 }
61  
62 6 ▶sub psgi_app {
Subroutine psgi_app: called CC=2 SCAR=7.1
Pod undocumented
63 6 my $self = shift;
64 6 ▶ return $self->{psgi_app} ||= $self->compile_psgi_app;
Condition66%2/3
$self->{"psgi_app"} ||= $self->compile_psgi_app
l!l&&r!l&&!r
420
MC/DC0%0/2
$self->{"psgi_app"} ||= $self->compile_psgi_app
$self->{"psgi_app"} $self->compile_psgi_app
Truth table66%2/3
A ||= B
A= $self->{"psgi_app"}B= $self->compile_psgi_app
ABresult
1X1
011
000
65 }
66  
67 2 ▶sub compile_psgi_app {
Subroutine compile_psgi_app: called CC=2 SCAR=7.2
Pod undocumented
68 2 my $self = shift;
69  
70 6 ▶ my $app = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
71 6 my $env = shift;
72 6 my $req = Tatsumaki::Request->new($env);
73  
74 6 ▶ my $handler = $self->dispatch($req)
Branch: 1 branch
BranchTrueFalseTotal
my $handler = $self->dispatch($req) or return [404, ["Content-Type", "text/html"], ["404 Not Found"]] 4 2 6
75 or return [ 404, [ 'Content-Type' => 'text/html' ], [ "404 Not Found" ] ];
76  
77 4 my $res = $handler->run;
78 2 };
79  
80 2 ▶ if ($self->static_path) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->static_path) 2 0 2
81 0 ▶ $app = Plack::Middleware::Static->wrap($app, path => sub { s/^\/(?:(favicon\.ico)|static\/)/$1||''/e }, root => $self->static_path);
Subroutine __ANON__: called CC=2 SCAR=10.5
Condition0%0/2
$1 || ''
l!l
00
MC/DC0%0/1
$1 || ''
$1
Truth table0%0/2
A
A= $1
Aresult
11
00
82 }
83  
84 2 $app;
85 }
86  
87 0 ▶sub _build_template {
Subroutine _build_template: not called CC=1 SCAR=6.9
88 0 my $self = shift;
89 0 require Tatsumaki::Template::Micro;
90 0 Tatsumaki::Template::Micro->new;
91 }
92  
93 0 ▶sub template_path {
Subroutine template_path: not called CC=3 SCAR=24.8
Pod undocumented
94 0 my $self = shift;
95 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
96 0 ▶ my $path = ref $_[0] eq 'ARRAY' ? $_[0] : [ $_[0] ];
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'ARRAY' ? : 0 0 0
97 0 $self->template->include_path($path);
98 }
99 0 $self->template->include_path;
100 }
101  
102 0 ▶sub add_service {
Subroutine add_service: not called CC=2 SCAR=17.9
Pod undocumented
103 0 my $self = shift;
104  
105 0 my($name, $service);
106 0 ▶ if (@_ == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 2) { } 0 0 0
107 0 ($name, $service) = @_;
108 } else {
109 0 $service = shift;
110 0 $name = $self->_service_name_for($service);
111 }
112  
113 0 $service->application($self);
114 0 $service->start;
115 0 $self->_services->{$name} = $service;
116 }
117  
118 0 ▶sub service {
Subroutine service: not called CC=1 SCAR=6.9
Pod undocumented
119 0 my($self, $name) = @_;
120 0 $self->_services->{$name};
121 }
122  
123 0 ▶sub services {
Subroutine services: not called CC=1 SCAR=6.9
Pod undocumented
124 0 my $self = shift;
125 0 values %{$self->_services};
126 }
127  
128 0 ▶sub _service_name_for {
Subroutine _service_name_for: not called CC=2 SCAR=17.9
129 0 my($self, $service) = @_;
130  
131 0 my $ref = ref $service;
132 0 $ref =~ s/^Tatsumaki::Service:://;
133  
134 0 my $name = $ref;
135  
136 0 my $i = 0;
137 0 ▶ while (exists $self->_services->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->_services->{$name}) 0 0 0
138 0 $name = $ref . $i++;
139 }
140  
141 0 return $name;
142 }
143  
144 4 ▶no Any::Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
145 4 __PACKAGE__->meta->make_immutable;
146  
147 4 1;
148  
149  
« Tatsumaki/Request.pm ↑ Summary Tatsumaki/Template/Micro.pm »