FormHandler/Field/Money.pm

filter: Statementstmt 100% 32 / 32 Branchbran 83.3% 5 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 9 / 9 Podpod 0.0% 0 / 1 totaltotal 95.8% 46 / 48 CC 4 SCAR 4.1
CC
4
Cov
97%
__ANON__
2.0
__ANON__
2.0
__ANON__
2.0
CRAP 1.5
« FormHandler/Widget/Theme/Bootstrap.pm ↑ Summary FormHandler/Field/Boolean.pm »
1 package HTML::FormHandler::Field::Money;
2 # ABSTRACT: US currency-like values
3 2 $HTML::FormHandler::Field::Money::VERSION = '0.410001';
4 2 ▶use HTML::FormHandler::Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 extends 'HTML::FormHandler::Field::Text';
6  
7 2 has '+html5_type_attr' => ( default => 'number' );
8 2 has 'currency_symbol' => (
9 is => 'ro',
10 isa => 'Str',
11 default => '$',
12 );
13 2 has 'allow_commas' => (
14 is => 'ro',
15 isa => 'Bool',
16 default => 0,
17 );
18  
19 2 our $class_messages = {
20 'money_convert' => 'Value cannot be converted to money',
21 'money_real' => 'Value must be a real number',
22 };
23  
24 sub get_class_messages {
25 9 ▶ my $self = shift;
Subroutine get_class_messages: called CC=1 SCAR=0.0
Pod undocumented
26 return {
27 9 %{ $self->next::method },
28 %$class_messages,
29 }
30 }
31  
32 apply(
33 [ { # remove any leading currency symbol
34 transform => sub {
35 6 ▶ my ( $value, $field ) = @_;
Subroutine __ANON__: called CC=2 SCAR=7.0
36 6 my $c = $field->currency_symbol;
37 6 ▶ $value =~ s/^\Q$c\E// if $c;
Branch: 1 branch
BranchTrueFalseTotal
if $c 6 0 6
38 6 return $value;
39 },
40 },
41 { # check number looks real, optionally allow comma digit groupings
42 check => sub {
43 6 ▶ my ( $value, $field ) = @_;
Subroutine __ANON__: called CC=2 SCAR=6.9
44 6 ▶ return $field->allow_commas
Branch: 1 branch
BranchTrueFalseTotal
$field->allow_commas ? : 1 5 6
45 ? $value =~ /^[-+]?(?:\d+|\d{1,3}(,\d{3})*)(?:\.\d+)?$/
46 : $value =~ /^[-+]?\d+(?:\.\d+)?$/;
47 },
48 message => sub {
49 3 ▶ my ( $value, $field ) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
50 3 return [ $field->get_message('money_real'), $value ];
51 },
52 },
53 { # remove commas
54 transform => sub {
55 6 ▶ my ($value, $field) = @_;
Subroutine __ANON__: called CC=2 SCAR=6.9
56 6 ▶ $value =~ tr/,//d if $field->allow_commas;
Branch: 1 branch
BranchTrueFalseTotal
if $field->allow_commas 1 5 6
57 6 return $value;
58 },
59 },
60 { # convert to standard number, formatted to 2 decimal palaces
61 6 ▶ transform => sub { sprintf '%.2f', $_[0] },
Subroutine __ANON__: called CC=1 SCAR=0.0
62 message => sub {
63 3 ▶ my ( $value, $field ) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
64 3 return [ $field->get_message('money_convert'), $value ];
65 },
66 },
67 2 ]
68 );
69  
70  
71  
72 2 __PACKAGE__->meta->make_immutable;
73 2 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
74 2 1;
75  
76 __END__
77  
78 =pod
79  
80 =encoding UTF-8
81  
82 =head1 NAME
83  
84 HTML::FormHandler::Field::Money - US currency-like values
85  
86 =head1 VERSION
87  
88 version 0.410001
89  
90 =head1 DESCRIPTION
91  
92 Validates that a positive or negative real value is entered.
93 Formatted with two decimal places.
94  
95 Uses a period for the decimal point. Widget type is 'text'.
96  
97 If form has 'is_html5' flag active it will render <input type="number" ... />
98 instead of type="text"
99  
100 =head1 ATTRIBUTES
101  
102 =head2
103  
104 =over
105  
106 =item currency_symbol
107  
108 Currency symbol to remove from start of input if found, default is dollar
109 C<$>.
110  
111 =item allow_commas
112  
113 Allow commas in input for digit grouping? Digits are grouped into groups of 3,
114 for example C<1,000,000,000>. Defaults to I<false>.
115  
116 =back
117  
118 =head1 AUTHOR
119  
120 FormHandler Contributors - see HTML::FormHandler
121  
122 =head1 COPYRIGHT AND LICENSE
123  
124 This software is copyright (c) 2017 by Gerda Shank.
125  
126 This is free software; you can redistribute it and/or modify it under
127 the same terms as the Perl 5 programming language system itself.
128  
129 =cut
« FormHandler/Widget/Theme/Bootstrap.pm ↑ Summary FormHandler/Field/Boolean.pm »