Class: InCSV::Types::Decimal

Inherits:
ColumnType show all
Defined in:
lib/incsv/types/decimal.rb

Overview

Represents a fixed-precision decimal number.

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ColumnType

#clean_value, #initialize, name

Constructor Details

This class inherits a constructor from InCSV::ColumnType

Class Method Details

.clean_value(value) ⇒ Object

Converts values to BigDecimal format before storage.



23
24
25
# File 'lib/incsv/types/decimal.rb', line 23

def self.clean_value(value)
  BigDecimal(value.strip)
end

.for_databaseObject

What type of column to create in the database.



6
7
8
# File 'lib/incsv/types/decimal.rb', line 6

def self.for_database
  "DECIMAL(15,10)"
end

Instance Method Details

#match?Boolean

Returns true if the supplied value is a decimal number or an integer, since integers are also valid decimals.

This allows for columns that contain e.g.:

123.45 123 128.2

Returns:

  • (Boolean)


18
19
20
# File 'lib/incsv/types/decimal.rb', line 18

def match?
  value.strip.match(/\A[0-9]+\.?([0-9]+)?\z/)
end