Class: Serega::SeregaUtils::ToHash

Inherits:
Object
  • Object
show all
Defined in:
lib/serega/utils/to_hash.rb

Overview

Utility to transform almost anything to Hash

Class Method Summary collapse

Class Method Details

.call(value) ⇒ Hash

Constructs deep hashes from provided data

Examples:

Serega::SeregaUtils::ToHash.(nil) # => {}
Serega::SeregaUtils::ToHash.(false) # => {}
Serega::SeregaUtils::ToHash.(:foo) # => {:foo=>{}}
Serega::SeregaUtils::ToHash.("foo") # => {:foo=>{}}
Serega::SeregaUtils::ToHash.(%w[foo bar]) # => {:foo=>{}, :bar=>{}}
Serega::SeregaUtils::ToHash.({ foo: nil, bar: false }) # => {:foo=>{}, :bar=>{}}
Serega::SeregaUtils::ToHash.({ foo: :bar }) # => {:foo=>{:bar=>{}}}
Serega::SeregaUtils::ToHash.({ foo: [:bar] }) # => {:foo=>{:bar=>{}}}

Parameters:

  • value (Array, Hash, String, Symbol, NilClass, FalseClass)

    Value to transform

Returns:

  • (Hash)

    Transformed data



27
28
29
30
31
32
33
34
35
36
# File 'lib/serega/utils/to_hash.rb', line 27

def call(value)
  case value
  when Array then array_to_hash(value)
  when Hash then hash_to_hash(value)
  when NilClass, FalseClass then nil_to_hash(value)
  when String then string_to_hash(value)
  when Symbol then symbol_to_hash(value)
  else raise SeregaError, "Cant convert #{value.class} class object to hash"
  end
end