MuForm/Field/Float.pm

filter: Statementstmt 98.0% 49 / 50 Branchbran 62.5% 10 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 9 / 9 Podpod 25.0% 1 / 4 totaltotal 87.3% 69 / 79 SCAR 22.1
CC
9
Cov
89%
validate
7.3
input_to_value
2.0
value_to_fif
2.0
CRAP 9.1
« MuForm/Merge.pm ↑ Summary MuForm/Meta.pm »
1 package Data::MuForm::Field::Float;
2 # ABSTRACT: validate a float value
3  
4 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Data::MuForm::Meta;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 extends 'Data::MuForm::Field::Text';
7 1 ▶use Types::Standard -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9  
10 1 has '+size' => ( default => 8 );
11 1 has 'precision' => ( is => 'rw', default => 2 );
12 1 has 'decimal_symbol' => ( is => 'rw', default => '.');
13 1 has 'decimal_symbol_for_db' => ( is => 'rw', default => '.');
14 1 ▶has '+transform_input_to_value' => ( default => sub { *input_to_value } );
Subroutine __ANON__: called CC=1 SCAR=0.0
15 1 ▶has '+transform_value_to_fif' => ( default => sub { *value_to_fif } );
Subroutine __ANON__: called CC=1 SCAR=0.0
16  
17 1 our $class_messages = {
18 'float_needed' => 'Must be a number. May contain numbers, +, - and decimal separator \'[_1]\'',
19 'float_size' => 'Total size of number must be less than or equal to {size}, but is {actual_size}',
20 'float_precision1' => 'May have only one digit after the decimal point, but has {num_digits:num}',
21 'float_precision2' => 'May have a maximum of {precision:num} digits after the decimal point, but has {num_digits:num}',
22 };
23  
24 sub get_class_messages {
25 3 ▶ my $self = shift;
Subroutine get_class_messages: called CC=1 SCAR=0.0
Pod undocumented
26 return {
27 3 %{ $self->next::method },
28 %$class_messages,
29 }
30 }
31  
32 sub input_to_value {
33 4 ▶ my ( $self, $value ) = @_;
Subroutine input_to_value: called CC=2 SCAR=7.0
Pod undocumented
34 4 ▶ return $value unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 4 4
35 4 $value =~ s/^\+//;
36 4 return $value;
37 }
38  
39 sub value_to_fif {
40 1 ▶ my ( $self, $value ) = @_;
Subroutine value_to_fif: called CC=2 SCAR=7.0
Pod undocumented
41 1 ▶ return $value unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 1 1
42 1 my $symbol = $self->decimal_symbol;
43 1 my $symbol_db = $self->decimal_symbol_for_db;
44 1 $value =~ s/\Q$symbol_db\E/$symbol/x;
45 1 return $value;
46 }
47  
48 sub validate {
49 4 ▶ my $field = shift;
Subroutine validate: called CC=7 SCAR=19.8
Pod documented
50  
51 #return unless $field->next::method;
52 4 my ($integer_part, $decimal_part) = ();
53 4 my $value = $field->value;
54 4 my $symbol = $field->decimal_symbol;
55 4 my $symbol_db = $field->decimal_symbol_for_db;
56  
57 4 ▶ if ($value =~ m/^-?([0-9]+)(\Q$symbol\E([0-9]+))?$/x) { # \Q ... \E - All the characters between the \Q and the \E are interpreted as literal characters.
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /^-?([0-9]+)(\Q$symbol\E([0-9]+))?$/x) { } 4 0 4
58 4 $integer_part = $1;
59 4 ▶ $decimal_part = defined $3 ? $3 : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $3 ? : 4 0 4
60 }
61 else {
62 0 return $field->add_error( $field->get_message('float_needed'), $symbol );
63 }
64  
65 # check total float size
66 4 ▶ if ( my $allowed_size = $field->size ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $allowed_size = $field->size) 4 0 4
67 4 my $total_size = length($integer_part) + length($decimal_part);
68 4 ▶ return $field->add_error( $field->get_message('float_size'),
Branch: 1 branch
BranchTrueFalseTotal
if $total_size > $allowed_size 1 3 4
69 size => $allowed_size, actual_size => $total_size )
70 if $total_size > $allowed_size;
71 }
72  
73 # check precision
74 3 ▶ if ( my $allowed_precision = $field->precision ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $allowed_precision = $field->precision) 3 0 3
75 3 ▶ return $field->add_error_nx(
Branch: 1 branch
BranchTrueFalseTotal
if length $decimal_part > $allowed_precision 1 2 3
76 $field->get_message('float_precision1'),
77 $field->get_message('float_precision2'),
78 $allowed_precision, precision => $allowed_precision, num_digits => length($decimal_part))
79 if length $decimal_part > $allowed_precision;
80 }
81  
82 # Inflate to database accepted format
83 2 $value =~ s/\Q$symbol\E/$symbol_db/x;
84 2 $field->value($value);
85  
86 2 return 1;
87 }
88  
89 1 1;
90  
91 __END__
92  
93 =pod
94  
95 =encoding UTF-8
96  
97 =head1 NAME
98  
99 Data::MuForm::Field::Float - validate a float value
100  
101 =head1 VERSION
102  
103 version 0.04
104  
105 =head1 DESCRIPTION
106  
107 This accepts a positive or negative float/integer. Negative numbers may
108 be prefixed with a dash. By default a max of eight digits including 2 precision
109 are accepted. Default decimal symbol is ','.
110 Widget type is 'text'.
111  
112 # For example 1234,12 has size of 6 and precision of 2
113 # and separator symbol of ','
114  
115 has_field 'test_result' => (
116 type => 'Float',
117 size => 8, # Total size of number including decimal part.
118 precision => 2, # Size of the part after decimal symbol.
119 decimal_symbol => '.', # Decimal symbol accepted in web page form
120 decimal_symbol_for_db => '.', # For inflation. Decimal symbol accepted in DB, which automatically converted to.
121 range_start => 0,
122 range_end => 100
123 );
124  
125 =head2 messages
126  
127 float_needed
128 float_size
129 float_precision1
130 float_precision2
131  
132 =head1 AUTHOR
133  
134 Gerda Shank
135  
136 =head1 COPYRIGHT AND LICENSE
137  
138 This software is copyright (c) 2017 by Gerda Shank.
139  
140 This is free software; you can redistribute it and/or modify it under
141 the same terms as the Perl 5 programming language system itself.
142  
143 =cut
« MuForm/Merge.pm ↑ Summary MuForm/Meta.pm »