module Scrivito module FloatConversion FLOAT_STRING_REGEX = /\A-?\d+(\.\d+)?\z/.freeze DEFAULT_VALUE = 0.0 def self.deserialize_from_backend(backend_value) case backend_value when Integer, Float, FLOAT_STRING_REGEX value = backend_value.to_f value.finite? ? value : DEFAULT_VALUE when nil, String DEFAULT_VALUE else raise Scrivito::InternalError, "invalid value #{backend_value} (class: #{backend_value.class})" end end def self.serialize_for_backend(value, attr_name) serialized_value = value.to_f unless serialized_value.finite? raise ClientError.new( "Value #{value} for attribute #{attr_name} is not supported.") end value end end end