Module: Kredis::TypeCasting

Included in:
Kredis
Defined in:
lib/kredis/type_casting.rb

Defined Under Namespace

Classes: InvalidType

Constant Summary collapse

TYPES =
{
  string: ActiveModel::Type::String.new,
  integer: ActiveModel::Type::Integer.new,
  decimal: ActiveModel::Type::Decimal.new,
  float: ActiveModel::Type::Float.new,
  boolean: Kredis::Type::Boolean.new,
  datetime: Kredis::Type::DateTime.new,
  json: Kredis::Type::Json.new
}

Instance Method Summary collapse

Instance Method Details

#string_to_type(value, type) ⇒ Object

Raises:



28
29
30
31
32
# File 'lib/kredis/type_casting.rb', line 28

def string_to_type(value, type)
  raise InvalidType if type && !TYPES.key?(type)

  TYPES[type || :string].cast(value)
end

#strings_to_types(values, type) ⇒ Object



38
39
40
# File 'lib/kredis/type_casting.rb', line 38

def strings_to_types(values, type)
  Array(values).flatten.map { |value| string_to_type(value, type) }
end

#type_to_string(value, type) ⇒ Object

Raises:



22
23
24
25
26
# File 'lib/kredis/type_casting.rb', line 22

def type_to_string(value, type)
  raise InvalidType if type && !TYPES.key?(type)

  TYPES[type || :string].serialize(value)
end

#types_to_strings(values, type) ⇒ Object



34
35
36
# File 'lib/kredis/type_casting.rb', line 34

def types_to_strings(values, type)
  Array(values).flatten.map { |value| type_to_string(value, type) }
end