Sha256: 8d52328e1f9e9b32afbaa628d9b9e0e9421210a6df0d3ec6836364f8588b52b6
Contents?: true
Size: 1.16 KB
Versions: 1
Compression:
Stored size: 1.16 KB
Contents
require "json" module Kredis::TypeCasting class InvalidType < StandardError; end VALID_TYPES = %i[ string integer decimal float boolean datetime json ] def type_to_string(value) case value when nil "" when Integer value.to_s when BigDecimal value.to_d when Float value.to_s when TrueClass, FalseClass value ? "t" : "f" when Time, DateTime, ActiveSupport::TimeWithZone value.iso8601(9) when Hash JSON.dump(value) else value end end def string_to_type(value, type) raise InvalidType if type && !VALID_TYPES.include?(type) case type when nil, :string then value when :integer then value.to_i when :decimal then value.to_d when :float then value.to_f when :boolean then value == "t" ? true : false when :datetime then Time.iso8601(value) when :json then JSON.load(value) end if value.present? end def types_to_strings(values) Array(values).flatten.map { |value| type_to_string(value) } end def strings_to_types(values, type) Array(values).flatten.map { |value| string_to_type(value, type) } end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
kredis-0.3.0 | lib/kredis/type_casting.rb |