Module: ActiveObject::Hash

Defined in:
lib/active_object/hash.rb

Instance Method Summary collapse

Instance Method Details

#assert_valid_keys(*valid_keys) ⇒ Object



3
4
5
6
7
8
9
10
11
12
# File 'lib/active_object/hash.rb', line 3

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



14
15
16
17
18
19
20
21
# File 'lib/active_object/hash.rb', line 14

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



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

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

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

#collect_values(&block) ⇒ Object



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

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

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

#compactObject



23
24
25
# File 'lib/active_object/hash.rb', line 23

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

#compact!Object



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

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

#deep_merge(other_hash, &block) ⇒ Object

rubocop:enable Lint/UnusedMethodArgument



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

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

#deep_merge!(other_hash, &block) ⇒ Object

rubocop:disable Metrics/MethodLength



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/active_object/hash.rb', line 50

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

#denillify(value = 0) ⇒ Object

rubocop:enable Metrics/MethodLength



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

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

#denillify!(value = 0) ⇒ Object



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

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

#dig(key, *rest) ⇒ Object



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

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



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

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

#except!(*keys) ⇒ Object



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

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

#hmap(&block) ⇒ Object



92
93
94
# File 'lib/active_object/hash.rb', line 92

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

#hmap!(&block) ⇒ Object

rubocop:disable Lint/UnusedMethodArgument



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

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

#nillifyObject

rubocop:enable Lint/UnusedMethodArgument



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

def nillify
  dup.nillify!
end

#nillify!Object



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

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

#only(*keys) ⇒ Object



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

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

#only!(*keys) ⇒ Object



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

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

#rename_keys(*keys) ⇒ Object



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

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

#rename_keys!(*keys) ⇒ Object



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

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



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

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

#reverse_merge!(other_hash) ⇒ Object



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

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

#sampleObject



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

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

#sample!Object



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

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

#sample_keyObject



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

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

#sample_key!Object



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

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

#sample_valueObject



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

def sample_value
  fetch(sample_key)
end

#sample_value!Object



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

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

#shuffleObject



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

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

#shuffle!Object



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

def shuffle!
  replace(shuffle)
end

#slice(*keys) ⇒ Object



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

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

#slice!(*keys) ⇒ Object



183
184
185
186
187
188
189
190
191
192
# File 'lib/active_object/hash.rb', line 183

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



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

def stringify_keys
  dup.stringify_keys!
end

#stringify_keys!Object



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

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

#stripObject



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

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

#strip!Object



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

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

#symbolize_and_underscore_keysObject



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

def symbolize_and_underscore_keys
  dup.symbolize_and_underscore_keys!
end

#symbolize_and_underscore_keys!Object



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

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



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

def symbolize_keys
  dup.symbolize_keys!
end

#symbolize_keys!Object



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

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

#transform_keys(&block) ⇒ Object



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

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

#transform_keys!(&block) ⇒ Object

rubocop:disable Lint/UnusedMethodArgument



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

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



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

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

#transform_values!(&block) ⇒ Object

rubocop:disable Lint/UnusedMethodArgument



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

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

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