Module: Redstruct::Utils::Coercion

Included in:
Hls::Lock, Types::Hash, Types::String
Defined in:
lib/redstruct/utils/coercion.rb

Overview

Coercion utilities to map Redis replies to Ruby types, or vice-versa

Class Method Summary collapse

Class Method Details

.coerce_array(value) ⇒ Array

Coerces the value into an array.

Returns the value if it is already an array (or subclass)
Returns value.to_a if it responds to to_a
Returns [value] otherwise

Parameters:

  • value (Object)

    The value to coerce

Returns:

  • (Array)

    The coerced value



11
12
13
14
15
16
# File 'lib/redstruct/utils/coercion.rb', line 11

def coerce_array(value)
  return [] if value.nil?
  return value if value.is_a?(Array)
  return value.to_a if value.respond_to?(:to_a)
  return [value]
end

.coerce_bool(value) ⇒ Boolean

Coerces an object into a boolean:

If nil or 0 (after .to_i) => false
True otherwise

Parameters:

  • value (Object)

    The object to coerce into a bool

Returns:

  • (Boolean)

    Coerced value



24
25
26
27
28
29
# File 'lib/redstruct/utils/coercion.rb', line 24

def coerce_bool(value)
  return false if value.nil?
  return false if value.to_i == 0

  return true
end