Module: ActiveObject::Hash
- Defined in:
- lib/active_object/hash.rb
Instance Method Summary collapse
- #assert_valid_keys(*valid_keys) ⇒ Object
- #assert_valid_keys!(*valid_keys) ⇒ Object
-
#collect_keys(&block) ⇒ Object
rubocop:disable Lint/UnusedMethodArgument.
- #collect_values(&block) ⇒ Object
- #compact ⇒ Object
- #compact! ⇒ Object
-
#deep_merge(other_hash, &block) ⇒ Object
rubocop:enable Lint/UnusedMethodArgument.
-
#deep_merge!(other_hash, &block) ⇒ Object
rubocop:disable Metrics/MethodLength.
-
#demote(key) ⇒ Object
rubocop:enable Metrics/MethodLength.
- #demote!(key) ⇒ Object
- #denillify(value = 0) ⇒ Object
- #denillify!(value = 0) ⇒ Object
- #dig(key, *rest) ⇒ Object
- #except(*keys) ⇒ Object
- #except!(*keys) ⇒ Object
- #hmap(&block) ⇒ Object
-
#hmap!(&block) ⇒ Object
rubocop:disable Lint/UnusedMethodArgument.
-
#nillify ⇒ Object
rubocop:enable Lint/UnusedMethodArgument.
- #nillify! ⇒ Object
- #only(*keys) ⇒ Object
- #only!(*keys) ⇒ Object
- #only_fill(*keys, placeholder: nil) ⇒ Object
- #only_fill!(*keys, placeholder: nil) ⇒ Object
- #pair?(key, value) ⇒ Boolean
- #promote(key) ⇒ Object
- #promote!(key) ⇒ Object
- #rename_keys(*keys) ⇒ Object
- #rename_keys!(*keys) ⇒ Object
- #reverse_merge(other_hash) ⇒ Object
- #reverse_merge!(other_hash) ⇒ Object
- #sample ⇒ Object
- #sample! ⇒ Object
- #sample_key ⇒ Object
- #sample_key! ⇒ Object
- #sample_value ⇒ Object
- #sample_value! ⇒ Object
- #shuffle ⇒ Object
- #shuffle! ⇒ Object
- #slice(*keys) ⇒ Object
- #slice!(*keys) ⇒ Object
- #stringify_keys ⇒ Object
- #stringify_keys! ⇒ Object
- #strip ⇒ Object
- #strip! ⇒ Object
- #symbolize_and_underscore_keys ⇒ Object
- #symbolize_and_underscore_keys! ⇒ Object
- #symbolize_keys ⇒ Object
- #symbolize_keys! ⇒ Object
- #transform_keys(&block) ⇒ Object
-
#transform_keys!(&block) ⇒ Object
rubocop:disable Lint/UnusedMethodArgument.
-
#transform_values(&block) ⇒ Object
rubocop:enable Lint/UnusedMethodArgument.
-
#transform_values!(&block) ⇒ Object
rubocop:disable Lint/UnusedMethodArgument.
-
#vacant?(key) ⇒ Boolean
rubocop:enable Lint/UnusedMethodArgument.
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 74 |
# File 'lib/active_object/hash.rb', line 70 def demote(key) return self unless key?(key) merge(key => delete(key)) end |
#demote!(key) ⇒ Object
76 77 78 |
# File 'lib/active_object/hash.rb', line 76 def demote!(key) replace(demote(key)) end |
#denillify(value = 0) ⇒ Object
80 81 82 |
# File 'lib/active_object/hash.rb', line 80 def denillify(value = 0) each { |key, val| self[key] = val.nil? ? value : val } end |
#denillify!(value = 0) ⇒ Object
84 85 86 |
# File 'lib/active_object/hash.rb', line 84 def denillify!(value = 0) replace(denillify(value)) end |
#dig(key, *rest) ⇒ Object
88 89 90 91 92 93 94 |
# File 'lib/active_object/hash.rb', line 88 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
96 97 98 |
# File 'lib/active_object/hash.rb', line 96 def except(*keys) dup.except!(*keys) end |
#except!(*keys) ⇒ Object
100 101 102 103 |
# File 'lib/active_object/hash.rb', line 100 def except!(*keys) keys.flatten.each { |key| delete(key) } self end |
#hmap(&block) ⇒ Object
105 106 107 |
# File 'lib/active_object/hash.rb', line 105 def hmap(&block) dup.hmap!(&block) end |
#hmap!(&block) ⇒ Object
rubocop:disable Lint/UnusedMethodArgument
110 111 112 |
# File 'lib/active_object/hash.rb', line 110 def hmap!(&block) inject({}) { |hash, (key, val)| hash.merge(yield(key, val)) } end |
#nillify ⇒ Object
rubocop:enable Lint/UnusedMethodArgument
115 116 117 |
# File 'lib/active_object/hash.rb', line 115 def nillify dup.nillify! end |
#nillify! ⇒ Object
119 120 121 122 123 |
# File 'lib/active_object/hash.rb', line 119 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
125 126 127 |
# File 'lib/active_object/hash.rb', line 125 def only(*keys) dup.only!(*keys) end |
#only!(*keys) ⇒ Object
129 130 131 132 133 |
# File 'lib/active_object/hash.rb', line 129 def only!(*keys) hash = {} keys.flatten.each { |key| hash[key] = self[key] if key?(key) } replace(hash) end |
#only_fill(*keys, placeholder: nil) ⇒ Object
135 136 137 |
# File 'lib/active_object/hash.rb', line 135 def only_fill(*keys, placeholder: nil) dup.only_fill!(*keys, placeholder: placeholder) end |
#only_fill!(*keys, placeholder: nil) ⇒ Object
139 140 141 142 143 |
# File 'lib/active_object/hash.rb', line 139 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
145 146 147 |
# File 'lib/active_object/hash.rb', line 145 def pair?(key, value) self[key] == value end |
#promote(key) ⇒ Object
149 150 151 152 153 |
# File 'lib/active_object/hash.rb', line 149 def promote(key) return self unless key?(key) { key => delete(key) }.merge(self) end |
#promote!(key) ⇒ Object
155 156 157 |
# File 'lib/active_object/hash.rb', line 155 def promote!(key) replace(promote(key)) end |
#rename_keys(*keys) ⇒ Object
159 160 161 |
# File 'lib/active_object/hash.rb', line 159 def rename_keys(*keys) dup.rename_keys!(*keys) end |
#rename_keys!(*keys) ⇒ Object
163 164 165 166 167 |
# File 'lib/active_object/hash.rb', line 163 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
169 170 171 |
# File 'lib/active_object/hash.rb', line 169 def reverse_merge(other_hash) other_hash.merge(self) end |
#reverse_merge!(other_hash) ⇒ Object
173 174 175 |
# File 'lib/active_object/hash.rb', line 173 def reverse_merge!(other_hash) replace(reverse_merge(other_hash)) end |
#sample ⇒ Object
177 178 179 180 |
# File 'lib/active_object/hash.rb', line 177 def sample key = sample_key [key, fetch(key)] end |
#sample! ⇒ Object
182 183 184 185 186 |
# File 'lib/active_object/hash.rb', line 182 def sample! key, value = sample delete(key) [key, value] end |
#sample_key ⇒ Object
188 189 190 191 |
# File 'lib/active_object/hash.rb', line 188 def sample_key hash_keys = keys hash_keys.at(::Random.rand(hash_keys.length - 1)) end |
#sample_key! ⇒ Object
193 194 195 196 197 |
# File 'lib/active_object/hash.rb', line 193 def sample_key! key, = sample delete(key) key end |
#sample_value ⇒ Object
199 200 201 |
# File 'lib/active_object/hash.rb', line 199 def sample_value fetch(sample_key) end |
#sample_value! ⇒ Object
203 204 205 206 207 |
# File 'lib/active_object/hash.rb', line 203 def sample_value! key, value = sample delete(key) value end |
#shuffle ⇒ Object
209 210 211 |
# File 'lib/active_object/hash.rb', line 209 def shuffle ::Hash[to_a.sample(length)] end |
#shuffle! ⇒ Object
213 214 215 |
# File 'lib/active_object/hash.rb', line 213 def shuffle! replace(shuffle) end |
#slice(*keys) ⇒ Object
217 218 219 220 |
# File 'lib/active_object/hash.rb', line 217 def slice(*keys) keys.flatten .each_with_object(self.class.new) { |key, hsh| hsh[key] = self[key] if key?(key) } end |
#slice!(*keys) ⇒ Object
222 223 224 225 226 227 228 229 230 231 |
# File 'lib/active_object/hash.rb', line 222 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
233 234 235 |
# File 'lib/active_object/hash.rb', line 233 def stringify_keys dup.stringify_keys! end |
#stringify_keys! ⇒ Object
237 238 239 |
# File 'lib/active_object/hash.rb', line 237 def stringify_keys! each_with_object({}) { |(key, val), | [key.to_s] = val } end |
#strip ⇒ Object
241 242 243 |
# File 'lib/active_object/hash.rb', line 241 def strip select { |_, val| !val.blank? } end |
#strip! ⇒ Object
245 246 247 |
# File 'lib/active_object/hash.rb', line 245 def strip! reject! { |_, val| val.blank? } end |
#symbolize_and_underscore_keys ⇒ Object
257 258 259 |
# File 'lib/active_object/hash.rb', line 257 def symbolize_and_underscore_keys dup.symbolize_and_underscore_keys! end |
#symbolize_and_underscore_keys! ⇒ Object
261 262 263 264 265 |
# File 'lib/active_object/hash.rb', line 261 def symbolize_and_underscore_keys! each_with_object({}) do |(key, val), | [(key.to_s.tr(' ', '_').underscore.to_sym rescue key) || key] = val end end |
#symbolize_keys ⇒ Object
249 250 251 |
# File 'lib/active_object/hash.rb', line 249 def symbolize_keys dup.symbolize_keys! end |
#symbolize_keys! ⇒ Object
253 254 255 |
# File 'lib/active_object/hash.rb', line 253 def symbolize_keys! each_with_object({}) { |(key, val), | [(key.to_sym rescue key) || key] = val } end |
#transform_keys(&block) ⇒ Object
267 268 269 |
# File 'lib/active_object/hash.rb', line 267 def transform_keys(&block) dup.transform_keys!(&block) end |
#transform_keys!(&block) ⇒ Object
rubocop:disable Lint/UnusedMethodArgument
272 273 274 275 276 277 |
# File 'lib/active_object/hash.rb', line 272 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
280 281 282 |
# File 'lib/active_object/hash.rb', line 280 def transform_values(&block) dup.transform_values!(&block) end |
#transform_values!(&block) ⇒ Object
rubocop:disable Lint/UnusedMethodArgument
285 286 287 288 289 |
# File 'lib/active_object/hash.rb', line 285 def transform_values!(&block) return(enum_for(:transform_values!)) unless block_given? each { |key, val| self[key] = yield(val) } end |
#vacant?(key) ⇒ Boolean
rubocop:enable Lint/UnusedMethodArgument
292 293 294 |
# File 'lib/active_object/hash.rb', line 292 def vacant?(key) self[key].blank? end |