Sha256: dbff41eb97fcad4df7a8991dee404feafe66dd053c6ff3ec3f2b4fdd53aefc2f

Contents?: true

Size: 775 Bytes

Versions: 51

Compression:

Stored size: 775 Bytes

Contents

module Scrivito
  module IntegerConversion
    INTEGER_STRING_REGEX = /\A-?\d+\z/.freeze
    RANGE = Range.new(-(2**53 - 1), (2**53 - 1)).freeze

    def self.deserialize_from_backend(backend_value)
      case backend_value
      when Integer, Float, INTEGER_STRING_REGEX
        value = backend_value.to_i
        RANGE.include?(value) ? value : nil
      when nil, String
        nil
      else
        raise Scrivito::InternalError,
            "invalid value #{backend_value} (class: #{backend_value.class})"
      end
    end

    def self.serialize_for_backend(value, attr_name)
      unless RANGE.include?(value)
        raise ClientError.new(
            "Value #{value} for attribute #{attr_name} is out of range: #{RANGE}")
      end

      value
    end
  end
end

Version data entries

51 entries across 51 versions & 1 rubygems

Version Path
scrivito_sdk-1.7.0 lib/scrivito/integer_conversion.rb
scrivito_sdk-1.7.0.rc2 lib/scrivito/integer_conversion.rb
scrivito_sdk-1.6.1 lib/scrivito/integer_conversion.rb
scrivito_sdk-1.6.1.rc1 lib/scrivito/integer_conversion.rb
scrivito_sdk-1.7.0.rc1 lib/scrivito/integer_conversion.rb
scrivito_sdk-1.6.0 lib/scrivito/integer_conversion.rb
scrivito_sdk-1.6.0.rc5 lib/scrivito/integer_conversion.rb
scrivito_sdk-1.6.0.rc4 lib/scrivito/integer_conversion.rb
scrivito_sdk-1.6.0.rc3 lib/scrivito/integer_conversion.rb
scrivito_sdk-1.6.0.rc2 lib/scrivito/integer_conversion.rb
scrivito_sdk-1.6.0.rc1 lib/scrivito/integer_conversion.rb