Module: ActiveObject::Hash

Defined in:
lib/active_object/hash.rb

Instance Method Summary collapse

Instance Method Details

#assert_valid_keys(*valid_keys) ⇒ Object



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

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



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

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



35
36
37
38
39
# File 'lib/active_object/hash.rb', line 35

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

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

#collect_values(&block) ⇒ Object



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

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

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

#compact ⇒ Object



26
27
28
# File 'lib/active_object/hash.rb', line 26

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

#compact! ⇒ Object



30
31
32
# File 'lib/active_object/hash.rb', line 30

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

#deep_merge(other_hash, &block) ⇒ Object

rubocop:enable Lint/UnusedMethodArgument



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

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

#deep_merge!(other_hash, &block) ⇒ Object

rubocop:disable Metrics/MethodLength



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

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



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

def demote(key)
  return self unless key?(key)
  merge(key => delete(key))
end

#demote!(key) ⇒ Object



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

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

#denillify(value = 0) ⇒ Object



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

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

#denillify!(value = 0) ⇒ Object



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

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

#dig(key, *rest) ⇒ Object



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

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



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

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

#except!(*keys) ⇒ Object



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

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

#hmap(&block) ⇒ Object



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

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

#hmap!(&block) ⇒ Object

rubocop:disable Lint/UnusedMethodArgument



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

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

#nillify ⇒ Object

rubocop:enable Lint/UnusedMethodArgument



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

def nillify
  dup.nillify!
end

#nillify! ⇒ Object



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

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



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

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

#only!(*keys) ⇒ Object



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

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

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



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

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

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



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

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)


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

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

#promote(key) ⇒ Object



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

def promote(key)
  return self unless key?(key)
  { key => delete(key) }.merge(self)
end

#promote!(key) ⇒ Object



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

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

#rename_keys(*keys) ⇒ Object



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

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

#rename_keys!(*keys) ⇒ Object



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

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



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

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

#reverse_merge!(other_hash) ⇒ Object



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

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

#sample ⇒ Object



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

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

#sample! ⇒ Object



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

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

#sample_key ⇒ Object



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

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

#sample_key! ⇒ Object



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

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

#sample_value ⇒ Object



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

def sample_value
  fetch(sample_key)
end

#sample_value! ⇒ Object



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

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

#shuffle ⇒ Object



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

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

#shuffle! ⇒ Object



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

def shuffle!
  replace(shuffle)
end

#slice(*keys) ⇒ Object



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

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

#slice!(*keys) ⇒ Object



220
221
222
223
224
225
226
227
228
229
# File 'lib/active_object/hash.rb', line 220

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_keys ⇒ Object



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

def stringify_keys
  dup.stringify_keys!
end

#stringify_keys! ⇒ Object



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

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

#strip ⇒ Object



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

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

#strip! ⇒ Object



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

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

#symbolize_and_underscore_keys ⇒ Object



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

def symbolize_and_underscore_keys
  dup.symbolize_and_underscore_keys!
end

#symbolize_and_underscore_keys! ⇒ Object



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

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_keys ⇒ Object



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

def symbolize_keys
  dup.symbolize_keys!
end

#symbolize_keys! ⇒ Object



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

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

#transform_keys(&block) ⇒ Object



265
266
267
# File 'lib/active_object/hash.rb', line 265

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

#transform_keys!(&block) ⇒ Object

rubocop:disable Lint/UnusedMethodArgument



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

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



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

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

#transform_values!(&block) ⇒ Object

rubocop:disable Lint/UnusedMethodArgument



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

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

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