Class: CSVConverter::Converters::BigDecimalConverter

Inherits:
BaseConverter
  • Object
show all
Defined in:
lib/csv_converter/converters/big_decimal_converter.rb

Overview

Converts a string into a decimal number

Instance Attribute Summary

Attributes inherited from BaseConverter

#options, #raw_data

Instance Method Summary collapse

Methods inherited from BaseConverter

#data, #empty_value?, #initialize

Constructor Details

This class inherits a constructor from CSVConverter::Converters::BaseConverter

Instance Method Details

#callBigDecimal

Converts data into a BigDecimal. If the decimal separator is a comma it is replaced by a period before parsing.

Returns:

  • (BigDecimal)

    if an error occurs during conversion nil is returned.



12
13
14
15
16
# File 'lib/csv_converter/converters/big_decimal_converter.rb', line 12

def call
  call!
rescue CSVConverter::Error
  nullable_object
end

#call!BigDecimal

Converts data into a BigDecimal. If the decimal separator is a comma it is replaced by a period before parsing.

Returns:

  • (BigDecimal)

    if an error occurs during conversion an error is raised.



21
22
23
24
25
# File 'lib/csv_converter/converters/big_decimal_converter.rb', line 21

def call!
  BigDecimal(data.sub(',', '.'))
rescue StandardError => e
  raise CSVConverter::Error.new(e.message, options)
end