MuForm/Field/Integer.pm

filter: Statementstmt 96.8% 31 / 32 Branchbran 75.0% 12 / 16 Conditioncond 100% 6 / 6 MC/DCmcdc 100% 4 / 4 Subroutinesub 100% 4 / 4 Podpod 50.0% 1 / 2 totaltotal 90.6% 58 / 64 SCAR 24.1
CC
11
Cov
91%
validate
11.3
BEGIN
1.0
BEGIN
1.0
CRAP 11.1
« MuForm/Field/Compound.pm ↑ Summary MuForm/Field/Multiple.pm »
1 package Data::MuForm::Field::Integer;
2 # ABSTRACT: validate an integer value
3  
4 8 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 ▶use Data::MuForm::Meta;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 8 extends 'Data::MuForm::Field::Text';
7  
8 8 has '+html5_input_type' => ( default => 'number' );
9 8 has '+size' => ( default => 8 );
10  
11 8 has 'range_start' => ( is => 'rw' );
12 8 has 'range_end' => ( is => 'rw' );
13  
14 8 our $class_messages = {
15 'integer_needed' => 'Value must be an integer',
16 'range_too_low' => 'Value must be greater than or equal to [_1]',
17 'range_too_high' => 'Value must be less than or equal to [_1]',
18 'range_incorrect' => 'Value must be between [_1] and [_2]',
19 };
20  
21 sub get_class_messages {
22 6 ▶ my $self = shift;
Subroutine get_class_messages: called CC=1 SCAR=0.0
Pod undocumented
23 return {
24 6 %{ $self->next::method },
25 %$class_messages,
26 }
27 }
28  
29 sub validate {
30 19 ▶ my $field = shift;
Subroutine validate: called CC=11 SCAR=24.2
Pod documented
31  
32 19 my $value = $field->value;
33 19 ▶ return 1 unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 19 19
34  
35 19 $value =~ s/^\+//;
36 19 $field->value($value);
37  
38 19 ▶ unless ( $value =~ /^-?[0-9]+$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($value =~ /^-?[0-9]+$/) 2 17 19
39 2 $field->add_error($field->get_message('integer_needed'));
40 }
41  
42 19 my $low = $field->range_start;
43 19 my $high = $field->range_end;
44  
45 19 ▶ if ( defined $low && defined $high ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $low and defined $high) 11 8 19
Condition100%3/3
defined $low and defined $high
!ll&&!rl&&r
6211
MC/DC100%2/2
defined $low and defined $high
defined $low defined $high
Truth table100%3/3
A and B
A= defined $lowB= defined $high
ABresult
0X0
100
111
46 return
47 11 ▶ $value >= $low && $value <= $high ? 1 :
Branch: 1 branch
BranchTrueFalseTotal
$value >= $low && $value <= $high ? : 8 3 11
Condition100%3/3
$value >= $low and $value <= $high
!ll&&!rl&&r
128
MC/DC100%2/2
$value >= $low and $value <= $high
$value >= $low $value <= $high
Truth table100%3/3
A and B
A= $value >= $lowB= $value <= $high
ABresult
0X0
100
111
48 $field->add_error( $field->get_message('range_incorrect'), low => $low, high => $high );
49 }
50  
51 8 ▶ if ( defined $low ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $low) 2 6 8
52 return
53 2 ▶ $value >= $low ? 1 :
Branch: 1 branch
BranchTrueFalseTotal
$value >= $low ? : 1 1 2
54 $field->add_error( $field->get_message('range_too_low'), low => $low );
55 }
56  
57 6 ▶ if ( defined $high ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $high) 0 6 6
58 return
59 0 ▶ $value <= $high ? 1 :
Branch: 1 branch
BranchTrueFalseTotal
$value <= $high ? : 0 0 0
60 $field->add_error( $field->get_message('range_too_high'), high => $high );
61 }
62  
63 6 return 1;
64 }
65  
66  
67  
68 8 1;
69  
70 __END__
71  
72 =pod
73  
74 =encoding UTF-8
75  
76 =head1 NAME
77  
78 Data::MuForm::Field::Integer - validate an integer value
79  
80 =head1 VERSION
81  
82 version 0.04
83  
84 =head1 DESCRIPTION
85  
86 This accepts a positive or negative integer. Negative integers may
87 be prefixed with a dash. By default a max of eight digits are accepted.
88 Widget type is 'text'.
89  
90 If form has 'is_html5' flag active it will render <input type="number" ... />
91 instead of type="text"
92  
93 The 'range_start' and 'range_end' attributes may be used to limit valid numbers.
94  
95 =head1 AUTHOR
96  
97 Gerda Shank
98  
99 =head1 COPYRIGHT AND LICENSE
100  
101 This software is copyright (c) 2017 by Gerda Shank.
102  
103 This is free software; you can redistribute it and/or modify it under
104 the same terms as the Perl 5 programming language system itself.
105  
106 =cut
« MuForm/Field/Compound.pm ↑ Summary MuForm/Field/Multiple.pm »