Module: Redstruct::Utils::Coercion
Overview
Coercion utilities to map Redis replies to Ruby types, or vice-versa
Class Method Summary collapse
-
.coerce_array(value) ⇒ Array
Coerces the value into an array.
-
.coerce_bool(value) ⇒ Boolean
Coerces an object into a boolean: If nil or 0 (after .to_i) => false True otherwise.
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
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
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 |