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|
    unless valid_keys.include?(key)
      raise ArgumentError,
            "Unknown key: #{key.inspect}. Valid keys are: #{valid_keys.map(&:inspect).join(', ')}"
    end
  end
end

#compactObject



14
15
16
# File 'lib/active_object/hash.rb', line 14

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

#compact!Object



18
19
20
# File 'lib/active_object/hash.rb', line 18

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

#deep_merge(other_hash, &block) ⇒ Object



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

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

#deep_merge!(other_hash, &block) ⇒ Object

rubocop:disable Metrics/MethodLength



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/active_object/hash.rb', line 27

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



44
45
46
47
48
49
50
51
52
53
# File 'lib/active_object/hash.rb', line 44

def dig(key, *rest)
  value = (self[key] rescue nil)
  return if value.nil?

  if rest.empty?
    value
  elsif value.respond_to?(:dig)
    value.dig(*rest)
  end
end

#except(*keys) ⇒ Object



55
56
57
# File 'lib/active_object/hash.rb', line 55

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

#except!(*keys) ⇒ Object



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

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

#hmap(&block) ⇒ Object



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

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

#hmap!(&block) ⇒ Object

rubocop:disable Lint/UnusedMethodArgument



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

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

#nillifyObject

rubocop:enable Lint/UnusedMethodArgument



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

def nillify
  dup.nillify!
end

#nillify!Object



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

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

#only(*keys) ⇒ Object



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

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

#only!(*keys) ⇒ Object



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

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

#rename_keys(*keys) ⇒ Object



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

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

#rename_keys!(*keys) ⇒ Object



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

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



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

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

#reverse_merge!(other_hash) ⇒ Object



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

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

#sampleObject



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

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

#sample!Object



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

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

#sample_keyObject



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

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

#sample_key!Object



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

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

#sample_valueObject



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

def sample_value
  fetch(sample_key)
end

#sample_value!Object



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

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

#shuffleObject



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

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

#shuffle!Object



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

def shuffle!
  replace(shuffle)
end

#slice(*keys) ⇒ Object



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

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

#slice!(*keys) ⇒ Object



156
157
158
159
160
161
162
163
164
165
# File 'lib/active_object/hash.rb', line 156

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



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

def stringify_keys
  dup.stringify_keys!
end

#stringify_keys!Object



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

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

#stripObject



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

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

#strip!Object



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

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

#symbolize_and_underscore_keysObject



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

def symbolize_and_underscore_keys
  dup.symbolize_and_underscore_keys!
end

#symbolize_and_underscore_keys!Object



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

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



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

def symbolize_keys
  dup.symbolize_keys!
end

#symbolize_keys!Object



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

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

#transform_keys(&block) ⇒ Object



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

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

#transform_keys!(&block) ⇒ Object

rubocop:disable Lint/UnusedMethodArgument



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

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



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

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

#transform_values!(&block) ⇒ Object

rubocop:disable Lint/UnusedMethodArgument



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

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

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