Class: Monies::Parser

Inherits:
StringScanner
  • Object
show all
Defined in:
lib/monies/parser.rb

Instance Method Summary collapse

Instance Method Details

#parseObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/monies/parser.rb', line 5

def parse
  if comma_decimal_separator?
    @decimal_separator = COMMA

    @thousands_separator = POINT_THINSP
  else
    @decimal_separator = POINT

    @thousands_separator = COMMA
  end

  parse_minus_sign
  parse_currency_symbol
  parse_integral_digits
  parse_fractional_digits
  parse_space
  parse_currency_code

  if @integral_digits.nil?
    raise ArgumentError, "can't parse #{string.inspect}"
  end

  if @fractional_digits.nil?
    value = @integral_digits.to_i

    scale = 0
  else
    value = (@integral_digits + @fractional_digits).to_i

    scale = @fractional_digits.length
  end

  currency = if @currency_code
    @currency_code
  elsif @currency_symbol
    Monies.symbols.fetch(@currency_symbol)
  elsif Monies.currency
    Monies.currency
  else
    raise ArgumentError, "can't parse #{string.inspect} without currency"
  end

  if @minus_sign
    -Monies.new(value, scale, currency)
  else
    Monies.new(value, scale, currency)
  end
end