Module: Redstruct::Utils::Coercion

Included in:
Lock, Struct
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



13
14
15
16
17
18
19
20
# File 'lib/redstruct/utils/coercion.rb', line 13

def coerce_array(value)
  case value
  when nil then []
  when Array then value
  else
    value.respond_to?(:to_a) ? value.to_a : [value]
  end
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



28
29
30
31
32
33
34
35
# File 'lib/redstruct/utils/coercion.rb', line 28

def coerce_bool(value)
  case value
  when nil, false then false
  when Numeric then !value.zero?
  else
    true
  end
end