blib/lib/Mojolicious/Plugin/AutoParams.pm

filter: Statementstmt 96.8% 31 / 32 Branchbran 83.3% 10 / 12 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 5 / 5 Podpod 100% 1 / 1 totaltotal 90.9% 50 / 55 CC 9 SCAR 18.9
CC
9
Cov
91%
__ANON__
9.1
register
1.0
BEGIN
1.0
CRAP 6.6
↑ Summary
1 package Mojolicious::Plugin::AutoParams;
2 4 ▶use Mojo::Base 'Mojolicious::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 4 our $VERSION = '0.02';
7  
8 4 ▶sub register {
Subroutine register: called CC=1 SCAR=0.0
Pod documented
9 4 my ($self,$app,$config) = @_;
10  
11 8 ▶ $app->hook(around_action => sub {
Subroutine __ANON__: called CC=9 SCAR=22.1
12 8 my ($next, $c, $action, $last) = @_;
13 8 my $endpoint = $c->match->endpoint;
14 8 ▶ return $c->$action unless ref $endpoint eq 'Mojolicious::Routes::Route';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $endpoint eq "Mojolicious::Routes::Route" 0 8 8
15 8 my @placeholders = @{ $endpoint->pattern->placeholders };
16 8 my @params;
17 my %params;
18 8 for my $level ( @{ $c->match->stack }) {
19 8 ▶ my $placeholder = shift @placeholders or last;
Branch: 1 branch
BranchTrueFalseTotal
my $placeholder = shift @placeholders or last 7 1 8
20 7 ▶ while ($placeholder and exists( $level->{$placeholder} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($placeholder and exists $level->{$placeholder}) 10 7 17
Condition66%2/3
$placeholder and exists $level->{$placeholder}
!ll&&!rl&&r
7010
MC/DC50%1/2
$placeholder and exists $level->{$placeholder}
$placeholder exists $level->{$placeholder}
Truth table66%2/3
A and B
A= $placeholderB= exists $level->{$placeholder}
ABresult
0X0
100
111
21 10 push @params, $level->{$placeholder};
22 10 $params{$placeholder} = $level->{$placeholder};
23 10 $placeholder = shift @placeholders;
24 }
25 }
26 8 ▶ return $c->$action(@params) unless $config->{named};
Branch: 1 branch
BranchTrueFalseTotal
unless $config->{"named"} 4 4 8
27 4 ▶ return $c->$action(%params) if $config->{named} eq '1';
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"named"} eq 1 2 2 4
28 2 ▶ if ($config->{named} eq '_') {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{"named"} eq "_") 2 0 2
29 2 local $_ = \%params;
30 2 return $c->$action;
31 }
32 0 return $c->$action;
33 4 });
34 }
35  
36 4 1;
37  
38 =encoding utf8
39  
40 =head1 NAME
41  
42 Mojolicious::Plugin::AutoParams - Send captured placeholder values as parameters for routes.
43  
44 =head1 SYNOPSIS
45  
46 use Mojolicious::Lite;
47 use experimental 'signatures';
48 plugin 'auto_params';
49  
50 get '/hello/:name' => sub ($c,$name) { $c->render(text => "hi, $name") }
51  
52 OR
53  
54 use Mojolicious::Lite;
55 plugin 'auto_params', { named => 1 }
56  
57 get '/hello/:name' => sub {
58 my $c = shift;
59 my %args = @_;
60 $c->render(text => "hi, $args{name}")
61 };
62  
63 OR
64  
65 use Mojolicious::Lite;
66 plugin 'auto_params', { named => '_' }
67  
68 get '/hello/:name' => sub { shift->render(text => "hi, $_->{name}") }
69  
70 =head1 DESCRIPTION
71  
72 This module automatically sends placeholders as a list of parameters to routes.
73  
74 By default it uses positional parameters, but it will optionally send a hash if
75 the "named" option is set to 1.
76  
77 Setting 'named' to '_' will set the local $_ to a hashref of the placeholders.
78  
79 =head1 AUTHOR
80  
81 Brian Duggan <bduggan@matatu.org>
82  
83 =cut
84  
85  
↑ Summary