blib/lib/Raisin/Param.pm

filter: Statementstmt 98.7% 78 / 79 Branchbran 86.9% 40 / 46 Conditioncond 88.2% 15 / 17 MC/DCmcdc 81.8% 9 / 11 Subroutinesub 100% 11 / 11 Podpod 75.0% 3 / 4 totaltotal 92.8% 156 / 168 CC 32 SCAR 23.9
CC
32
Cov
94%
validate
19.4
_parse
7.0
new
4.0
CRAP 10.9
« blib/lib/Raisin.pm ↑ Summary blib/lib/Raisin/Routes.pm »
1 #!perl
2 #PODNAME: Raisin::Param
3 #ABSTRACT: Parameter class for Raisin
4  
5 20 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 20 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 package Raisin::Param;
9 20 $Raisin::Param::VERSION = '0.94';
10 20 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 20 ▶use Plack::Util::Accessor qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
12 named
13 required
14  
15 default
16 desc
17 enclosed
18 name
19 regex
20 type
21 coerce
22 20 );
23  
24 20 ▶use Raisin::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 20 my @ATTRIBUTES = qw(name type default regex desc coerce);
27 20 my @LOCATIONS = qw(path formData body header query);
28  
29 160 ▶sub new {
Subroutine new: called CC=4 SCAR=13.9
Pod undocumented
30 160 my ($class, %args) = @_;
31 160 my $self = bless {}, $class;
32  
33 160 ▶ $self->{named} = $args{named} || 0;
Condition100%2/2
$args{'named'} || 0
l!l
22138
MC/DC100%1/1
$args{'named'} || 0
$args{'named'}
Truth table100%2/2
A
A= $args{'named'}
Aresult
11
00
34 160 ▶ $self->{required} = $args{type} =~ /^require(s|d)$/ ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$args{'type'} =~ /^require(s|d)$/ ? : 115 45 160
35  
36 160 ▶ return unless $self->_parse($args{spec});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_parse($args{"spec"}) 1 159 160
37  
38 159 $self;
39 }
40  
41 160 ▶sub _parse {
Subroutine _parse: called CC=7 SCAR=19.5
42 160 my ($self, $spec) = @_;
43  
44 160 $self->{$_} = $spec->{$_} for @ATTRIBUTES;
45  
46 160 ▶ if ($spec->{in}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec->{"in"}) 8 152 160
47 8 ▶ return unless $self->in($spec->{in});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->in($spec->{"in"}) 1 7 8
48 }
49  
50 159 ▶ if ($spec->{encloses}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec->{"encloses"}) 30 129 159
51 30 ▶ if ($self->type->name eq 'HashRef') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->type->name eq 'HashRef') { } 28 2 30
52 28 $self->{enclosed} = _compile_enclosed($spec->{encloses});
53 }
54 else {
55 2 Raisin::log(
56 warn => 'Ignoring enclosed parameters for `%s`, type should be `HashRef` not `%s`',
57 $self->name, $self->type->name
58 );
59 }
60 }
61  
62 159 ▶ $self->{coerce} = defined($spec->{coerce}) ? $spec->{coerce} : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $spec->{'coerce'} ? : 0 159 159
63  
64 159 return 1;
65 }
66  
67 28 ▶sub _compile_enclosed {
Subroutine _compile_enclosed: called CC=3 SCAR=11.0
68 28 my $params = shift;
69  
70 28 my @enclosed;
71 28 my $next_param = Raisin::Util::iterate_params($params);
72 28 ▶ while (my ($type, $spec) = $next_param->()) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = &$next_param()) 84 0 84
73 84 ▶ last unless $type;
Branch: 1 branch
BranchTrueFalseTotal
unless $type 28 56 84
74  
75 56 push @enclosed, Raisin::Param->new(
76 named => 0,
77 type => $type, # -> requires/optional
78 spec => $spec, # -> { name => ..., type => ... }
79 );
80 }
81  
82 28 \@enclosed;
83 }
84  
85 16 ▶sub display_name { shift->name }
Subroutine display_name: called CC=1 SCAR=0.0
Pod documented
86  
87 58 ▶sub in {
Subroutine in: called CC=3 SCAR=11.0
Pod documented
88 58 my ($self, $value) = @_;
89  
90 58 ▶ if (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 8 50 58
91 8 ▶ unless (grep { $value eq $_ } @LOCATIONS) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$value eq $_;} @LOCATIONS) 1 7 8
92 1 Raisin::log(warn => '`%s` should be one of: %s',
93 $self->name, join ', ', @LOCATIONS);
94 1 return;
95 }
96  
97 7 $self->{in} = $value;
98 }
99  
100 57 $self->{in};
101 }
102  
103 128 ▶sub validate {
Subroutine validate: called CC=19 SCAR=29.7
Pod documented
104 128 my ($self, $ref_value, $quiet) = @_;
105  
106 # Required and empty
107 # Only optional parameters can have default value
108 128 ▶ if ($self->required && !defined($$ref_value)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->required and not defined $$ref_value) 3 125 128
Condition100%3/3
$self->required and not defined $$ref_value
!ll&&!rl&&r
44813
MC/DC100%2/2
$self->required and not defined $$ref_value
$self->required not defined $$ref_value
Truth table100%3/3
A and not(B)
A= $self->requiredB= not defined $$ref_value
ABresult
0X0
100
111
109 3 ▶ Raisin::log(warn => '`%s` is required', $self->name) unless $quiet;
Branch: 1 branch
BranchTrueFalseTotal
unless $quiet 3 0 3
110 3 return;
111 }
112  
113 # Optional and empty
114 125 ▶ if (!$self->required && !defined($$ref_value)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->required or defined $$ref_value) 21 104 125
Condition100%3/3
$self->required or defined $$ref_value
l!l&&r!l&&!r
812321
MC/DC100%2/2
$self->required or defined $$ref_value
$self->required defined $$ref_value
Truth table100%3/3
A or B
A= $self->requiredB= defined $$ref_value
ABresult
1X1
011
000
115 21 Raisin::log(info => '`%s` optional and empty', $self->name);
116 21 return 1;
117 }
118  
119 # Type check
120 104 eval {
121 104 ▶ if ($self->type->has_coercion && $self->coerce) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->type->has_coercion and $self->coerce) 1 103 104
Condition66%2/3
$self->type->has_coercion and $self->coerce
!ll&&!rl&&r
10301
MC/DC50%1/2
$self->type->has_coercion and $self->coerce
$self->type->has_coercion $self->coerce
Truth table66%2/3
A and B
A= $self->type->has_coercionB= $self->coerce
ABresult
0X0
100
111
122 1 $$ref_value = $self->type->coerce($$ref_value);
123 }
124  
125 104 ▶ if ($self->type->isa('Moose::Meta::TypeConstraint')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->type->isa('Moose::Meta::TypeConstraint')) { } 0 104 104
126 0 $self->type->assert_valid($$ref_value);
127 }
128 else {
129 104 $$ref_value = $self->type->($$ref_value);
130 }
131 };
132 104 ▶ if (my $e = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = $@) 6 98 104
133 6 ▶ unless ($quiet) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($quiet) 5 1 6
134 5 Raisin::log(warn => 'Param `%s` didn\'t pass constraint `%s` with value "%s"',
135 $self->name, $self->type->name, $$ref_value);
136 }
137 6 return;
138 }
139  
140 # Nested
141 98 ▶ if ($self->type->name eq 'HashRef' && $self->enclosed) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->type->name eq 'HashRef' and $self->enclosed) { } 11 87 98
elsif ($self->regex and not $$ref_value =~ $self->regex) { } 3 163 166
Condition66%2/3
$self->type->name eq "HashRef" and $self->enclosed
!ll&&!rl&&r
87011
MC/DC50%1/2
$self->type->name eq "HashRef" and $self->enclosed
$self->type->name eq "HashRef" $self->enclosed
Truth table66%2/3
A and B
A= $self->type->name eq "HashRef"B= $self->enclosed
ABresult
0X0
100
111
142 11 for my $p (@{ $self->enclosed }) {
143 19 my $v = $$ref_value;
144  
145 19 ▶ if ($self->type->name eq 'HashRef') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->type->name eq "HashRef") 19 0 19
146 19 $v = $v->{ $p->name };
147 }
148  
149 19 ▶ return unless $p->validate(\$v, $quiet);
Branch: 1 branch
BranchTrueFalseTotal
unless $p->validate(\$v, $quiet) 4 15 19
150 }
151 }
152 # Regex
153 ▶ elsif ($self->regex && $$ref_value !~ $self->regex) {
Condition100%3/3
$self->regex and not $$ref_value =~ $self->regex
!ll&&!rl&&r
7953
MC/DC100%2/2
$self->regex and not $$ref_value =~ $self->regex
$self->regex not $$ref_value =~ $self->regex
Truth table100%3/3
A and not(B)
A= $self->regexB= not $$ref_value =~ $self->regex
ABresult
0X0
100
111
154 3 ▶ unless ($quiet) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($quiet) 3 0 3
155 3 Raisin::log(warn => 'Param `%s` didn\'t match regex `%s` with value "%s"',
156 $self->name, $self->regex, $$ref_value);
157 }
158 3 return;
159 }
160  
161 91 1;
162 }
163  
164 20 1;
165  
166 __END__
« blib/lib/Raisin.pm ↑ Summary blib/lib/Raisin/Routes.pm »