Class: Numerical::Decoder

Inherits:
Object
  • Object
show all
Defined in:
lib/numerical/decoder.rb

Constant Summary collapse

NUMBERS =
Numerical::Constants::NUMBERS
SHORT_FORM =
Numerical::Constants::SHORT_FORM

Instance Method Summary collapse

Instance Method Details

#decode(value) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/numerical/decoder.rb', line 7

def decode(value)
  result = 0

  current = 0

  value.gsub(/(\d),(\d)/, "\\1\\2").split(/[ \-,]+/).each do |word|
    if /^\d+$/.match word
      current += word.to_i
    elsif NUMBERS.include? word
      current += NUMBERS[word]
    elsif "hundred" == word
      current *= 100
    elsif SHORT_FORM.include? word
      result += current * SHORT_FORM[word]
      current = 0
    elsif "and" == word
      # ...
    else
      return nil
    end
  end

  result += current
end