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

#compactObject



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

#denillify(value = 0) ⇒ Object

rubocop:enable Metrics/MethodLength



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

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

#denillify!(value = 0) ⇒ Object



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

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

#dig(key, *rest) ⇒ Object



78
79
80
81
82
83
84
# File 'lib/active_object/hash.rb', line 78

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



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

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

#except!(*keys) ⇒ Object



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

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

#hmap(&block) ⇒ Object



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

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

#hmap!(&block) ⇒ Object

rubocop:disable Lint/UnusedMethodArgument



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

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

#nillifyObject

rubocop:enable Lint/UnusedMethodArgument



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

def nillify
  dup.nillify!
end

#nillify!Object



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

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

#only(*keys) ⇒ Object



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

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

#only!(*keys) ⇒ Object



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

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

#rename_keys(*keys) ⇒ Object



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

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

#rename_keys!(*keys) ⇒ Object



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

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



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

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

#reverse_merge!(other_hash) ⇒ Object



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

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

#sampleObject



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

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

#sample!Object



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

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

#sample_keyObject



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

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

#sample_key!Object



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

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

#sample_valueObject



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

def sample_value
  fetch(sample_key)
end

#sample_value!Object



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

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

#shuffleObject



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

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

#shuffle!Object



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

def shuffle!
  replace(shuffle)
end

#slice(*keys) ⇒ Object



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

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

#slice!(*keys) ⇒ Object



186
187
188
189
190
191
192
193
194
195
# File 'lib/active_object/hash.rb', line 186

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



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

def stringify_keys
  dup.stringify_keys!
end

#stringify_keys!Object



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

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

#stripObject



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

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

#strip!Object



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

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

#symbolize_and_underscore_keysObject



221
222
223
# File 'lib/active_object/hash.rb', line 221

def symbolize_and_underscore_keys
  dup.symbolize_and_underscore_keys!
end

#symbolize_and_underscore_keys!Object



225
226
227
228
229
# File 'lib/active_object/hash.rb', line 225

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



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

def symbolize_keys
  dup.symbolize_keys!
end

#symbolize_keys!Object



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

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

#transform_keys(&block) ⇒ Object



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

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

#transform_keys!(&block) ⇒ Object

rubocop:disable Lint/UnusedMethodArgument



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

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



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

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

#transform_values!(&block) ⇒ Object

rubocop:disable Lint/UnusedMethodArgument



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

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

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