module Redstruct::Utils::Coercion

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

Public Class Methods

coerce_array(value) click to toggle source

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

@param [Object] value The value to coerce @return [Array] The coerced value

# 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) click to toggle source

Coerces an object into a boolean:

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

@param [Object] value The object to coerce into a bool @return [Boolean] Coerced value

# 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

Private Instance Methods

coerce_array(value) click to toggle source

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

@param [Object] value The value to coerce @return [Array] The coerced value

# 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) click to toggle source

Coerces an object into a boolean:

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

@param [Object] value The object to coerce into a bool @return [Boolean] Coerced value

# 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