| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | # ABSTRACT: Decimal Directive for Validation Class Field Definitions | 
| 2 |  |  |  |  |  |  |  | 
| 3 |  |  |  |  |  |  | package Validation::Class::Directive::Decimal; | 
| 4 |  |  |  |  |  |  |  | 
| 5 | 109 |  |  | 109 |  | 53489 | use strict; | 
|  | 109 |  |  |  |  | 281 |  | 
|  | 109 |  |  |  |  | 3166 |  | 
| 6 | 109 |  |  | 109 |  | 574 | use warnings; | 
|  | 109 |  |  |  |  | 229 |  | 
|  | 109 |  |  |  |  | 2832 |  | 
| 7 |  |  |  |  |  |  |  | 
| 8 | 109 |  |  | 109 |  | 578 | use base 'Validation::Class::Directive'; | 
|  | 109 |  |  |  |  | 280 |  | 
|  | 109 |  |  |  |  | 11014 |  | 
| 9 |  |  |  |  |  |  |  | 
| 10 | 109 |  |  | 109 |  | 824 | use Validation::Class::Util; | 
|  | 109 |  |  |  |  | 313 |  | 
|  | 109 |  |  |  |  | 735 |  | 
| 11 |  |  |  |  |  |  |  | 
| 12 |  |  |  |  |  |  | our $VERSION = '7.900059'; # VERSION | 
| 13 |  |  |  |  |  |  |  | 
| 14 |  |  |  |  |  |  |  | 
| 15 |  |  |  |  |  |  | has 'mixin'   => 1; | 
| 16 |  |  |  |  |  |  | has 'field'   => 1; | 
| 17 |  |  |  |  |  |  | has 'multi'   => 0; | 
| 18 |  |  |  |  |  |  | has 'message' => '%s requires a valid decimal number'; | 
| 19 |  |  |  |  |  |  |  | 
| 20 |  |  |  |  |  |  | sub validate { | 
| 21 |  |  |  |  |  |  |  | 
| 22 | 4 |  |  | 4 | 0 | 15 | my ($self, $proto, $field, $param) = @_; | 
| 23 |  |  |  |  |  |  |  | 
| 24 | 4 | 50 | 33 |  |  | 26 | if (defined $field->{decimal} && defined $param) { | 
| 25 |  |  |  |  |  |  |  | 
| 26 |  |  |  |  |  |  | # checks for a valid decimal. Both the sign and exponent are optional | 
| 27 |  |  |  |  |  |  |  | 
| 28 |  |  |  |  |  |  | # 0 => Any number of decimal places, including none | 
| 29 |  |  |  |  |  |  | # 1 => Any number of decimal places greater than 0, or a float|double | 
| 30 |  |  |  |  |  |  | # 2 => Exactly that many number of decimal places | 
| 31 |  |  |  |  |  |  |  | 
| 32 | 4 | 50 | 33 |  |  | 41 | if ($field->{required} || $param) { | 
| 33 |  |  |  |  |  |  |  | 
| 34 | 4 |  |  |  |  | 10 | my $type = $field->{decimal}; | 
| 35 |  |  |  |  |  |  |  | 
| 36 | 4 |  |  |  |  | 7 | my $lnum = '[0-9]+'; | 
| 37 | 4 |  |  |  |  | 10 | my $dnum = "[0-9]*[\.]${lnum}"; | 
| 38 | 4 |  |  |  |  | 11 | my $sign = '[+-]?'; | 
| 39 | 4 |  |  |  |  | 9 | my $exp  = "(?:[eE]${sign}${lnum})?"; | 
| 40 |  |  |  |  |  |  |  | 
| 41 | 4 |  |  |  |  | 5 | my $dre; | 
| 42 |  |  |  |  |  |  |  | 
| 43 | 4 | 50 |  |  |  | 17 | if ($type == 0) { | 
|  |  | 50 |  |  |  |  |  | 
| 44 | 0 |  |  |  |  | 0 | $dre = qr/^${sign}(?:${lnum}|${dnum})${exp}$/; | 
| 45 |  |  |  |  |  |  | } | 
| 46 |  |  |  |  |  |  |  | 
| 47 |  |  |  |  |  |  | elsif ($type == 1) { | 
| 48 | 4 |  |  |  |  | 64 | $dre = qr/^${sign}${dnum}${exp}$/; | 
| 49 |  |  |  |  |  |  | } | 
| 50 |  |  |  |  |  |  |  | 
| 51 |  |  |  |  |  |  | else { | 
| 52 | 0 |  |  |  |  | 0 | $type = "[0-9]\{${type}}"; | 
| 53 | 0 |  |  |  |  | 0 | $dnum = "(?:[0-9]*[\.]${type}|${lnum}[\.]${type})"; | 
| 54 | 0 |  |  |  |  | 0 | $dre  = qr/^${sign}${dnum}${exp}$/; | 
| 55 |  |  |  |  |  |  | } | 
| 56 |  |  |  |  |  |  |  | 
| 57 | 4 | 100 |  |  |  | 47 | $self->error($proto, $field) unless $param =~ $dre; | 
| 58 |  |  |  |  |  |  |  | 
| 59 |  |  |  |  |  |  | } | 
| 60 |  |  |  |  |  |  |  | 
| 61 |  |  |  |  |  |  | } | 
| 62 |  |  |  |  |  |  |  | 
| 63 | 4 |  |  |  |  | 13 | return $self; | 
| 64 |  |  |  |  |  |  |  | 
| 65 |  |  |  |  |  |  | } | 
| 66 |  |  |  |  |  |  |  | 
| 67 |  |  |  |  |  |  | 1; | 
| 68 |  |  |  |  |  |  |  | 
| 69 |  |  |  |  |  |  | __END__ |