Class: Tabledata::ColumnDefinition::IntegerProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/tabledata/column_definition.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ IntegerProcessor


41
42
43
44
45
46
# File 'lib/tabledata/column_definition.rb', line 41

def initialize(options)
  options     = options.dup
  value_range = options.delete(:between)
  @min        = options.delete(:min) || (value_range && value_range.begin) || 0
  @max        = options.delete(:max) || (value_range && value_range.end) || 0
end

Instance Method Details

#call(value, errors) ⇒ Object


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/tabledata/column_definition.rb', line 48

def call(value, errors)
  case value
    when /\A\s*\z/
      processed = nil
    when String
      begin
        processed = Integer(value, 10)
      rescue ArgumentError
        processed = nil
        errors << [:not_an_integer, {value: value}]
      end
    when Integer
      processed = value
    when Numeric
      processed = value.round
      difference = value-processed
      errors << [:not_an_integral_number, {rounded: processed, unrounded: value, difference: difference, absolute_difference: difference.abs}] if difference.abs > Float::EPSILON
    else
      begin
        processed = Integer(value)
      rescue ArgumentError
        errors << [:invalid_input, {value: value}]
      end
  end

  if processed
    errors << [:too_small, {min: @min, actual: processed}] if @min_length && processed < @min_length
    errors << [:too_big, {max: @max, actual: processed}] if @max_length && processed > @max_length
  end

  processed
end