Sha256: 5b96bd8998b38b71440f6f4a46dfcc513a67533dce7f4ed1c74e99f229bd529c
Contents?: true
Size: 1.13 KB
Versions: 2
Compression:
Stored size: 1.13 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.to_f 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.at(value.to_i) 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.1 | lib/kredis/type_casting.rb |
kredis-0.2.0 | lib/kredis/type_casting.rb |