Class: Hash

Inherits:
Object show all
Defined in:
lib/ab_admin/core_ext/hash.rb

Instance Method Summary collapse

Instance Method Details

#add(other_hash) ⇒ Object


65
66
67
68
69
# File 'lib/ab_admin/core_ext/hash.rb', line 65

def add(other_hash)
  other_hash.each_pair do |k, v|
    self[k] = v if self.keys.include?(k)
  end
end

#clear_valuesObject


33
34
35
36
37
38
39
# File 'lib/ab_admin/core_ext/hash.rb', line 33

def clear_values
  result = {}
  self.each_key do |key|
    result[key] = ''
  end
  result
end

#deep_add(other_hash) ⇒ Object


55
56
57
58
59
60
61
62
63
# File 'lib/ab_admin/core_ext/hash.rb', line 55

def deep_add(other_hash)
  other_hash.each_pair do |k, v|
    tv = self[k]
    if self[k]
      self[k] = tv.is_a?(Hash) && v.is_a?(Hash) ? tv.deep_add(v) : v
    end
  end
  self
end

#deep_clear_valuesObject


11
12
13
14
15
16
17
18
19
20
# File 'lib/ab_admin/core_ext/hash.rb', line 11

def deep_clear_values
  self.each_key do |key|
    if self[key].is_a?(Hash)
      self[key] = self[key].deep_clear_values
    else
      self[key] = ''
    end
  end
  self
end

#deep_diff(b) ⇒ Object


41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/ab_admin/core_ext/hash.rb', line 41

def deep_diff(b)
  a = self
  (a.keys | b.keys).inject({}) do |diff, k|
    if a[k] != b[k]
      if a[k].respond_to?(:deep_diff) && b[k].respond_to?(:deep_diff)
        diff[k] = a[k].deep_diff(b[k])
      else
        diff[k] = [a[k], b[k]]
      end
    end
    diff
  end
end

#deep_stringify_keysObject


117
118
119
120
121
122
123
# File 'lib/ab_admin/core_ext/hash.rb', line 117

def deep_stringify_keys
  inject({}) { |result, (key, value)|
    value = value.deep_stringify_keys if value.is_a?(Hash)
    result[(key.to_s rescue key) || key] = value
    result
  }
end

#deep_transform_values!(&block) ⇒ Object


22
23
24
25
26
27
28
29
30
31
# File 'lib/ab_admin/core_ext/hash.rb', line 22

def deep_transform_values!(&block)
  self.each_key do |key|
    if self[key].is_a?(Hash)
      self[key] = self[key].deep_transform_values!(&block)
    else
      self[key] = block.call(self[key])
    end
  end
  self
end

#downcase_keysObject


98
99
100
101
102
103
104
# File 'lib/ab_admin/core_ext/hash.rb', line 98

def downcase_keys
  new_hash = {}
  self.each do |k, v|
    new_hash[k.to_s.downcase] = v
  end
  new_hash
end

#no_blankObject


106
107
108
# File 'lib/ab_admin/core_ext/hash.rb', line 106

def no_blank
  reject { |_, v| v.blank? }
end

#reverse_deep_merge!(other_hash) ⇒ Object


3
4
5
6
7
8
9
# File 'lib/ab_admin/core_ext/hash.rb', line 3

def reverse_deep_merge!(other_hash)
  other_hash.each_pair do |k, v|
    tv = self[k]
    self[k] = tv.is_a?(Hash) && v.is_a?(Hash) ? v.deep_merge!(tv) : (self[k] || v)
  end
  self
end

#store_multi(value, *keys) ⇒ Object


87
88
89
90
91
92
93
94
95
96
# File 'lib/ab_admin/core_ext/hash.rb', line 87

def store_multi(value, *keys)
  key = keys.shift
  self[key] ||= {}
  if keys.empty?
    self[key] = value
  else
    self[key] = self[key].store_multi(value, *keys)
  end
  self
end

#try_keys(*try_keys) ⇒ Object


110
111
112
113
114
115
# File 'lib/ab_admin/core_ext/hash.rb', line 110

def try_keys(*try_keys)
  try_keys.each do |k|
    return self[k] if self.has_key?(k)
  end
  default
end

#val(*array) ⇒ Object


71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/ab_admin/core_ext/hash.rb', line 71

def val(*array)
  if array.empty?
    self
  else
    key = array.shift
    value = self[key]
    if array.empty?
      value
    elsif value.is_a? Hash
      value.val(*array)
    else
      nil
    end
  end
end