Class: Pema::FloatType

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

Instance Method Summary collapse

Instance Method Details

#parse(value, coerce = false) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/primate/pema.rb', line 72

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

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

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