Class: Hash

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

Instance Method Summary collapse

Instance Method Details

#assert_all_valid_keys!(*valid_keys) ⇒ Object

Raises:

  • (ArgumentError)


15
16
17
18
19
# File 'lib/lite/ruby/hash.rb', line 15

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)


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

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_valid_keys!(*valid_keys) ⇒ Object



5
6
7
8
9
10
11
12
13
# File 'lib/lite/ruby/hash.rb', line 5

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



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

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



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

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_keysObject

rubocop:enable Style/GuardClause



53
54
55
# File 'lib/lite/ruby/hash.rb', line 53

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

#collect_valuesObject



57
58
59
# File 'lib/lite/ruby/hash.rb', line 57

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

#deep_merge(other_hash, &block) ⇒ Object



61
62
63
# File 'lib/lite/ruby/hash.rb', line 61

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

#deep_merge!(other_hash, &block) ⇒ Object

rubocop:disable Metrics/MethodLength



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/lite/ruby/hash.rb', line 66

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



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

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

#demote!(key) ⇒ Object



87
88
89
90
91
92
# File 'lib/lite/ruby/hash.rb', line 87

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

  self[key] = delete(key)
  self
end

#denillify(value = 0) ⇒ Object



94
95
96
# File 'lib/lite/ruby/hash.rb', line 94

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

#denillify!(value = 0) ⇒ Object



98
99
100
# File 'lib/lite/ruby/hash.rb', line 98

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

#except(*keys) ⇒ Object



102
103
104
# File 'lib/lite/ruby/hash.rb', line 102

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

#except!(*keys) ⇒ Object



106
107
108
# File 'lib/lite/ruby/hash.rb', line 106

def except!(*keys)
  keys.each_with_object(self) { |key, _| delete(key) }
end

#extract!(*keys) ⇒ Object



110
111
112
# File 'lib/lite/ruby/hash.rb', line 110

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

#hmap(&block) ⇒ Object



114
115
116
# File 'lib/lite/ruby/hash.rb', line 114

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

#hmap!Object



118
119
120
# File 'lib/lite/ruby/hash.rb', line 118

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

#nillifyObject



122
123
124
# File 'lib/lite/ruby/hash.rb', line 122

def nillify
  dup.nillify!
end

#nillify!Object



126
127
128
129
130
# File 'lib/lite/ruby/hash.rb', line 126

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

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



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

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



136
137
138
# File 'lib/lite/ruby/hash.rb', line 136

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

#pair?(key, value) ⇒ Boolean

Returns:

  • (Boolean)


140
141
142
# File 'lib/lite/ruby/hash.rb', line 140

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

#promote(key) ⇒ Object



144
145
146
# File 'lib/lite/ruby/hash.rb', line 144

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

#promote!(key) ⇒ Object



148
149
150
151
152
# File 'lib/lite/ruby/hash.rb', line 148

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

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

#rename_keys(*keys) ⇒ Object



154
155
156
# File 'lib/lite/ruby/hash.rb', line 154

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

#rename_keys!(*keys) ⇒ Object



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

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



163
164
165
# File 'lib/lite/ruby/hash.rb', line 163

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

#reverse_merge!(other_hash) ⇒ Object



167
168
169
# File 'lib/lite/ruby/hash.rb', line 167

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

#sampleObject



171
172
173
174
# File 'lib/lite/ruby/hash.rb', line 171

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

#sample!Object



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

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

#sample_keyObject



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

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

#sample_key!Object



187
188
189
190
191
# File 'lib/lite/ruby/hash.rb', line 187

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

#sample_valueObject



193
194
195
# File 'lib/lite/ruby/hash.rb', line 193

def sample_value
  fetch(sample_key)
end

#sample_value!Object



197
198
199
200
201
# File 'lib/lite/ruby/hash.rb', line 197

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

#shuffleObject



203
204
205
# File 'lib/lite/ruby/hash.rb', line 203

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

#shuffle!Object



207
208
209
# File 'lib/lite/ruby/hash.rb', line 207

def shuffle!
  replace(shuffle)
end

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



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

def slice!(*keys)
  replace(slice(*keys))
end

#stringify_keysObject



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

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

#stringify_keys!Object



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

def stringify_keys!
  replace(stringify_keys)
end

#stripObject



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

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

#strip!Object



230
231
232
# File 'lib/lite/ruby/hash.rb', line 230

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

#symbolize_and_underscore_keysObject

rubocop:disable Metrics/MethodLength



251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/lite/ruby/hash.rb', line 251

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

    hash[new_key] = val
  end
end

#symbolize_and_underscore_keys!Object

rubocop:enable Metrics/MethodLength



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

def symbolize_and_underscore_keys!
  replace(symbolize_and_underscore_keys)
end

#symbolize_keysObject



234
235
236
237
238
239
240
241
242
243
244
# File 'lib/lite/ruby/hash.rb', line 234

def symbolize_keys
  each_with_object({}) do |(key, val), hash|
    new_key = begin
                key.to_s.to_sym
              rescue StandardError
                key
              end

    hash[new_key] = val
  end
end

#symbolize_keys!Object



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

def symbolize_keys!
  replace(symbolize_keys)
end

#to_objectObject Also known as: to_o



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

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

#vacant?(key) ⇒ Boolean

Returns:

  • (Boolean)


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

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