Class: IHash

Inherits:
Hash show all
Defined in:
lib/i_hash.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.stringify(obj) ⇒ Object



38
39
40
41
42
43
44
45
46
47
# File 'lib/i_hash.rb', line 38

def stringify(obj)
  if obj.is_a? Hash
    return obj.inject({}) do |memo, (k, v)|
      memo.tap { |m| m[k.to_s] = stringify(v) }
    end
  elsif obj.is_a? Array
    return obj.map { |memo| stringify(memo) }
  end
  obj
end

.symbolize(obj) ⇒ Object



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

def symbolize(obj)
  if obj.is_a? Hash
    return obj.inject({}) do |memo, (k, v)|
      memo.tap { |m| m[k.to_sym] = symbolize(v) }
    end
  elsif obj.is_a? Array
    return obj.map { |memo| symbolize(memo) }
  end
  obj
end

Instance Method Details

#compare(other_hash) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/i_hash.rb', line 10

def compare(other_hash)
  current_hash = IHash.symbolize(self)
  other_hash = IHash.symbolize(other_hash)
  (current_hash.keys | other_hash.keys).each_with_object({}) do |k, diff|
    current_hash_key = current_hash[k]
    other_hash_key = other_hash[k]
    if current_hash_key != other_hash_key
      diff[k] = [current_hash_key, other_hash_key]
      next unless current_hash_key.is_a?(Hash) && other_hash_key.is_a?(Hash)

      diff[k] = deep_diff(current_hash_key, other_hash_key)
    end
    diff
  end
end

#stringify_keysObject



6
7
8
# File 'lib/i_hash.rb', line 6

def stringify_keys
  IHash.stringify(self)
end

#symbolize_keysObject



2
3
4
# File 'lib/i_hash.rb', line 2

def symbolize_keys
  IHash.symbolize(self)
end