Class: HashKit::Helper

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

Instance Method Summary collapse

Instance Method Details

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



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/hash_kit/helper.rb', line 75

def from_hash(hash, klass, transforms =  [])
  obj = klass.new
  if hash ==nil || hash == {}
    return obj
  end

  hash.each do |k,v|
    if !obj.respond_to?(k)
      next
    end
    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
  return 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).



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/hash_kit/helper.rb', line 5

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

  #set the default proc to allow the key to be either string or symbol if a matching key is found.
  hash.default_proc = proc do |h, k|
    if h.key?(k.to_s)
      h[k.to_s]
    elsif h.key?(k.to_sym)
      h[k.to_sym]
    else
      nil
    end
  end

  #recursively process any child hashes
  hash.each do |key,value|
    if hash[key] != nil
      if hash[key].is_a?(Hash)
        indifferent!(hash[key])
      elsif hash[key].is_a?(Array)
        indifferent_array!(hash[key])
      end
    end
  end

  hash
end

#indifferent_array!(array) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/hash_kit/helper.rb', line 35

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

  array.each do |i|
    if i.is_a?(Hash)
      indifferent!(i)
    elsif i.is_a?(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.



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

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.



50
51
52
53
54
# File 'lib/hash_kit/helper.rb', line 50

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.



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

def to_hash(obj)
  return nil unless obj

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