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

#collect_keys(&block) ⇒ Object

rubocop:disable Lint/UnusedMethodArgument



37
38
39
40
41
# File 'lib/active_object/hash.rb', line 37

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

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

#collect_values(&block) ⇒ Object



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

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

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

#compactObject



28
29
30
# File 'lib/active_object/hash.rb', line 28

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

#compact!Object



32
33
34
# File 'lib/active_object/hash.rb', line 32

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

#deep_merge(other_hash, &block) ⇒ Object

rubocop:enable Lint/UnusedMethodArgument



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

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

#deep_merge!(other_hash, &block) ⇒ Object

rubocop:disable Metrics/MethodLength



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/active_object/hash.rb', line 55

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



72
73
74
75
76
# File 'lib/active_object/hash.rb', line 72

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

  merge(key => delete(key))
end

#demote!(key) ⇒ Object



78
79
80
# File 'lib/active_object/hash.rb', line 78

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

#denillify(value = 0) ⇒ Object



82
83
84
# File 'lib/active_object/hash.rb', line 82

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

#denillify!(value = 0) ⇒ Object



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

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

#dig(key, *rest) ⇒ Object



90
91
92
93
94
95
96
# File 'lib/active_object/hash.rb', line 90

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



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

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

#except!(*keys) ⇒ Object



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

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

#extract!(*keys) ⇒ Object



107
108
109
# File 'lib/active_object/hash.rb', line 107

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

#hmap(&block) ⇒ Object



111
112
113
# File 'lib/active_object/hash.rb', line 111

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

#hmap!(&block) ⇒ Object

rubocop:disable Lint/UnusedMethodArgument



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

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

#nillifyObject

rubocop:enable Lint/UnusedMethodArgument



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

def nillify
  dup.nillify!
end

#nillify!Object



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

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



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

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

#only!(*keys) ⇒ Object



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

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

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



141
142
143
# File 'lib/active_object/hash.rb', line 141

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

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



145
146
147
148
149
# File 'lib/active_object/hash.rb', line 145

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)


151
152
153
# File 'lib/active_object/hash.rb', line 151

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

#promote(key) ⇒ Object



155
156
157
158
159
# File 'lib/active_object/hash.rb', line 155

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

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

#promote!(key) ⇒ Object



161
162
163
# File 'lib/active_object/hash.rb', line 161

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

#rename_keys(*keys) ⇒ Object



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

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

#rename_keys!(*keys) ⇒ Object



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

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



175
176
177
# File 'lib/active_object/hash.rb', line 175

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

#reverse_merge!(other_hash) ⇒ Object



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

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

#sampleObject



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

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

#sample!Object



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

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

#sample_keyObject



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

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

#sample_key!Object



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

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

#sample_valueObject



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

def sample_value
  fetch(sample_key)
end

#sample_value!Object



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

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

#shuffleObject



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

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

#shuffle!Object



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

def shuffle!
  replace(shuffle)
end

#slice(*keys) ⇒ Object



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

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

#slice!(*keys) ⇒ Object



227
228
229
230
231
232
233
234
235
236
# File 'lib/active_object/hash.rb', line 227

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



238
239
240
# File 'lib/active_object/hash.rb', line 238

def stringify_keys
  dup.stringify_keys!
end

#stringify_keys!Object



242
243
244
# File 'lib/active_object/hash.rb', line 242

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

#stripObject



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

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

#strip!Object



250
251
252
# File 'lib/active_object/hash.rb', line 250

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

#symbolize_and_underscore_keysObject



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

def symbolize_and_underscore_keys
  dup.symbolize_and_underscore_keys!
end

#symbolize_and_underscore_keys!Object



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

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



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

def symbolize_keys
  dup.symbolize_keys!
end

#symbolize_keys!Object



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

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

#to_oObject



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

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

#transform_keys(&block) ⇒ Object



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

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

#transform_keys!(&block) ⇒ Object

rubocop:disable Lint/UnusedMethodArgument



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

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



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

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

#transform_values!(&block) ⇒ Object

rubocop:disable Lint/UnusedMethodArgument



294
295
296
297
298
# File 'lib/active_object/hash.rb', line 294

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)


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

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