blib/lib/Dancer/Plugin/Params/Normalization.pm

filter: Statementstmt 98.7% 80 / 81 Branchbran 80.9% 34 / 42 Conditioncond 37.5% 3 / 8 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 92.8% 13 / 14 Podpod n/a totaltotal 87.9% 131 / 149 CC 9 SCAR 11.4
CC
9
Cov
89%
__ANON__
6.8
__ANON__
4.0
BEGIN
1.0
CRAP 3.1
↑ Summary blib/lib/Dancer/Plugin/Params/Normalization/Trim.pm »
1 #
2 # This file is part of Dancer-Plugin-Params-Normalization
3 #
4 # This software is copyright (c) 2011 by Damien "dams" Krotkine.
5 #
6 # This is free software; you can redistribute it and/or modify it under
7 # the same terms as the Perl 5 programming language system itself.
8 #
9 package Dancer::Plugin::Params::Normalization;
10 {
11 11 $Dancer::Plugin::Params::Normalization::VERSION = '0.52';
12 }
13  
14 # ABSTRACT: A plugin for normalizing query parameters in Dancer
15  
16 11 ▶use Dancer ':syntax';
Subroutine BEGIN: called CC=1 SCAR=0.0
17 11 ▶use Dancer::Plugin;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 11 my $conf = plugin_setting;
20  
21 # method that does nothing. It's optimized to nothing at compile time
22 11 ▶my $void = sub(){};
Subroutine __ANON__: not called CC=1 SCAR=0.0
23  
24 # set the params_filter
25 11 my $params_filter = sub () { 1; };
26 11 ▶if (defined $conf->{params_filter}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $conf->{"params_filter"}) 1 10 11
27 1 my $re = $conf->{params_filter};
28 2 ▶ $params_filter = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
29 2 return scalar($_[0] =~ /$re/) };
30 }
31  
32 # method that loops on a hashref and apply a given method on its keys
33 14 ▶my $apply_on_keys = sub {
Subroutine __ANON__: called CC=6 SCAR=19.1
34 14 my ($h, $func) = @_;
35 14 my $new_h = {};
36 14 ▶ while (my ($k, $v) = each (%$h)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$h) 8 14 22
37 8 ▶ my $new_k = $params_filter->($k) ? $func->($k) : $k;
Branch: 1 branch
BranchTrueFalseTotal
&$params_filter($k) ? : 7 1 8
38 8 ▶ exists $new_h->{$new_k} && ! ($conf->{no_conflict_warn} || 0)
Branch: 1 branch
BranchTrueFalseTotal
exists $new_h->{$new_k} and not $conf->{"no_conflict_warn"} || 0 and warn "paramater names conflict while doing normalization of parameters '${k}' : it produces '${new_k}', which alreay exists." 0 8 8
Condition20%1/5
exists $new_h->{$new_k} and not $conf->{"no_conflict_warn"} || 0
!ll&&!rl&&r
800
$conf->{'no_conflict_warn'} || 0
l!l
00
MC/DC0%0/2
exists $new_h->{$new_k} and not $conf->{"no_conflict_warn"} || 0
exists $new_h->{$new_k} $conf->{'no_conflict_warn'}
Truth table33%1/3
A and not(B)
A= exists $new_h->{$new_k}B= $conf->{'no_conflict_warn'}
ABresult
0X0
110
101
39 and warn "paramater names conflict while doing normalization of parameters '$k' : it produces '$new_k', which alreay exists.";
40 8 $new_h->{$new_k} = $v;
41 }
42 14 return $new_h;
43 11 };
44  
45  
46 # default normalization method is passthrough (do nothing)
47 11 my $normalization_fonction = $void;
48 11 ▶if (defined $conf->{method} && $conf->{method} ne 'passthrough') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $conf->{"method"} and $conf->{"method"} ne "passthrough") 9 2 11
Condition66%2/3
defined $conf->{"method"} and $conf->{"method"} ne "passthrough"
!ll&&!rl&&r
209
MC/DC50%1/2
defined $conf->{"method"} and $conf->{"method"} ne "passthrough"
defined $conf->{"method"} $conf->{"method"} ne "passthrough"
Truth table66%2/3
A and B
A= defined $conf->{"method"}B= $conf->{"method"} ne "passthrough"
ABresult
0X0
100
111
49 9 my $method;
50 9 ▶ if ($conf->{method} eq 'lowercase') {
Branch: 3 branches
BranchTrueFalseTotal
if ($conf->{'method'} eq 'lowercase') { } 4 5 9
elsif ($conf->{'method'} eq 'uppercase') { } 1 4 5
elsif ($conf->{'method'} eq 'ucfirst') { } 1 3 4
51 10 ▶ $method = sub { my ($h) = @_; $apply_on_keys->($h, sub { lc($_[0]) } ) };
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
52 } elsif ($conf->{method} eq 'uppercase') {
53 2 ▶ $method = sub { my ($h) = @_; $apply_on_keys->($h, sub { uc($_[0]) } ) };
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
54 } elsif ($conf->{method} eq 'ucfirst') {
55 2 ▶ $method = sub { my ($h) = @_; $apply_on_keys->($h, sub { ucfirst($_[0]) } ) };
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
56 } else {
57 3 my $class = $conf->{method};
58 3 my $class_name = $class;
59 3 $class_name =~ s!::|'!/!g;
60 3 $class_name .= '.pm';
61 3 ▶ if ( ! $class->can('new') ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($class->can("new")) 1 2 3
62 1 eval { require $class_name };
63 1 ▶ $@ and die "error while requiring custom normalization class '$class' : $@";
Branch: 1 branch
BranchTrueFalseTotal
$@ and die "error while requiring custom normalization class '${class}' : $@" 0 1 1
64 }
65 3 my $abstract_classname = __PACKAGE__ . '::Abstract';
66 3 ▶ $class->isa(__PACKAGE__ . '::Abstract')
Branch: 1 branch
BranchTrueFalseTotal
$class->isa("Dancer::Plugin::Params::Normalization::Abstract") or die "custom normalization class '${class}' doesn't inherit from '${abstract_classname}'" 3 0 3
67 or die "custom normalization class '$class' doesn't inherit from '$abstract_classname'";
68 3 my $instance = $class->new();
69 # using a custom normalization is incompatible with params filters
70 defined $conf->{params_filter}
71 3 ▶ and die "your configuration contains a 'params_filter' fields, and a custom 'method' normalization class name. The two fields are incompatible";
Branch: 1 branch
BranchTrueFalseTotal
defined $conf->{"params_filter"} and die "your configuration contains a 'params_filter' fields, and a custom 'method' normalization class name. The two fields are incompatible" 0 3 3
72 # todo : use *method = \&{$class->normalize} or somethin'
73 10 ▶ $method = sub { $instance->normalize($_[0]) };
Subroutine __ANON__: called CC=1 SCAR=0.0
74 }
75  
76 9 my $params_types = $conf->{params_types};
77 # default value
78 9 ▶ defined $params_types
Branch: 1 branch
BranchTrueFalseTotal
defined $params_types or $params_types = ["query", "body"] 1 8 9
79 or $params_types = [ qw(query body) ];
80 9 ▶ ref $params_types eq 'ARRAY'
Branch: 1 branch
BranchTrueFalseTotal
ref $params_types eq "ARRAY" or die "configuration field 'params_types' should be an array" 9 0 9
81 or die "configuration field 'params_types' should be an array";
82  
83 9 my %params_types = map { $_ => 1 } @$params_types;
84 9 my $params_type_query = delete $params_types{query};
85 9 my $params_type_body = delete $params_types{body};
86 9 my $params_type_route = delete $params_types{route};
87 9 ▶ keys %params_types
Branch: 1 branch
BranchTrueFalseTotal
keys %params_types and die q[your configuration contains '] . join(", ", keys %params_types) . q[' as 'params_types' field(s), but only these are allowed : 'query', 'body', 'route'] 0 9 9
88 and die "your configuration contains '" . join(', ', keys %params_types) .
89 "' as 'params_types' field(s), but only these are allowed : 'query', 'body', 'route'";
90  
91 13 ▶ $normalization_fonction = sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
92 my ($new_query_params,
93 $new_body_params,
94 13 $new_route_params) = map { scalar(params($_)) } qw(query body route);
95 13 ▶ $params_type_query and $new_query_params = $method->($new_query_params);
Branch: 1 branch
BranchTrueFalseTotal
$params_type_query and $new_query_params = &$method($new_query_params) 11 2 13
96 13 ▶ $params_type_body and $new_body_params = $method->($new_body_params);
Branch: 1 branch
BranchTrueFalseTotal
$params_type_body and $new_body_params = &$method($new_body_params) 11 2 13
97 13 ▶ $params_type_route and $new_route_params = $method->($new_route_params);
Branch: 1 branch
BranchTrueFalseTotal
$params_type_route and $new_route_params = &$method($new_route_params) 2 11 13
98  
99 13 request->{params} = {};
100  
101 13 request->_set_query_params($new_query_params);
102 13 request->_set_body_params($new_body_params);
103 13 request->_set_route_params($new_route_params);
104 9 };
105 }
106  
107 11 ▶if (defined $conf->{general_rule}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $conf->{'general_rule'}) { } 1 10 11
108 1 ▶ $conf->{general_rule} =~ /^always$|^ondemand$/
Branch: 1 branch
BranchTrueFalseTotal
$conf->{"general_rule"} =~ /^always$|^ondemand$/ or die "configuration field general_rule must be one of : always, ondemand" 1 0 1
109 or die 'configuration field general_rule must be one of : always, ondemand';
110 1 ▶ if ($conf->{general_rule} eq 'ondemand') {
Branch: 1 branch
BranchTrueFalseTotal
if ($conf->{'general_rule'} eq 'ondemand') { } 1 0 1
111 1 ▶ register normalize => sub{ $normalization_fonction->() };
Subroutine __ANON__: called CC=1 SCAR=0.0
112 } else {
113 0 hook before => $normalization_fonction;
114 }
115 } else {
116 10 hook before => $normalization_fonction;
117 }
118  
119 11 register_plugin;
120  
121 11 1;
122  
123 __END__
↑ Summary blib/lib/Dancer/Plugin/Params/Normalization/Trim.pm »