Class: Hash

Inherits:
Object show all
Defined in:
lib/lite/ruby/hash.rb,
lib/lite/ruby/safe/hash.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.zip(keys, values) ⇒ Object



7
8
9
# File 'lib/lite/ruby/hash.rb', line 7

def zip(keys, values)
  keys.size.times.with_object({}) { |i, hash| hash[keys[i]] = values[i] }
end

Instance Method Details

#aka(new_key, old_key) ⇒ Object



13
14
15
16
# File 'lib/lite/ruby/hash.rb', line 13

def aka(new_key, old_key)
  self[new_key] = self[old_key] if key?(old_key)
  self
end

#assert_all_min_keys!(*valid_keys) ⇒ Object

Raises:

  • (ArgumentError)


32
33
34
35
36
# File 'lib/lite/ruby/hash.rb', line 32

def assert_all_min_keys!(*valid_keys)
  return assert_min_keys!(*valid_keys) unless empty?

  raise ArgumentError, 'An empty hash is not allowed'
end

#assert_all_pair_presence!(*valid_keys) ⇒ Object

Raises:

  • (ArgumentError)


50
51
52
53
54
# File 'lib/lite/ruby/hash.rb', line 50

def assert_all_pair_presence!(*valid_keys)
  return assert_pair_presence!(*valid_keys) unless empty?

  raise ArgumentError, 'An empty hash is not allowed'
end

#assert_all_valid_keys!(*valid_keys) ⇒ Object

Raises:

  • (ArgumentError)


66
67
68
69
70
# File 'lib/lite/ruby/hash.rb', line 66

def assert_all_valid_keys!(*valid_keys)
  return assert_valid_keys!(*valid_keys) unless empty?

  raise ArgumentError, 'An empty hash is not allowed'
end

#assert_all_valid_values!(*valid_values) ⇒ Object

Raises:

  • (ArgumentError)


82
83
84
85
86
# File 'lib/lite/ruby/hash.rb', line 82

def assert_all_valid_values!(*valid_values)
  return assert_valid_values!(*valid_values) unless empty?

  raise ArgumentError, 'An empty hash is not allowed'
end

#assert_min_keys!(*valid_keys) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/lite/ruby/hash.rb', line 18

def assert_min_keys!(*valid_keys)
  return self if empty?

  valid_keys.each do |key|
    next if key?(key)

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

  self
end

#assert_pair_presence!(*valid_keys) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/lite/ruby/hash.rb', line 38

def assert_pair_presence!(*valid_keys)
  each do |key, value|
    if !valid_keys.include?(key)
      raise ArgumentError,
            "Invalid key: #{key.inspect}. " \
            "Allowed keys are: #{valid_keys.map(&:inspect).join(', ')}"
    elsif value.respond_to?(:blank?) ? value.blank? : !value
      raise ArgumentError, "A #{value.inspect} value for #{key.inspect} is not allowed"
    end
  end
end

#assert_valid_keys!(*valid_keys) ⇒ Object



56
57
58
59
60
61
62
63
64
# File 'lib/lite/ruby/hash.rb', line 56

def assert_valid_keys!(*valid_keys)
  each_key do |key|
    next if valid_keys.include?(key)

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

#assert_valid_values!(*valid_values) ⇒ Object



72
73
74
75
76
77
78
79
80
# File 'lib/lite/ruby/hash.rb', line 72

def assert_valid_values!(*valid_values)
  each_value do |value|
    next if valid_values.include?(value)

    raise ArgumentError,
          "Invalid value: #{value.inspect}. " \
          "Allowed values are: #{valid_values.map(&:inspect).join(', ')}"
  end
end

#bury(*args) ⇒ Object

rubocop:disable Style/GuardClause



89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/lite/ruby/hash.rb', line 89

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

#collate(*others) ⇒ Object

rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/lite/ruby/hash.rb', line 105

def collate(*others)
  hash = {}

  each_key { |key| hash[key] = [] }

  others.each do |other|
    other.each_key { |key| hash[key] = [] }
  end

  each { |key, val| hash[key] << val }

  others.each do |other|
    other.each { |key, val| hash[key] << val }
  end

  hash.each_value(&:flatten!)
  hash
end

#collate!(other_hash) ⇒ Object

rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength



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

def collate!(other_hash)
  replace(collate(other_hash))
end

#collect_keys(&block) ⇒ Object Also known as: map_keys



129
130
131
# File 'lib/lite/ruby/hash.rb', line 129

def collect_keys(&block)
  keys.map(&block)
end

#collect_values(&block) ⇒ Object Also known as: map_values



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

def collect_values(&block)
  values.map(&block)
end

#dearray_singular_valuesObject



150
151
152
153
154
155
156
157
# File 'lib/lite/ruby/hash.rb', line 150

def dearray_singular_values
  each_with_object({}) do |(key, val), hash|
    hash[key] = case val
                when Array then val.size < 2 ? val[0] : val
                else val
                end
  end
end

#dearray_singular_values!Object



159
160
161
# File 'lib/lite/ruby/hash.rb', line 159

def dearray_singular_values!
  replace(dearray_singular_values)
end

#dearray_values(idx = 0) ⇒ Object



137
138
139
140
141
142
143
144
# File 'lib/lite/ruby/hash.rb', line 137

def dearray_values(idx = 0)
  each_with_object({}) do |(key, val), hash|
    hash[key] = case val
                when Array then val[idx] || val[-1]
                else val
                end
  end
end

#dearray_values!(idx = 0) ⇒ Object



146
147
148
# File 'lib/lite/ruby/hash.rb', line 146

def dearray_values!(idx = 0)
  replace(dearray_values(idx))
end

#deep_dupObject

rubocop:disable Style/CaseEquality



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/lite/ruby/safe/hash.rb', line 6

def deep_dup
  hash = dup

  each_pair do |key, value|
    if key.frozen? && ::String === key
      hash[key] = value.deep_dup
    else
      hash.delete(key)
      hash[key.deep_dup] = value.deep_dup
    end
  end

  hash
end

#deep_key?(*keys) ⇒ Boolean

Returns:

  • (Boolean)


163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/lite/ruby/hash.rb', line 163

def deep_key?(*keys)
  last_hash = self
  found_key = false

  keys.each do |key|
    break found_key = false unless last_hash.key?(key)

    last_hash = last_hash[key]
    found_key = true
  end

  found_key
end

#deep_merge(other_hash, &block) ⇒ Object

rubocop:enable Style/CaseEquality



22
23
24
# File 'lib/lite/ruby/safe/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
# File 'lib/lite/ruby/safe/hash.rb', line 26

def deep_merge!(other_hash, &block)
  merge!(other_hash) do |key, this_val, other_val|
    if this_val.is_a?(Hash) && other_val.is_a?(Hash)
      this_val.deep_merge(other_val, &block)
    elsif defined?(yield)
      yield(key, this_val, other_val)
    else
      other_val
    end
  end
end

#delete_unlessObject



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

def delete_unless
  delete_if { |key, val| !yield(key, val) }
end

#delete_values(*values) ⇒ Object



181
182
183
184
185
186
187
188
# File 'lib/lite/ruby/hash.rb', line 181

def delete_values(*values)
  each_key.with_object([]) do |key, array|
    next unless values.include?(self[key])

    array << key
    delete(key)
  end
end

#demote(key) ⇒ Object



190
191
192
# File 'lib/lite/ruby/hash.rb', line 190

def demote(key)
  dup.demote!(key)
end

#demote!(key) ⇒ Object



194
195
196
197
198
199
# File 'lib/lite/ruby/hash.rb', line 194

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

  self[key] = delete(key)
  self
end

#denillify(identity = 0) ⇒ Object



201
202
203
# File 'lib/lite/ruby/hash.rb', line 201

def denillify(identity = 0)
  dup.denillify!(identity)
end

#denillify!(identity = 0) ⇒ Object



205
206
207
# File 'lib/lite/ruby/hash.rb', line 205

def denillify!(identity = 0)
  each { |key, val| self[key] = val || identity }
end

#diff(hash) ⇒ Object



209
210
211
212
213
# File 'lib/lite/ruby/hash.rb', line 209

def diff(hash)
  h1 = dup.delete_if { |k, v| hash[k] == v }
  h2 = hash.dup.delete_if { |k, _| key?(k) }
  h1.merge(h2)
end

#except(*keys) ⇒ Object



38
39
40
# File 'lib/lite/ruby/safe/hash.rb', line 38

def except(*keys)
  slice(*self.keys - keys)
end

#except!(*keys) ⇒ Object



42
43
44
45
# File 'lib/lite/ruby/safe/hash.rb', line 42

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

#extract!(*keys) ⇒ Object



47
48
49
# File 'lib/lite/ruby/safe/hash.rb', line 47

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

#hmap(&block) ⇒ Object



215
216
217
# File 'lib/lite/ruby/hash.rb', line 215

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

#hmap!Object



219
220
221
# File 'lib/lite/ruby/hash.rb', line 219

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

#insert(name, value) ⇒ Object



223
224
225
226
227
228
# File 'lib/lite/ruby/hash.rb', line 223

def insert(name, value)
  return false if key?(name)

  store(name, value)
  true
end

#invertObject



230
231
232
233
234
235
236
237
238
# File 'lib/lite/ruby/hash.rb', line 230

def invert
  each_pair.with_object({}) do |(key, val), hash|
    if val.is_a?(Array)
      val.each { |x| hash[x] = (hash.key?(x) ? [key, hash[x]].flatten : key) }
    else
      hash[val] = (hash.key?(val) ? [key, hash[val]].flatten : key)
    end
  end
end

#keys?(*check_keys) ⇒ Boolean Also known as: has_keys?

Returns:

  • (Boolean)


240
241
242
243
# File 'lib/lite/ruby/hash.rb', line 240

def keys?(*check_keys)
  unknown_keys = check_keys - keys
  unknown_keys.empty?
end

#nillifyObject



245
246
247
# File 'lib/lite/ruby/hash.rb', line 245

def nillify
  dup.nillify!
end

#nillify!Object



249
250
251
252
253
254
255
# File 'lib/lite/ruby/hash.rb', line 249

def nillify!
  each do |key, val|
    next if val.nil?

    self[key] = nil if respond_to?(:blank?) ? val.blank? : !val
  end
end

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



257
258
259
# File 'lib/lite/ruby/hash.rb', line 257

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

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



261
262
263
# File 'lib/lite/ruby/hash.rb', line 261

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

#only_keys?(*check_keys) ⇒ Boolean Also known as: has_only_keys?

Returns:

  • (Boolean)


265
266
267
268
# File 'lib/lite/ruby/hash.rb', line 265

def only_keys?(*check_keys)
  unknown_keys = keys - check_keys
  unknown_keys.empty?
end

#pair?(key, value) ⇒ Boolean

Returns:

  • (Boolean)


270
271
272
# File 'lib/lite/ruby/hash.rb', line 270

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

#promote(key) ⇒ Object



274
275
276
# File 'lib/lite/ruby/hash.rb', line 274

def promote(key)
  dup.promote!(key)
end

#promote!(key) ⇒ Object



278
279
280
281
282
# File 'lib/lite/ruby/hash.rb', line 278

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

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

#rename_keys(*keys) ⇒ Object



284
285
286
# File 'lib/lite/ruby/hash.rb', line 284

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

#rename_keys!(*keys) ⇒ Object



288
289
290
291
# File 'lib/lite/ruby/hash.rb', line 288

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

#reverse_merge(other_hash) ⇒ Object



51
52
53
# File 'lib/lite/ruby/safe/hash.rb', line 51

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

#reverse_merge!(other_hash) ⇒ Object



55
56
57
# File 'lib/lite/ruby/safe/hash.rb', line 55

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

#sampleObject



293
294
295
296
# File 'lib/lite/ruby/hash.rb', line 293

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

#sample!Object



298
299
300
301
302
# File 'lib/lite/ruby/hash.rb', line 298

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

#sample_keyObject



304
305
306
307
# File 'lib/lite/ruby/hash.rb', line 304

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

#sample_key!Object



309
310
311
312
313
# File 'lib/lite/ruby/hash.rb', line 309

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

#sample_valueObject



315
316
317
# File 'lib/lite/ruby/hash.rb', line 315

def sample_value
  fetch(sample_key)
end

#sample_value!Object



319
320
321
322
323
# File 'lib/lite/ruby/hash.rb', line 319

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

#shuffleObject



325
326
327
# File 'lib/lite/ruby/hash.rb', line 325

def shuffle
  to_a.sample(size).to_h
end

#shuffle!Object



329
330
331
# File 'lib/lite/ruby/hash.rb', line 329

def shuffle!
  replace(shuffle)
end

#slice!(*keys) ⇒ Object Also known as: only!



333
334
335
336
337
338
339
340
# File 'lib/lite/ruby/hash.rb', line 333

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



59
60
61
# File 'lib/lite/ruby/safe/hash.rb', line 59

def stringify_keys
  transform_keys(&:to_s)
end

#stringify_keys!Object



63
64
65
# File 'lib/lite/ruby/safe/hash.rb', line 63

def stringify_keys!
  transform_keys!(&:to_s)
end

#stripObject



342
343
344
# File 'lib/lite/ruby/hash.rb', line 342

def strip
  reject { |_, val| respond_to?(:blank?) ? val.blank? : !val }
end

#strip!Object



346
347
348
# File 'lib/lite/ruby/hash.rb', line 346

def strip!
  reject! { |_, val| respond_to?(:blank?) ? val.blank? : !val }
end

#symbolize_and_underscore_keysObject

rubocop:disable Metrics/MethodLength



351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
# File 'lib/lite/ruby/hash.rb', line 351

def symbolize_and_underscore_keys
  each_with_object({}) do |(key, val), hash|
    new_key = begin
      str = key.dup.to_s
      str.gsub!(/::/, '/') || str
      str.gsub!(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2') || str
      str.gsub!(/([a-z\d])([A-Z])/, '\1_\2') || str
      str.tr!(' -', '_') || str
      str.downcase!
      str.to_sym
    rescue StandardError
      key
    end

    hash[new_key] = val
  end
end

#symbolize_and_underscore_keys!Object

rubocop:enable Metrics/MethodLength



370
371
372
# File 'lib/lite/ruby/hash.rb', line 370

def symbolize_and_underscore_keys!
  replace(symbolize_and_underscore_keys)
end

#symbolize_keysObject



67
68
69
70
71
72
73
# File 'lib/lite/ruby/safe/hash.rb', line 67

def symbolize_keys
  transform_keys do |key|
    key.to_sym
  rescue StandardError
    key
  end
end

#symbolize_keys!Object



75
76
77
78
79
80
81
# File 'lib/lite/ruby/safe/hash.rb', line 75

def symbolize_keys!
  transform_keys! do |key|
    key.to_sym
  rescue StandardError
    key
  end
end

#to_objectObject Also known as: to_o



374
375
376
# File 'lib/lite/ruby/hash.rb', line 374

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

#to_open_struct(lazy: true) ⇒ Object



378
379
380
381
382
# File 'lib/lite/ruby/hash.rb', line 378

def to_open_struct(lazy: true)
  struct = OpenStruct.new(self)
  struct.methods(lazy)
  struct
end

#to_structObject



384
385
386
387
# File 'lib/lite/ruby/hash.rb', line 384

def to_struct
  struct = Struct.new(*keys)
  struct.new(*values)
end

#update_eachObject



389
390
391
# File 'lib/lite/ruby/hash.rb', line 389

def update_each
  replace(each_with_object({}) { |(key, val), hash| hash.update(yield(key, val)) })
end

#update_keysObject



393
394
395
396
397
# File 'lib/lite/ruby/hash.rb', line 393

def update_keys
  return to_enum(:update_keys) unless defined?(yield)

  replace(each_with_object({}) { |(key, val), hash| hash[yield(key)] = val })
end

#update_valuesObject



399
400
401
402
403
# File 'lib/lite/ruby/hash.rb', line 399

def update_values
  return to_enum(:update_values) unless defined?(yield)

  replace(each_with_object({}) { |(key, val), hash| hash[key] = yield(val) })
end

#vacant?(key) ⇒ Boolean

Returns:

  • (Boolean)


405
406
407
408
# File 'lib/lite/ruby/hash.rb', line 405

def vacant?(key)
  value = self[key]
  respond_to?(:blank?) ? value.blank? : !value
end