lib/Form/Sensible/Field/Number.pm

filter: Statementstmt 97.0% 66 / 68 Branchbran 79.4% 27 / 34 Conditioncond 68.0% 17 / 25 MC/DCmcdc 50.0% 8 / 16 Subroutinesub 100% 8 / 8 Podpod 100% 5 / 5 totaltotal 83.9% 131 / 156 CC 27 SCAR 22.7
CC
27
Cov
87%
get_potential_values
14.3
__ANON__
12.0
in_step
4.0
CRAP 9.7
« lib/Form/Sensible/Renderer/HTML/RenderedForm.pm ↑ Summary lib/Form/Sensible/Validator/Result.pm »
1 package Form::Sensible::Field::Number;
2  
3 16 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 16 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 16 extends 'Form::Sensible::Field';
6  
7 16 has 'integer_only' => (
8 is => 'rw',
9 isa => 'Bool',
10 required => 1,
11 default => 0,
12 );
13  
14 16 has 'lower_bound' => (
15 is => 'rw',
16 isa => 'Num',
17 required => 0,
18 );
19  
20 16 has 'upper_bound' => (
21 is => 'rw',
22 isa => 'Num',
23 required => 0,
24 );
25  
26 16 has 'step' => (
27 is => 'rw',
28 isa => 'Num',
29 required => 0,
30 );
31  
32  
33 39 ▶around 'validate' => sub {
Subroutine __ANON__: called CC=12 SCAR=24.9
34 39 my $orig = shift;
35 39 my $self = shift;
36  
37 39 my @errors;
38 39 push @errors, $self->$orig(@_);
39  
40 39 my $regex = qr/^[-+]? # Sign
41 (?: [0-9]+ # Integer portion
42 (?: \. [0-9]* )? # Fractional portion
43 | \. [0-9]+ # Just a decimal
44 )
45 $/xms;
46  
47 39 ▶ if (defined($self->validation->{'regex'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->validation->{"regex"}) 6 33 39
48 6 $regex = $self->validation->{'regex'};
49 6 ▶ if (ref($regex) ne 'Regexp') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $regex ne "Regexp") 0 6 6
50 0 $regex = qr/$regex/;
51 }
52 }
53  
54 39 ▶ if ($self->value !~ $regex ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->value =~ /$regex/) 1 38 39
55 1 push @errors, "_FIELDNAME_ is not a number";
56 }
57  
58 39 ▶ if (defined($self->lower_bound) && $self->value < $self->lower_bound) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->lower_bound and $self->value < $self->lower_bound) 1 38 39
Condition100%3/3
defined $self->lower_bound and $self->value < $self->lower_bound
!ll&&!rl&&r
17211
MC/DC100%2/2
defined $self->lower_bound and $self->value < $self->lower_bound
defined $self->lower_bound $self->value < $self->lower_bound
Truth table100%3/3
A and B
A= defined $self->lower_boundB= $self->value < $self->lower_bound
ABresult
0X0
100
111
59 1 push @errors, "_FIELDNAME_ is lower than the minimum allowed value";
60 }
61 39 ▶ if (defined($self->upper_bound) && $self->value > $self->upper_bound) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->upper_bound and $self->value > $self->upper_bound) 1 38 39
Condition100%3/3
defined $self->upper_bound and $self->value > $self->upper_bound
!ll&&!rl&&r
17211
MC/DC100%2/2
defined $self->upper_bound and $self->value > $self->upper_bound
defined $self->upper_bound $self->value > $self->upper_bound
Truth table100%3/3
A and B
A= defined $self->upper_boundB= $self->value > $self->upper_bound
ABresult
0X0
100
111
62 1 push @errors, "_FIELDNAME_ is higher than the maximum allowed value";
63 }
64  
65 39 ▶ if ( $self->integer_only && $self->value != int($self->value)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->integer_only and $self->value != int $self->value) 4 35 39
Condition100%3/3
$self->integer_only and $self->value != int $self->value
!ll&&!rl&&r
17184
MC/DC100%2/2
$self->integer_only and $self->value != int $self->value
$self->integer_only $self->value != int $self->value
Truth table100%3/3
A and B
A= $self->integer_onlyB= $self->value != int $self->value
ABresult
0X0
100
111
66 4 push @errors, "_FIELDNAME_ must be an integer.";
67 }
68  
69 ## we ran the gauntlet last check is to see if value is in step.
70 39 ▶ if (defined($self->step) && !$self->in_step()) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->step and not $self->in_step) 5 34 39
Condition100%3/3
defined $self->step and not $self->in_step
!ll&&!rl&&r
2865
MC/DC100%2/2
defined $self->step and not $self->in_step
defined $self->step not $self->in_step
Truth table100%3/3
A and not(B)
A= defined $self->stepB= not $self->in_step
ABresult
0X0
100
111
71 5 push @errors, "_FIELDNAME_ must be a multiple of " . $self->step;
72 }
73 39 return @errors;
74 16 };
75  
76  
77 ## this is used when generating a slider or select of valid values.
78 4 ▶sub get_potential_values {
Subroutine get_potential_values: called CC=11 SCAR=26.6
Pod documented
79 4 my ($self, $step, $lower_bound, $upper_bound) = @_;
80  
81 4 ▶ if (!$step) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($step) 4 0 4
82 4 ▶ $step = $self->step || 1;
Condition50%1/2
$self->step || 1
l!l
40
MC/DC0%0/1
$self->step || 1
$self->step
Truth table50%1/2
A
A= $self->step
Aresult
11
00
83 }
84 4 ▶ if (!defined($lower_bound) && defined $self->lower_bound ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $lower_bound and defined $self->lower_bound) 4 0 4
Condition33%1/3
not defined $lower_bound and defined $self->lower_bound
!ll&&!rl&&r
004
MC/DC0%0/2
not defined $lower_bound and defined $self->lower_bound
not defined $lower_bound defined $self->lower_bound
Truth table33%1/3
not(A) and B
A= not defined $lower_boundB= defined $self->lower_bound
ABresult
0X0
100
111
85 4 $lower_bound = $self->lower_bound;
86 }
87 4 ▶ if (!defined($upper_bound) && defined $self->upper_bound ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $upper_bound and defined $self->upper_bound) 4 0 4
Condition33%1/3
not defined $upper_bound and defined $self->upper_bound
!ll&&!rl&&r
004
MC/DC0%0/2
not defined $upper_bound and defined $self->upper_bound
not defined $upper_bound defined $self->upper_bound
Truth table33%1/3
not(A) and B
A= not defined $upper_boundB= defined $self->upper_bound
ABresult
0X0
100
111
88 4 $upper_bound = $self->upper_bound;
89 }
90  
91 # If either the lower or upper bound is not set, no potential values
92 4 ▶ return if ! defined $upper_bound || ! defined $lower_bound;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $upper_bound and defined $lower_bound 0 4 4
Condition33%1/3
defined $upper_bound and defined $lower_bound
!ll&&!rl&&r
004
MC/DC0%0/2
defined $upper_bound and defined $lower_bound
defined $upper_bound defined $lower_bound
Truth table33%1/3
A and B
A= defined $upper_boundB= defined $lower_bound
ABresult
0X0
100
111
93  
94 4 my $value = $lower_bound;
95  
96 ## this check ensures that we start with a value that is within our
97 ## bounds. If $self->lower_bound does not lie on a step boundary,
98 ## and we generated all our numbers from lower_bound, we would be
99 ## producing a bunch of options that were always invalid.
100 ## Technically speaking, we shouldn't have a lower bound that is invalid
101 ## but who are we kidding? It will happen.
102  
103 4 ▶ if (!$self->in_step($value, $step)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->in_step($value, $step)) 4 0 4
104 # lower bound doesn't lie on a step boundary. Bump $div by 1 and
105 # multiply by step. Should be the first value that lies above our
106 # provided bound.
107 4 my $div = $value / $step;
108  
109 4 $value = (int($div)+1) * $step;
110 }
111  
112 4 my @vals;
113 4 ▶ while ($value <= $upper_bound) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value <= $upper_bound) 28 4 32
114 28 push @vals, $value;
115 28 $value += $step;
116 }
117 4 return @vals;
118 }
119  
120 ## this allows a Number to behave like a Select if used that way.
121 4 ▶sub get_options {
Subroutine get_options: called CC=2 SCAR=7.4
Pod documented
122 4 my ($self) = @_;
123  
124 4 my @values = $self->get_potential_values();
125 4 ▶ return [ map { { name => $_, value => $_ } } @values ] if @values;
Branch: 1 branch
BranchTrueFalseTotal
if @values 4 0 4
126 0 return $self->value;
127 }
128  
129 6 ▶sub accepts_multiple {
Subroutine accepts_multiple: called CC=1 SCAR=0.0
Pod documented
130 6 my ($self) = @_;
131  
132 6 return 0;
133 }
134  
135 15 ▶sub in_step {
Subroutine in_step: called CC=4 SCAR=13.9
Pod documented
136 15 my ($self, $value, $step) = @_;
137  
138 15 ▶ if (!$step) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($step) 11 4 15
139 11 ▶ $step = $self->step || 1;
Condition50%1/2
$self->step || 1
l!l
110
MC/DC0%0/1
$self->step || 1
$self->step
Truth table50%1/2
A
A= $self->step
Aresult
11
00
140 }
141 15 ▶ if (!defined($value)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 11 4 15
142 11 $value = $self->value;
143 }
144  
145 ## we have to do the step check this way, because % will not deal with
146 ## a fractional step value.
147 15 my $div = $value / $step;
148 15 return ($div == int($div));
149  
150 }
151  
152 2 ▶sub get_additional_configuration {
Subroutine get_additional_configuration: called CC=2 SCAR=6.9
Pod documented
153 2 my $self = shift;
154  
155 2 ▶ return { map { defined ($self->$_) ? ( $_ => $self->$_ ) : () }
Branch: 1 branch
BranchTrueFalseTotal
defined $self->$_ ? : 5 3 8
156 qw/step lower_bound upper_bound integer_only/
157 };
158 }
159  
160 16 __PACKAGE__->meta->make_immutable;
161 16 1;
162  
163 __END__
« lib/Form/Sensible/Renderer/HTML/RenderedForm.pm ↑ Summary lib/Form/Sensible/Validator/Result.pm »