MuForm/Field/Currency.pm

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