Class: HashKit::Helper

Inherits:
Object
  • Object
show all
Defined in:
lib/hash_kit/helper.rb

Overview

Hash kit Helper class

Constant Summary collapse

INDIFFERENT_PROC =
proc do |h, k|
  case k
  when Symbol
    # Symbol#name returns a frozen string without allocating (Ruby 3.0+)
    str = k.name
    h[str] if h.key?(str)
  when String
    sym = k.to_sym
    h[sym] if h.key?(sym)
  else
    h[k.to_s]
  end
end

Instance Method Summary collapse

Instance Method Details

#from_hash(hash, klass, transforms = []) ⇒ Object

Return an object of type klass from the values in the given hash

Parameters:

  • hash (Hash)
  • klass (Class)
  • transforms (Array) (defaults to: [])

Returns:

  • (Object)

Raises:

  • (ArgumentError)


90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/hash_kit/helper.rb', line 90

def from_hash(hash, klass, transforms = [])
  obj = klass.new
  return obj if hash.nil? || hash.empty?
  raise ArgumentError, "#{hash.inspect} is not a hash" unless hash.is_a?(Hash)

  hash.each do |k, v|
    next unless obj.respond_to?(k)

    transform = transforms.detect { |t| t.key.to_sym == k.to_sym }
    if !transform.nil?
      if v.is_a?(Hash)
        child = from_hash(v, transform.klass, transforms)
        obj.instance_variable_set("@#{k}", child)
      elsif v.is_a?(Array)
        items = v.map do |i|
          from_hash(i, transform.klass, transforms)
        end
        obj.instance_variable_set("@#{k}", items)
      end
    else
      obj.instance_variable_set("@#{k}", v)
    end
  end

  obj
end

#indifferent!(hash) ⇒ Object

This method is called to make a hash allow indifferent access (it will accept both strings & symbols for a valid key).



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/hash_kit/helper.rb', line 22

def indifferent!(hash)
  return unless hash.is_a?(Hash)

  # Set the default proc to allow the key to be either string or symbol if
  # a matching key is found.
  hash.default_proc = INDIFFERENT_PROC

  # Recursively process any child hashes
  hash.each_value do |value|
    case value
    when Hash
      indifferent!(value)
    when Array
      indifferent_array!(value)
    end
  end

  hash
end

#indifferent_array!(array) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/hash_kit/helper.rb', line 42

def indifferent_array!(array)
  return unless array.is_a?(Array)

  array.each do |i|
    case i
    when Hash
      indifferent!(i)
    when Array
      indifferent_array!(i)
    end
  end
end

#stringify(hash) ⇒ Object

This method is called to convert all the keys of a hash into strings to allow consistent usage of hashes within your Ruby application.



65
66
67
68
69
# File 'lib/hash_kit/helper.rb', line 65

def stringify(hash)
  {}.tap do |h|
    hash.each { |key, value| h[key.to_s] = map_value_string(value) }
  end
end

#symbolize(hash) ⇒ Object

This method is called to convert all the keys of a hash into symbols to allow consistent usage of hashes within your Ruby application.



57
58
59
60
61
# File 'lib/hash_kit/helper.rb', line 57

def symbolize(hash)
  {}.tap do |h|
    hash.each { |key, value| h[key.to_sym] = map_value_symbol(value) }
  end
end

#to_hash(obj) ⇒ Hash

Convert an object to a hash representation of its instance variables.

Returns:

  • (Hash)

    if the object is not nil, otherwise nil is returned.



73
74
75
76
77
78
79
80
81
82
# File 'lib/hash_kit/helper.rb', line 73

def to_hash(obj)
  return nil unless obj
  return obj if obj.is_a?(Hash)

  hash = {}
  obj.instance_variables.each do |key|
    hash[key[1..-1].to_sym] = deeply_to_hash(obj.instance_variable_get(key))
  end
  hash
end