Class: Pema::IntType

Inherits:
Field
  • Object
show all
Defined in:
lib/primate/pema.rb

Instance Method Summary collapse

Instance Method Details

#parse(value, coerce = false) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/primate/pema.rb', line 50

def parse(value, coerce = false)
  return value if value.is_a?(Integer)

  if coerce
    case value
    when Float
      return value.to_i
    when String
      return 0 if value.empty?
      return Integer(value)
    else
      raise ValidationError, "cannot coerce #{value.class} to int"
    end
  end

  raise ValidationError, "expected integer, got #{value.class}"
rescue ArgumentError
  raise ValidationError, "cannot parse '#{value}' as integer"
end