examples/tasks-modulino/lib/MyApp.pm

filter: Statementstmt 93.1% 27 / 29 Branchbran n/a Conditioncond 36.3% 4 / 11 MC/DCmcdc 16.6% 1 / 6 Subroutinesub 81.8% 9 / 11 Podpod 0.0% 0 / 1 totaltotal 70.6% 41 / 58 CC 6 SCAR 12.6
CC
6
Cov
78%
__ANON__
12.0
__ANON__
2.0
__ANON__
2.0
CRAP 3.5
« examples/chat-showcase/app.pl ↑ Summary examples/custom-send-events/app.pl »
1 package MyApp;
2  
3 1 ▶use v5.40;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use experimental 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use PAGI::Nano;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 # The "grows up" run shape: a modulino. Loading the file returns the app (the
9 # last expression), so it runs directly:
10 #
11 # pagi-server -Ilib lib/MyApp.pm
12 #
13 # It is also dual-use: `use MyApp; my $app = MyApp->to_app` for tests, or
14 # `mount '/tasks' => MyApp->to_app` to nest it inside a larger app. Same app
15 # value either way — no rewrite from the single-file form.
16  
17 # The handlers initialize their slice of shared state defensively (//= []). This
18 # keeps the modulino robust in both run shapes: standalone, startup seeds the
19 # state; mounted inside a larger app, the router does not forward lifespan events
20 # to mounted children, so the outer app owns lifecycle and the defensive init
21 # covers the gap.
22 3 ▶sub to_app ($class) {
Subroutine to_app: called CC=1 SCAR=0.3
Pod undocumented
23 3 ▶ return app {
Subroutine __ANON__: called CC=1 SCAR=0.4
24 3 ▶ startup async sub ($state) { $state->{tasks} //= [] };
Subroutine __ANON__: called CC=2 SCAR=7.0
Condition50%1/2
$state->{"tasks"} //= []
l!l
01
MC/DC0%0/1
$state->{"tasks"} //= []
$state->{"tasks"}
Truth table50%1/2
A
A= $state->{"tasks"}
Aresult
11
00
25  
26 3 ▶ get '/' => sub ($c) { $c->state->{tasks} //= [] };
Subroutine __ANON__: called CC=2 SCAR=6.9
Condition100%2/2
$c->state->{"tasks"} //= []
l!l
11
MC/DC100%1/1
$c->state->{"tasks"} //= []
$c->state->{"tasks"}
Truth table100%2/2
A
A= $c->state->{"tasks"}
Aresult
11
00
27  
28 0 ▶ get '/:id' => sub ($c, $id) {
Subroutine __ANON__: not called CC=3 SCAR=24.8
29 0 ▶ ($c->state->{tasks} //= [])->[$id - 1]
Condition0%0/5
($c->state->{"tasks"} //= [])->[$id - 1] // $c->json({"error", "not found"}, "status", 404)
l!l&&r!l&&!r
000
$c->state->{"tasks"} //= []
l!l
00
MC/DC0%0/2
($c->state->{"tasks"} //= [])->[$id - 1] // $c->json({"error", "not found"}, "status", 404)
($c->state->{"tasks"} //= [])->[$id - 1] $c->json({"error", "not found"}, "status", 404)
MC/DC0%0/1
$c->state->{"tasks"} //= []
$c->state->{"tasks"}
Truth table0%0/3
A // B
A= ($c->state->{"tasks"} //= [])->[$id - 1]B= $c->json({"error", "not found"}, "status", 404)
ABresult
1X1
011
000
Truth table0%0/2
A
A= $c->state->{"tasks"}
Aresult
11
00
30 // $c->json({ error => 'not found' }, status => 404);
31 3 };
32  
33 1 ▶ post '/' => async sub ($c) {
Subroutine __ANON__: called CC=2 SCAR=7.1
34 my $attrs = await $c->params->required(
35 'title', +{ tags => [] },
36 0 ▶ sub ($c, $missing) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
37 0 $c->json({ error => 'missing', fields => $missing }, status => 400);
38 },
39 1 );
40 1 ▶ my $tasks = $c->state->{tasks} //= [];
Condition50%1/2
$c->state->{"tasks"} //= []
l!l
10
MC/DC0%0/1
$c->state->{"tasks"} //= []
$c->state->{"tasks"}
Truth table50%1/2
A
A= $c->state->{"tasks"}
Aresult
11
00
41 1 push @$tasks, { id => scalar(@$tasks) + 1, %$attrs };
42 1 $c->json($tasks->[-1], status => 201);
43 3 };
44 3 };
45 }
46  
47 1 __PACKAGE__->to_app;
« examples/chat-showcase/app.pl ↑ Summary examples/custom-send-events/app.pl »