Sha256: 1e126c050d1839e2c514ab62e35ca700cc1d47c29df1d249c77077f58d80884b

Contents?: true

Size: 788 Bytes

Versions: 27

Compression:

Stored size: 788 Bytes

Contents

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

Version data entries

27 entries across 27 versions & 1 rubygems

Version Path
scrivito_sdk-1.4.0.rc1 lib/scrivito/float_conversion.rb
scrivito_sdk-1.3.1 lib/scrivito/float_conversion.rb
scrivito_sdk-1.3.1.rc1 lib/scrivito/float_conversion.rb
scrivito_sdk-1.3.0 lib/scrivito/float_conversion.rb
scrivito_sdk-1.3.0.rc3 lib/scrivito/float_conversion.rb
scrivito_sdk-1.3.0.rc2 lib/scrivito/float_conversion.rb
scrivito_sdk-1.3.0.rc1 lib/scrivito/float_conversion.rb