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: ActiveModel::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:



25
26
27
28
29
# File 'lib/kredis/type_casting.rb', line 25

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



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

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

#type_to_string(value, type) ⇒ Object

Raises:



19
20
21
22
23
# File 'lib/kredis/type_casting.rb', line 19

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



31
32
33
# File 'lib/kredis/type_casting.rb', line 31

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