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

#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



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

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

#deep_merge!(other_hash, &block) ⇒ Object

rubocop:disable Metrics/MethodLength



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

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

#dig(key, *rest) ⇒ Object

rubocop:enable Metrics/MethodLength



53
54
55
56
57
58
59
# File 'lib/active_object/hash.rb', line 53

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



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

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

#except!(*keys) ⇒ Object



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

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

#hmap(&block) ⇒ Object



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

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

#hmap!(&block) ⇒ Object

rubocop:disable Lint/UnusedMethodArgument



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

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

#nillifyObject

rubocop:enable Lint/UnusedMethodArgument



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

def nillify
  dup.nillify!
end

#nillify!Object



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

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

#only(*keys) ⇒ Object



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

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

#only!(*keys) ⇒ Object



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

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

#rename_keys(*keys) ⇒ Object



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

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

#rename_keys!(*keys) ⇒ Object



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

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



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

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

#reverse_merge!(other_hash) ⇒ Object



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

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

#sampleObject



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

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

#sample!Object



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

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

#sample_keyObject



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

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

#sample_key!Object



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

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

#sample_valueObject



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

def sample_value
  fetch(sample_key)
end

#sample_value!Object



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

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

#shuffleObject



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

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

#shuffle!Object



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

def shuffle!
  replace(shuffle)
end

#slice(*keys) ⇒ Object



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

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

#slice!(*keys) ⇒ Object



161
162
163
164
165
166
167
168
169
170
# File 'lib/active_object/hash.rb', line 161

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



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

def stringify_keys
  dup.stringify_keys!
end

#stringify_keys!Object



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

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

#stripObject



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

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

#strip!Object



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

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

#symbolize_and_underscore_keysObject



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

def symbolize_and_underscore_keys
  dup.symbolize_and_underscore_keys!
end

#symbolize_and_underscore_keys!Object



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

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



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

def symbolize_keys
  dup.symbolize_keys!
end

#symbolize_keys!Object



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

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

#transform_keys(&block) ⇒ Object



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

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

#transform_keys!(&block) ⇒ Object

rubocop:disable Lint/UnusedMethodArgument



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

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



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

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

#transform_values!(&block) ⇒ Object

rubocop:disable Lint/UnusedMethodArgument



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

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

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