Class: NumRepr::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/numrepr/money.rb,
lib/numrepr/format.rb,
lib/numrepr/decimal.rb,
lib/numrepr/percent.rb

Constant Summary collapse

DASH =
"-\u{2013}"
IDASH =
DASH
NEG_TRAILING =
true
NEG_PAREN =
true
GRP =
",.'\u{2019}\u{202f}"
DEC =
".,\u{b7}"

Class Method Summary collapse

Class Method Details

.into_d(str) ⇒ Object



31
32
33
34
35
36
# File 'lib/numrepr/format.rb', line 31

def into_d str
  digs = prepare_float str
  BigDecimal digs
rescue ArgumentError, NoMethodError
  raise ArgumentError, "Couldn't parse as a decimal: #{str}"
end

.into_decimal(str) ⇒ Object



268
269
270
271
272
273
# File 'lib/numrepr/decimal.rb', line 268

def into_decimal str
  digs = prepare_float str
  Decimal digs
rescue ArgumentError, NoMethodError
  raise ArgumentError, "Couldn't parse as a decimal: #{str}"
end

.into_f(str) ⇒ Object



24
25
26
27
28
29
# File 'lib/numrepr/format.rb', line 24

def into_f str
  digs = prepare_float str
  Float digs
rescue ArgumentError, NoMethodError
  raise ArgumentError, "Couldn't parse as a float: #{str}"
end

.into_i(str) ⇒ Object



17
18
19
20
21
22
# File 'lib/numrepr/format.rb', line 17

def into_i str
  check_string str
  Integer remove_grp str
rescue ArgumentError, NoMethodError
  raise ArgumentError, "Couldn't parse as an integer: #{str}"
end

.into_money(str) ⇒ Object



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/numrepr/money.rb', line 204

def into_money str
  check_string str
  ints, frac = decsplit str.dup
  unless ints =~ /\d/ then
    self::IDASH.each_char { |d|
      if ints.end_with? d then
        ints.slice! ints.length-1
        break
      end
    }
  end
  unless frac =~ /\d/ then
    self::DASH.each_char { |d|
      if frac.start_with? d then
        frac.slice! 0
        break
      end
    }
  end
  digs = remove_grps [ints, frac]
  if self::NEG_TRAILING then
    digs.sub! /(\d.*)-/ do "-#$1" end
  end
  if self::NEG_PAREN then
    digs.sub! /\((\d.*?)\)/ do "-#$1" end
  end
  Money.new digs
rescue ArgumentError, NoMethodError
  raise ArgumentError, "Couldn't parse as a monetary: #{str}"
end

.into_percent(str) ⇒ Object



194
195
196
197
198
199
200
201
# File 'lib/numrepr/percent.rb', line 194

def into_percent str
  check_string str
  ints, frac = decsplit str.dup
  digs = remove_grps [ints, frac]
  Percent.new digs
rescue ArgumentError, NoMethodError
  raise ArgumentError, "Couldn't parse as percents: #{str}"
end