Class: NumbersInWords::Special
- Inherits:
-
Object
- Object
- NumbersInWords::Special
show all
- Extended by:
- Forwardable
- Includes:
- FractionParsing
- Defined in:
- lib/numbers_in_words/parsing/special.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
#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_compress ⇒ Object
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
|
#that ⇒ Object
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
#call ⇒ Object
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
|
#float ⇒ Object
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_digits ⇒ Object
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
|
#negative ⇒ Object
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
46
47
48
|
# File 'lib/numbers_in_words/parsing/special.rb', line 46
def numeric?(word)
word.match(/\d+/)
end
|
#one ⇒ Object
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
|