Class: Humanized::Parser::NumericClass

Inherits:
Humanized::Parser show all
Defined in:
lib/more/humanized/parser/numeric.rb

Constant Summary

Constants inherited from Humanized::Parser

Numeric

Instance Method Summary collapse

Instance Method Details

#parse(string, options) ⇒ Object



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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/more/humanized/parser/numeric.rb', line 24

def parse(string, options)
  
  humanizer = options[:humanizer]
  if options.key? :default
    scope = [:numeric, :format]._[options[:format], :default]
  else
    scope = [:numeric, :format, :default]._
  end
  
  separator = humanizer.get(scope.separator)
  delimiter = humanizer.get(scope.delimiter)
  pedantic = options.fetch(:pedantic, false)
  
  result = Result.new(string, options)
  
  
  if pedantic
    regexp = Regexp.new([
        '^(-?)(\d{1,3}(?:',
        Regexp.escape(separator),
        '\d{3})?)(',
        Regexp.escape(delimiter),
        '(\d+))?$'
      ].join)
  else
    regexp = Regexp.new([
        '^(-?)([\d',
        Regexp.escape(separator),
        ']+)(',
        Regexp.escape(delimiter),
        '(\d+))?$'
      ].join)
  end
  
  m = regexp.match(string)
  
  if m.nil?
    return result
  end
  
  result.emit( BigDecimal( [ m[1], m[2].gsub(separator,''),  '.', m[4] ].join ), :precision => (m[4] ? m[4].length : 0) )
  
  return result
  
end

#providesObject



70
71
72
# File 'lib/more/humanized/parser/numeric.rb', line 70

def provides
  return [:numeric]
end