class CurrencyParser::Parser

Attributes

allow_negative[RW]
delimiter[R]
format[RW]
separator[R]

Public Class Methods

new(format = :de, *args) click to toggle source

Defaults:

  • :format - The default format is ‘:de`

  • :separator - The character to use after the integer part, default is ‘,’

  • :delimiter - The character to use between every 3 digits of the integer part, default ‘.’

# File lib/currency_parser.rb, line 12
def initialize(format = :de, *args)
  # NOTE defaults
  set_format(@format = format)
  @allow_negative = false

  opt = args.last.is_a?(Hash) ? args.pop : {}
  @allow_negative = opt[:allow_negative] if opt[:allow_negative] && (opt[:allow_negative] == true || opt[:allow_negative] == false)
end
to_us(value) click to toggle source
# File lib/currency_parser.rb, line 49
def to_us(value)
  new.to_us(value)
end

Public Instance Methods

to_us(value) click to toggle source

@param value [String] @param args [Hash] @option args [String] :format the format @return [String]

# File lib/currency_parser.rb, line 29
def to_us(value)
  value = value.gsub('-', '') unless allow_negative
  value = value.gsub('/', '')

  case format
  when :de
    int = value.gsub('.', '')

    if int.match(',').to_a.any?
      result = int.split(',')
      int = result[0]
      frac = result[1]
      return int + '.' + frac
    else
      int
    end
  end
end

Private Instance Methods

set_format(locale) click to toggle source
# File lib/currency_parser.rb, line 55
def set_format(locale)
  case locale
  when :de
    @separator = ','
    @delimiter = '.'
  else
    raise CurrencyParser::InvalidFormatError
  end
end