Module: ActiveObject::Hash

Defined in:
lib/active_object/hash.rb

Instance Method Summary collapse

Instance Method Details

#assert_valid_keys(*valid_keys) ⇒ Object


8
9
10
11
12
13
14
15
16
17
# File 'lib/active_object/hash.rb', line 8

def assert_valid_keys(*valid_keys)
  valid_keys.flatten!

  each_key do |key|
    next if valid_keys.include?(key)

    raise ArgumentError,
          "Unknown key: #{key.inspect}. Valid keys are: #{valid_keys.map(&:inspect).join(', ')}"
  end
end

#assert_valid_keys!(*valid_keys) ⇒ Object


19
20
21
22
23
24
25
26
# File 'lib/active_object/hash.rb', line 19

def assert_valid_keys!(*valid_keys)
  if empty?
    raise ArgumentError,
          "Empty hash. Valid keys are: #{valid_keys.map(&:inspect).join(', ')}"
  else
    assert_valid_keys(*valid_keys)
  end
end

#bury(*args) ⇒ Object

rubocop:disable Style/GuardClause


29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/active_object/hash.rb', line 29

def bury(*args)
  if args.count < 2
    raise ArgumentError, '2 or more arguments required'
  elsif args.count == 2
    self[args[0]] = args[1]
  else
    arg = args.shift

    self[arg] = {} unless self[arg]
    self[arg].bury(*args) unless args.empty?
  end

  self
end

#collect_keys(&block) ⇒ Object

rubocop:disable Lint/UnusedMethodArgument


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

def collect_keys(&block)
  return enum_for(:collect_keys) unless block_given?

  collect { |key, _| yield(key) }
end

#collect_values(&block) ⇒ Object


60
61
62
63
64
# File 'lib/active_object/hash.rb', line 60

def collect_values(&block)
  return enum_for(:collect_values) unless block_given?

  collect { |_, val| yield(val) }
end

#compactObject

rubocop:enable Style/GuardClause


45
46
47
# File 'lib/active_object/hash.rb', line 45

def compact
  select { |_, val| !val.nil? }
end

#compact!Object


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

def compact!
  reject! { |_, val| val.nil? }
end

#deep_merge(other_hash, &block) ⇒ Object

rubocop:enable Lint/UnusedMethodArgument


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

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

#deep_merge!(other_hash, &block) ⇒ Object

rubocop:disable Metrics/MethodLength


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

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, yield(block))
                        elsif block_given? && key?(current_key)
                          yield(current_key, this_value, other_value)
                        else
                          other_value
                        end
  end

  self
end

#demote(key) ⇒ Object

rubocop:enable Metrics/MethodLength


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

def demote(key)
  return self unless key?(key)

  merge(key => delete(key))
end

#demote!(key) ⇒ Object


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

def demote!(key)
  replace(demote(key))
end

#denillify(value = 0) ⇒ Object


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

def denillify(value = 0)
  each { |key, val| self[key] = val.nil? ? value : val }
end

#denillify!(value = 0) ⇒ Object


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

def denillify!(value = 0)
  replace(denillify(value))
end

#dig(key, *rest) ⇒ Object


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

def dig(key, *rest)
  value = (self[key] rescue nil)

  return if value.nil?
  return value if rest.empty?
  return value.dig(*rest) if value.respond_to?(:dig)
end

#except(*keys) ⇒ Object


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

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

#except!(*keys) ⇒ Object


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

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

#extract!(*keys) ⇒ Object


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

def extract!(*keys)
  keys.each_with_object({}) { |key, hash| hash[key] = delete(key) if key?(key) }
end

#hmap(&block) ⇒ Object


128
129
130
# File 'lib/active_object/hash.rb', line 128

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

#hmap!(&block) ⇒ Object

rubocop:disable Lint/UnusedMethodArgument


133
134
135
# File 'lib/active_object/hash.rb', line 133

def hmap!(&block)
  inject({}) { |hash, (key, val)| hash.merge(yield(key, val)) }
end

#nillifyObject

rubocop:enable Lint/UnusedMethodArgument


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

def nillify
  dup.nillify!
end

#nillify!Object


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

def nillify!
  each do |key, val|
    self[key] = nil if !val.nil? && (val.try(:blank?) || val.try(:to_s).blank?)
  end
end

#only(*keys) ⇒ Object


148
149
150
# File 'lib/active_object/hash.rb', line 148

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

#only!(*keys) ⇒ Object


152
153
154
155
156
# File 'lib/active_object/hash.rb', line 152

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

#only_fill(*keys, placeholder: nil) ⇒ Object


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

def only_fill(*keys, placeholder: nil)
  dup.only_fill!(*keys, placeholder: placeholder)
end

#only_fill!(*keys, placeholder: nil) ⇒ Object


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

def only_fill!(*keys, placeholder: nil)
  hash = {}
  keys.flatten.each { |key| hash[key] = key?(key) ? self[key] : placeholder }
  replace(hash)
end

#pair?(key, value) ⇒ Boolean

Returns:

  • (Boolean)

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

def pair?(key, value)
  self[key] == value
end

#promote(key) ⇒ Object


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

def promote(key)
  return self unless key?(key)

  { key => delete(key) }.merge(self)
end

#promote!(key) ⇒ Object


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

def promote!(key)
  replace(promote(key))
end

#rename_keys(*keys) ⇒ Object


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

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

#rename_keys!(*keys) ⇒ Object


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

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

#reverse_merge(other_hash) ⇒ Object


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

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

#reverse_merge!(other_hash) ⇒ Object


196
197
198
# File 'lib/active_object/hash.rb', line 196

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

#sampleObject


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

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

#sample!Object


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

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

#sample_keyObject


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

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

#sample_key!Object


216
217
218
219
220
# File 'lib/active_object/hash.rb', line 216

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

#sample_valueObject


222
223
224
# File 'lib/active_object/hash.rb', line 222

def sample_value
  fetch(sample_key)
end

#sample_value!Object


226
227
228
229
230
# File 'lib/active_object/hash.rb', line 226

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

#shuffleObject


232
233
234
# File 'lib/active_object/hash.rb', line 232

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

#shuffle!Object


236
237
238
# File 'lib/active_object/hash.rb', line 236

def shuffle!
  replace(shuffle)
end

#slice(*keys) ⇒ Object


240
241
242
# File 'lib/active_object/hash.rb', line 240

def slice(*keys)
  keys.flatten.each_with_object({}) { |key, hsh| hsh[key] = self[key] if key?(key) }
end

#slice!(*keys) ⇒ Object


244
245
246
247
248
249
250
251
252
253
# File 'lib/active_object/hash.rb', line 244

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


255
256
257
# File 'lib/active_object/hash.rb', line 255

def stringify_keys
  dup.stringify_keys!
end

#stringify_keys!Object


259
260
261
# File 'lib/active_object/hash.rb', line 259

def stringify_keys!
  each_with_object({}) { |(key, val), options| options[key.to_s] = val }
end

#stripObject


263
264
265
# File 'lib/active_object/hash.rb', line 263

def strip
  select { |_, val| !val.blank? }
end

#strip!Object


267
268
269
# File 'lib/active_object/hash.rb', line 267

def strip!
  reject! { |_, val| val.blank? }
end

#symbolize_and_underscore_keysObject


279
280
281
# File 'lib/active_object/hash.rb', line 279

def symbolize_and_underscore_keys
  dup.symbolize_and_underscore_keys!
end

#symbolize_and_underscore_keys!Object


283
284
285
286
287
# File 'lib/active_object/hash.rb', line 283

def symbolize_and_underscore_keys!
  each_with_object({}) do |(key, val), options|
    options[(key.to_s.tr(' ', '_').underscore.to_sym rescue key) || key] = val
  end
end

#symbolize_keysObject


271
272
273
# File 'lib/active_object/hash.rb', line 271

def symbolize_keys
  dup.symbolize_keys!
end

#symbolize_keys!Object


275
276
277
# File 'lib/active_object/hash.rb', line 275

def symbolize_keys!
  each_with_object({}) { |(key, val), options| options[(key.to_sym rescue key) || key] = val }
end

#to_oObject


289
290
291
# File 'lib/active_object/hash.rb', line 289

def to_o
  JSON.parse(to_json, object_class: OpenStruct)
end

#transform_keys(&block) ⇒ Object


293
294
295
# File 'lib/active_object/hash.rb', line 293

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

#transform_keys!(&block) ⇒ Object

rubocop:disable Lint/UnusedMethodArgument


298
299
300
301
302
303
# File 'lib/active_object/hash.rb', line 298

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

  each_key { |key| self[yield(key)] = delete(key) }
  self
end

#transform_values(&block) ⇒ Object

rubocop:enable Lint/UnusedMethodArgument


306
307
308
# File 'lib/active_object/hash.rb', line 306

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

#transform_values!(&block) ⇒ Object

rubocop:disable Lint/UnusedMethodArgument


311
312
313
314
315
# File 'lib/active_object/hash.rb', line 311

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

  each { |key, val| self[key] = yield(val) }
end

#vacant?(key) ⇒ Boolean

rubocop:enable Lint/UnusedMethodArgument

Returns:

  • (Boolean)

318
319
320
# File 'lib/active_object/hash.rb', line 318

def vacant?(key)
  self[key].blank?
end