Class: InCSV::Types::Currency

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

Overview

Represents a currency value, without its symbol/identifier, stored as a DECIMAL(10, 2) to avoid rounding errors.

Not storing the identifier is an issue that should be resolved at some point, ideally; it's obviously an issue in files that have multiple currencies in the same column.

Constant Summary collapse

MATCH_EXPRESSION =

A regular expression which matches all supported currency types.

/\A(\$|£)([0-9,\.]+)\z/

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

Strip the currency symbol, and remove any comma separators. This creates an issue with locales other than English, in which commas are used for decimal points, but this will work for English.


28
29
30
31
32
33
34
# File 'lib/incsv/types/currency.rb', line 28

def self.clean_value(value)
  return unless value

  value.strip.match(MATCH_EXPRESSION) do |match|
    BigDecimal(match[2].delete(","))
  end
end

.for_databaseObject

What type of column to create in the database.


14
15
16
# File 'lib/incsv/types/currency.rb', line 14

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

Instance Method Details

#match?Boolean

Returns true if the given value is a supported currency type, or false otherwise.


20
21
22
# File 'lib/incsv/types/currency.rb', line 20

def match?
  value.strip.match(MATCH_EXPRESSION)
end