Class: NumbersInWords::Special

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
FractionParsing
Defined in:
lib/numbers_in_words/parsing/special.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from FractionParsing

#fraction, #possible_fraction?, #strip_punctuation, #text, #text_including_punctuation

Constructor Details

#initialize(that, only_compress) ⇒ Special

Returns a new instance of Special.



14
15
16
17
# File 'lib/numbers_in_words/parsing/special.rb', line 14

def initialize(that, only_compress)
  @that = that
  @only_compress = only_compress
end

Instance Attribute Details

#only_compressObject (readonly)

Returns the value of attribute only_compress.



12
13
14
# File 'lib/numbers_in_words/parsing/special.rb', line 12

def only_compress
  @only_compress
end

#thatObject (readonly)

Returns the value of attribute that.



12
13
14
# File 'lib/numbers_in_words/parsing/special.rb', line 12

def that
  @that
end

Instance Method Details

#callObject



19
20
21
22
23
24
25
# File 'lib/numbers_in_words/parsing/special.rb', line 19

def call
  float ||
    negative ||
    fraction(that) ||
    mixed_words_and_digits ||
    one
end

#check_one(txt) ⇒ Object



63
64
65
# File 'lib/numbers_in_words/parsing/special.rb', line 63

def check_one(txt)
  txt.match(/^one (#{POWERS_RX})$/)
end

#floatObject



27
28
29
# File 'lib/numbers_in_words/parsing/special.rb', line 27

def float
  text_including_punctuation.to_f if text =~ /^-?\d+(.\d+)?$/
end

#mixed_words_and_digitsObject



39
40
41
42
43
44
# File 'lib/numbers_in_words/parsing/special.rb', line 39

def mixed_words_and_digits
  return unless numeric?(that)

  in_words = that.split(' ').map { |word| numeric?(word) ? NumbersInWords.in_words(word) : word }.join(' ')
  ToNumber.new(in_words, only_compress).call
end

#negativeObject



31
32
33
34
35
36
37
# File 'lib/numbers_in_words/parsing/special.rb', line 31

def negative
  stripped = strip_minus text
  return unless stripped

  stripped_n = NumbersInWords.in_numbers(stripped, only_compress: only_compress)
  only_compress ? stripped_n.map { |k| k * -1 } : -1 * stripped_n
end

#numeric?(word) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/numbers_in_words/parsing/special.rb', line 46

def numeric?(word)
  word.match(/\d+/)
end

#oneObject



54
55
56
57
58
59
60
61
# File 'lib/numbers_in_words/parsing/special.rb', line 54

def one
  one = check_one text

  return unless one

  res = NumbersInWords.in_numbers(one[1])
  only_compress ? [res] : res
end

#strip_minus(txt) ⇒ Object



50
51
52
# File 'lib/numbers_in_words/parsing/special.rb', line 50

def strip_minus(txt)
  txt.gsub(/^minus/, '') if txt =~ /^minus/
end