| 1 | package HTML::FormHandler::Field::Money; | ||||||||||
| 2 | # ABSTRACT: US currency-like values | ||||||||||
| 3 | 2 | $HTML::FormHandler::Field::Money::VERSION = '0.410002'; | |||||||||
| 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 | 9 | ▶ | sub get_class_messages { | ||||||||
|
Subroutine
get_class_messages: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 25 | 9 | my $self = shift; | |||||||||
| 26 | return { | ||||||||||
| 27 | 9 | %{ $self->next::method }, | |||||||||
| 28 | %$class_messages, | ||||||||||
| 29 | } | ||||||||||
| 30 | } | ||||||||||
| 31 | |||||||||||
| 32 | apply( | ||||||||||
| 33 | [ { # remove any leading currency symbol | ||||||||||
| 34 | 6 | ▶ | transform => sub { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||
| 35 | 6 | my ( $value, $field ) = @_; | |||||||||
| 36 | 6 | my $c = $field->currency_symbol; | |||||||||
| 37 | 6 | ▶ | $value =~ s/^\Q$c\E// if $c; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 38 | 6 | return $value; | |||||||||
| 39 | }, | ||||||||||
| 40 | }, | ||||||||||
| 41 | { # check number looks real, optionally allow comma digit groupings | ||||||||||
| 42 | 6 | ▶ | check => sub { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||
| 43 | 6 | my ( $value, $field ) = @_; | |||||||||
| 44 | 6 | ▶ | return $field->allow_commas | ||||||||
|
Branch: 1 branch
| |||||||||||
| 45 | ? $value =~ /^[-+]?(?:\d+|\d{1,3}(,\d{3})*)(?:\.\d+)?$/ | ||||||||||
| 46 | : $value =~ /^[-+]?\d+(?:\.\d+)?$/; | ||||||||||
| 47 | }, | ||||||||||
| 48 | 3 | ▶ | message => sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 49 | 3 | my ( $value, $field ) = @_; | |||||||||
| 50 | 3 | return [ $field->get_message('money_real'), $value ]; | |||||||||
| 51 | }, | ||||||||||
| 52 | }, | ||||||||||
| 53 | { # remove commas | ||||||||||
| 54 | 6 | ▶ | transform => sub { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||
| 55 | 6 | my ($value, $field) = @_; | |||||||||
| 56 | 6 | ▶ | $value =~ tr/,//d if $field->allow_commas; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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 | 3 | ▶ | message => sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 63 | 3 | my ( $value, $field ) = @_; | |||||||||
| 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__ | ||||||||||