Sha256: 464f4843151b39e7cc8861ed7601dc4a8e4ea20b91bee9a7684d73ed4587057e
Contents?: true
Size: 1.14 KB
Versions: 2
Compression:
Stored size: 1.14 KB
Contents
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 && !type.in?(VALID_TYPES) 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
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
kredis-0.2.3 | lib/kredis/type_casting.rb |
kredis-0.2.2 | lib/kredis/type_casting.rb |