Module: ActiveObject::Hash

Defined in:
lib/active_object/hash.rb

Instance Method Summary collapse

Instance Method Details

#assert_valid_keys(*valid_keys) ⇒ Object



3
4
5
6
7
8
9
10
11
12
# File 'lib/active_object/hash.rb', line 3

def assert_valid_keys(*valid_keys)
  valid_keys.flatten!

  each_key do |k|
    unless valid_keys.include?(k)
      raise ArgumentError,
        "Unknown key: #{k.inspect}. Valid keys are: #{valid_keys.map(&:inspect).join(', ')}"
    end
  end
end

#compactObject



14
15
16
# File 'lib/active_object/hash.rb', line 14

def compact
  select { |k, v| !v.nil? }
end

#compact!Object



18
19
20
# File 'lib/active_object/hash.rb', line 18

def compact!
  reject! { |k, v| v.nil? }
end

#deep_merge(other_hash, &block) ⇒ Object



22
23
24
# File 'lib/active_object/hash.rb', line 22

def deep_merge(other_hash, &block)
  dup.deep_merge!(other_hash, &block)
end

#deep_merge!(other_hash, &block) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/active_object/hash.rb', line 26

def deep_merge!(other_hash, &block)
  other_hash.each_pair do |current_key, other_value|
    this_value = self[current_key]

    self[current_key] = if this_value.is_a?(Hash) && other_value.is_a?(Hash)
      this_value.deep_merge(other_value, &block)
    else
      block_given? && key?(current_key) ? block.call(current_key, this_value, other_value) : other_value
    end
  end

  self
end

#dig(key, *rest) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/active_object/hash.rb', line 40

def dig(key, *rest)
  if value = (self[key] rescue nil)
    if rest.empty?
      value
    elsif value.respond_to?(:dig)
      value.dig(*rest)
    end
  end
end

#except(*keys) ⇒ Object



50
51
52
# File 'lib/active_object/hash.rb', line 50

def except(*keys)
  dup.except!(*keys)
end

#except!(*keys) ⇒ Object



54
55
56
57
# File 'lib/active_object/hash.rb', line 54

def except!(*keys)
  keys.flatten.each { |k| delete(k) }
  self
end

#hmap(&block) ⇒ Object



59
60
61
# File 'lib/active_object/hash.rb', line 59

def hmap(&block)
  dup.hmap!(&block)
end

#hmap!(&block) ⇒ Object



63
64
65
# File 'lib/active_object/hash.rb', line 63

def hmap!(&block)
  inject({}) { |hash, (k, v)| hash.merge(block.call(k, v)) }
end

#nillifyObject



67
68
69
# File 'lib/active_object/hash.rb', line 67

def nillify
  dup.nillify!
end

#nillify!Object



71
72
73
# File 'lib/active_object/hash.rb', line 71

def nillify!
  each { |k, v| self[k] = nil if !v.nil? && (v.try(:blank?) || v.try(:to_s).blank?) }
end

#only(*keys) ⇒ Object



75
76
77
# File 'lib/active_object/hash.rb', line 75

def only(*keys)
  dup.only!(*keys)
end

#only!(*keys) ⇒ Object



79
80
81
82
83
# File 'lib/active_object/hash.rb', line 79

def only!(*keys)
  hash = {}
  keys.flatten.each { |k| hash[k] = self[k] if self.has_key?(k) }
  replace(hash)
end

#rename_keys(*keys) ⇒ Object



85
86
87
# File 'lib/active_object/hash.rb', line 85

def rename_keys(*keys)
  dup.rename_keys!(*keys)
end

#rename_keys!(*keys) ⇒ Object



89
90
91
92
93
# File 'lib/active_object/hash.rb', line 89

def rename_keys!(*keys)
  keys = Hash[*keys.flatten]
  keys.each { |k, v| self[v] = delete(k) if self[k] }
  self
end

#reverse_merge(other_hash) ⇒ Object



95
96
97
# File 'lib/active_object/hash.rb', line 95

def reverse_merge(other_hash)
  other_hash.merge(self)
end

#reverse_merge!(other_hash) ⇒ Object



99
100
101
# File 'lib/active_object/hash.rb', line 99

def reverse_merge!(other_hash)
  replace(reverse_merge(other_hash))
end

#sampleObject



103
104
105
106
# File 'lib/active_object/hash.rb', line 103

def sample
  key = sample_key
  [key, fetch(key)]
end

#sample!Object



108
109
110
111
112
# File 'lib/active_object/hash.rb', line 108

def sample!
  key, value = sample
  delete(key)
  [key, value]
end

#sample_keyObject



114
115
116
117
# File 'lib/active_object/hash.rb', line 114

def sample_key
  hash_keys = keys
  hash_keys.at(Random.rand(hash_keys.length - 1))
end

#sample_key!Object



119
120
121
122
123
# File 'lib/active_object/hash.rb', line 119

def sample_key!
  key, value = sample
  delete(key)
  key
end

#sample_valueObject



125
126
127
# File 'lib/active_object/hash.rb', line 125

def sample_value
  fetch(sample_key)
end

#sample_value!Object



129
130
131
132
133
# File 'lib/active_object/hash.rb', line 129

def sample_value!
  key, value = sample
  delete(key)
  value
end

#shuffleObject



135
136
137
# File 'lib/active_object/hash.rb', line 135

def shuffle
  Hash[to_a.sample(length)]
end

#shuffle!Object



139
140
141
# File 'lib/active_object/hash.rb', line 139

def shuffle!
  replace(shuffle)
end

#slice(*keys) ⇒ Object



143
144
145
# File 'lib/active_object/hash.rb', line 143

def slice(*keys)
  keys.flatten.each_with_object(self.class.new) { |k, h| h[k] = self[k] if has_key?(k) }
end

#slice!(*keys) ⇒ Object



147
148
149
150
151
152
153
154
155
156
# File 'lib/active_object/hash.rb', line 147

def slice!(*keys)
  omit = slice(*self.keys - keys)
  hash = slice(*keys)

  hash.default = default
  hash.default_proc = default_proc if default_proc

  replace(hash)
  omit
end

#stringify_keysObject



158
159
160
# File 'lib/active_object/hash.rb', line 158

def stringify_keys
  dup.stringify_keys!
end

#stringify_keys!Object



162
163
164
165
166
167
# File 'lib/active_object/hash.rb', line 162

def stringify_keys!
  inject({}) do |options, (key, value)|
    options[key.to_s] = value
    options
  end
end

#stripObject



169
170
171
# File 'lib/active_object/hash.rb', line 169

def strip
  select { |k, v| !v.blank? }
end

#strip!Object



173
174
175
# File 'lib/active_object/hash.rb', line 173

def strip!
  reject! { |k, v| v.blank? }
end

#symbolize_and_underscore_keysObject



188
189
190
# File 'lib/active_object/hash.rb', line 188

def symbolize_and_underscore_keys
  dup.symbolize_and_underscore_keys!
end

#symbolize_and_underscore_keys!Object



192
193
194
195
196
197
# File 'lib/active_object/hash.rb', line 192

def symbolize_and_underscore_keys!
  inject({}) do |options, (key, value)|
    options[(key.to_s.gsub(" ", "_").underscore.to_sym rescue key) || key] = value
    options
  end
end

#symbolize_keysObject



177
178
179
# File 'lib/active_object/hash.rb', line 177

def symbolize_keys
  dup.symbolize_keys!
end

#symbolize_keys!Object



181
182
183
184
185
186
# File 'lib/active_object/hash.rb', line 181

def symbolize_keys!
  inject({}) do |options, (key, value)|
    options[(key.to_sym rescue key) || key] = value
    options
  end
end

#transform_keys(&block) ⇒ Object



199
200
201
# File 'lib/active_object/hash.rb', line 199

def transform_keys(&block)
  dup.transform_keys!(&block)
end

#transform_keys!(&block) ⇒ Object



203
204
205
206
207
208
# File 'lib/active_object/hash.rb', line 203

def transform_keys!(&block)
  return(enum_for(:transform_keys!)) unless block_given?

  keys.each { |k| self[yield(k)] = delete(k) }
  self
end

#transform_values(&block) ⇒ Object



210
211
212
# File 'lib/active_object/hash.rb', line 210

def transform_values(&block)
  dup.transform_values!(&block)
end

#transform_values!(&block) ⇒ Object



214
215
216
217
218
# File 'lib/active_object/hash.rb', line 214

def transform_values!(&block)
  return(enum_for(:transform_values!)) unless block_given?

  each { |k, v| self[k] = yield(v) }
end