| 1 | package ODS::Table::Column::Float; | ||
| 2 | |||
| 3 | extends 'ODS::Table::Column::Base'; | ||
| 4 | |||
| 5 | use ODS::Utils qw/error/; | ||
| 6 | |||
| 7 | has precision => isa(integer); | ||
| 8 | |||
| 9 | has number => isa(boolean); | ||
| 10 | |||
| 11 | sub validation { | ||
| 12 | if (ref($_[1]) || ${$_[1]} !~ m/\d+(\.\d+)?/) { | ||
| 13 | croak sprintf "The value passed to the %s column does not match the float constraint.", | ||
| 14 | $_[0]->name; | ||
| 15 | } | ||
| 16 | return $_[1]; | ||
| 17 | } | ||
| 18 | |||
| 19 | sub inflation { | ||
| 20 | my ($self, $value) = @_; | ||
| 21 | if ($self->precision) { | ||
| 22 | my $precision = '%.' . $self->precision . 'f'; | ||
| 23 | $value = sprintf($precision, $value); | ||
| 24 | } | ||
| 25 | return $value; | ||
| 26 | } | ||
| 27 | |||
| 28 | sub deflation { | ||
| 29 | my ($self, $value) = @_; | ||
| 30 | if ($_[0]->number && $_[0]->precision) { | ||
| 31 | $value = 0 + $value; | ||
| 32 | } | ||
| 33 | return $value; | ||
| 34 | } | ||
| 35 | |||
| 36 | 1; | ||
| 37 | |||
| 38 | __END__ |