Class: Hash
Class Method Summary collapse
Instance Method Summary collapse
- #aka(new_key, old_key) ⇒ Object
- #assert_all_min_keys!(*valid_keys) ⇒ Object
- #assert_all_pair_presence!(*valid_keys) ⇒ Object
- #assert_all_valid_keys!(*valid_keys) ⇒ Object
- #assert_all_valid_values!(*valid_values) ⇒ Object
- #assert_min_keys!(*valid_keys) ⇒ Object
- #assert_pair_presence!(*valid_keys) ⇒ Object
- #assert_valid_keys!(*valid_keys) ⇒ Object
- #assert_valid_values!(*valid_values) ⇒ Object
-
#bury(*args) ⇒ Object
rubocop:disable Style/GuardClause.
-
#collate(*others) ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength.
-
#collate!(other_hash) ⇒ Object
rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength.
- #collect_keys(&block) ⇒ Object (also: #map_keys)
- #collect_values(&block) ⇒ Object (also: #map_values)
- #dearray_singular_values ⇒ Object
- #dearray_singular_values! ⇒ Object
- #dearray_values(idx = 0) ⇒ Object
- #dearray_values!(idx = 0) ⇒ Object
-
#deep_dup ⇒ Object
rubocop:disable Style/CaseEquality.
- #deep_fetch(*keys, &block) ⇒ Object
- #deep_key?(*keys) ⇒ Boolean
-
#deep_merge(other_hash, &block) ⇒ Object
rubocop:enable Style/CaseEquality.
- #deep_merge!(other_hash, &block) ⇒ Object
- #delete_unless ⇒ Object
- #delete_values(*values) ⇒ Object
- #demote(key) ⇒ Object
- #demote!(key) ⇒ Object
- #denillify(identity = 0) ⇒ Object
- #denillify!(identity = 0) ⇒ Object
- #diff(hash) ⇒ Object
- #except(*keys) ⇒ Object
- #except!(*keys) ⇒ Object
- #extract!(*keys) ⇒ Object
- #hmap(&block) ⇒ Object
- #hmap! ⇒ Object
- #insert(name, value) ⇒ Object
- #invert ⇒ Object
- #keys?(*check_keys) ⇒ Boolean (also: #has_keys?)
- #nillify ⇒ Object
- #nillify! ⇒ Object
- #only_fill(*keys, placeholder: nil) ⇒ Object
- #only_fill!(*keys, placeholder: nil) ⇒ Object
- #only_keys?(*check_keys) ⇒ Boolean (also: #has_only_keys?)
- #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 (also: #only!)
- #stringify_keys ⇒ Object
- #stringify_keys! ⇒ Object
- #strip ⇒ Object
- #strip! ⇒ Object
-
#symbolize_and_underscore_keys ⇒ Object
rubocop:disable Metrics/MethodLength.
-
#symbolize_and_underscore_keys! ⇒ Object
rubocop:enable Metrics/MethodLength.
- #symbolize_keys ⇒ Object
- #symbolize_keys! ⇒ Object
- #to_object ⇒ Object (also: #to_o)
- #to_open_struct(lazy: true) ⇒ Object
- #to_struct ⇒ Object
- #update_each ⇒ Object
- #update_keys ⇒ Object
- #update_values ⇒ Object
- #vacant?(key) ⇒ Boolean
Class Method Details
.zip(keys, values) ⇒ Object
7 8 9 |
# File 'lib/lite/ruby/hash.rb', line 7 def zip(keys, values) keys.size.times.with_object({}) { |i, hash| hash[keys[i]] = values[i] } end |
Instance Method Details
#aka(new_key, old_key) ⇒ Object
13 14 15 16 |
# File 'lib/lite/ruby/hash.rb', line 13 def aka(new_key, old_key) self[new_key] = self[old_key] if key?(old_key) self end |
#assert_all_min_keys!(*valid_keys) ⇒ Object
32 33 34 35 36 |
# File 'lib/lite/ruby/hash.rb', line 32 def assert_all_min_keys!(*valid_keys) return assert_min_keys!(*valid_keys) unless empty? raise ArgumentError, 'An empty hash is not allowed' end |
#assert_all_pair_presence!(*valid_keys) ⇒ Object
50 51 52 53 54 |
# File 'lib/lite/ruby/hash.rb', line 50 def assert_all_pair_presence!(*valid_keys) return assert_pair_presence!(*valid_keys) unless empty? raise ArgumentError, 'An empty hash is not allowed' end |
#assert_all_valid_keys!(*valid_keys) ⇒ Object
66 67 68 69 70 |
# File 'lib/lite/ruby/hash.rb', line 66 def assert_all_valid_keys!(*valid_keys) return assert_valid_keys!(*valid_keys) unless empty? raise ArgumentError, 'An empty hash is not allowed' end |
#assert_all_valid_values!(*valid_values) ⇒ Object
82 83 84 85 86 |
# File 'lib/lite/ruby/hash.rb', line 82 def assert_all_valid_values!(*valid_values) return assert_valid_values!(*valid_values) unless empty? raise ArgumentError, 'An empty hash is not allowed' end |
#assert_min_keys!(*valid_keys) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/lite/ruby/hash.rb', line 18 def assert_min_keys!(*valid_keys) return self if empty? valid_keys.each do |key| next if key?(key) raise ArgumentError, "Missing key: #{key.inspect}. " \ "Minimum keys are: #{valid_keys.map(&:inspect).join(', ')}" end self end |
#assert_pair_presence!(*valid_keys) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/lite/ruby/hash.rb', line 38 def assert_pair_presence!(*valid_keys) each do |key, value| if !valid_keys.include?(key) raise ArgumentError, "Invalid key: #{key.inspect}. " \ "Allowed keys are: #{valid_keys.map(&:inspect).join(', ')}" elsif value.respond_to?(:blank?) ? value.blank? : !value raise ArgumentError, "A #{value.inspect} value for #{key.inspect} is not allowed" end end end |
#assert_valid_keys!(*valid_keys) ⇒ Object
56 57 58 59 60 61 62 63 64 |
# File 'lib/lite/ruby/hash.rb', line 56 def assert_valid_keys!(*valid_keys) each_key do |key| next if valid_keys.include?(key) raise ArgumentError, "Invalid key: #{key.inspect}. " \ "Allowed keys are: #{valid_keys.map(&:inspect).join(', ')}" end end |
#assert_valid_values!(*valid_values) ⇒ Object
72 73 74 75 76 77 78 79 80 |
# File 'lib/lite/ruby/hash.rb', line 72 def assert_valid_values!(*valid_values) each_value do |value| next if valid_values.include?(value) raise ArgumentError, "Invalid value: #{value.inspect}. " \ "Allowed values are: #{valid_values.map(&:inspect).join(', ')}" end end |
#bury(*args) ⇒ Object
rubocop:disable Style/GuardClause
89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/lite/ruby/hash.rb', line 89 def bury(*args) if args.count < 2 raise ArgumentError, '2 or more arguments required' elsif args.count == 2 self[args[0]] = args[1] else arg = args.shift self[arg] = {} unless self[arg] self[arg].bury(*args) unless args.empty? end self end |
#collate(*others) ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/lite/ruby/hash.rb', line 105 def collate(*others) hash = {} each_key { |key| hash[key] = [] } others.each do |other| other.each_key { |key| hash[key] = [] } end each { |key, val| hash[key] << val } others.each do |other| other.each { |key, val| hash[key] << val } end hash.each_value(&:flatten!) hash end |
#collate!(other_hash) ⇒ Object
rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength
125 126 127 |
# File 'lib/lite/ruby/hash.rb', line 125 def collate!(other_hash) replace(collate(other_hash)) end |
#collect_keys(&block) ⇒ Object Also known as: map_keys
129 130 131 |
# File 'lib/lite/ruby/hash.rb', line 129 def collect_keys(&block) keys.map(&block) end |
#collect_values(&block) ⇒ Object Also known as: map_values
133 134 135 |
# File 'lib/lite/ruby/hash.rb', line 133 def collect_values(&block) values.map(&block) end |
#dearray_singular_values ⇒ Object
150 151 152 153 154 155 156 157 |
# File 'lib/lite/ruby/hash.rb', line 150 def dearray_singular_values each_with_object({}) do |(key, val), hash| hash[key] = case val when Array then val.size < 2 ? val[0] : val else val end end end |
#dearray_singular_values! ⇒ Object
159 160 161 |
# File 'lib/lite/ruby/hash.rb', line 159 def dearray_singular_values! replace(dearray_singular_values) end |
#dearray_values(idx = 0) ⇒ Object
137 138 139 140 141 142 143 144 |
# File 'lib/lite/ruby/hash.rb', line 137 def dearray_values(idx = 0) each_with_object({}) do |(key, val), hash| hash[key] = case val when Array then val[idx] || val[-1] else val end end end |
#dearray_values!(idx = 0) ⇒ Object
146 147 148 |
# File 'lib/lite/ruby/hash.rb', line 146 def dearray_values!(idx = 0) replace(dearray_values(idx)) end |
#deep_dup ⇒ Object
rubocop:disable Style/CaseEquality
6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/lite/ruby/safe/hash.rb', line 6 def deep_dup hash = dup each_pair do |key, value| if key.frozen? && ::String === key hash[key] = value.deep_dup else hash.delete(key) hash[key.deep_dup] = value.deep_dup end end hash end |
#deep_fetch(*keys, &block) ⇒ Object
163 164 165 |
# File 'lib/lite/ruby/hash.rb', line 163 def deep_fetch(*keys, &block) keys.reduce(self) { |hash_object, key| hash_object.fetch(key, &block) } end |
#deep_key?(*keys) ⇒ Boolean
167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/lite/ruby/hash.rb', line 167 def deep_key?(*keys) last_hash = self found_key = false keys.each do |key| break found_key = false unless last_hash.key?(key) last_hash = last_hash[key] found_key = true end found_key end |
#deep_merge(other_hash, &block) ⇒ Object
rubocop:enable Style/CaseEquality
22 23 24 |
# File 'lib/lite/ruby/safe/hash.rb', line 22 def deep_merge(other_hash, &block) dup.deep_merge!(other_hash, &block) end |
#deep_merge!(other_hash, &block) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/lite/ruby/safe/hash.rb', line 26 def deep_merge!(other_hash, &block) merge!(other_hash) do |key, this_val, other_val| if this_val.is_a?(Hash) && other_val.is_a?(Hash) this_val.deep_merge(other_val, &block) elsif defined?(yield) yield(key, this_val, other_val) else other_val end end end |
#delete_unless ⇒ Object
181 182 183 |
# File 'lib/lite/ruby/hash.rb', line 181 def delete_unless delete_if { |key, val| !yield(key, val) } end |
#delete_values(*values) ⇒ Object
185 186 187 188 189 190 191 192 |
# File 'lib/lite/ruby/hash.rb', line 185 def delete_values(*values) each_key.with_object([]) do |key, array| next unless values.include?(self[key]) array << key delete(key) end end |
#demote(key) ⇒ Object
194 195 196 |
# File 'lib/lite/ruby/hash.rb', line 194 def demote(key) dup.demote!(key) end |
#demote!(key) ⇒ Object
198 199 200 201 202 203 |
# File 'lib/lite/ruby/hash.rb', line 198 def demote!(key) return self unless key?(key) self[key] = delete(key) self end |
#denillify(identity = 0) ⇒ Object
205 206 207 |
# File 'lib/lite/ruby/hash.rb', line 205 def denillify(identity = 0) dup.denillify!(identity) end |
#denillify!(identity = 0) ⇒ Object
209 210 211 |
# File 'lib/lite/ruby/hash.rb', line 209 def denillify!(identity = 0) each { |key, val| self[key] = val || identity } end |
#diff(hash) ⇒ Object
213 214 215 216 217 |
# File 'lib/lite/ruby/hash.rb', line 213 def diff(hash) h1 = dup.delete_if { |k, v| hash[k] == v } h2 = hash.dup.delete_if { |k, _| key?(k) } h1.merge(h2) end |
#except(*keys) ⇒ Object
38 39 40 |
# File 'lib/lite/ruby/safe/hash.rb', line 38 def except(*keys) slice(*self.keys - keys) end |
#except!(*keys) ⇒ Object
42 43 44 45 |
# File 'lib/lite/ruby/safe/hash.rb', line 42 def except!(*keys) keys.each { |key| delete(key) } self end |
#extract!(*keys) ⇒ Object
47 48 49 |
# File 'lib/lite/ruby/safe/hash.rb', line 47 def extract!(*keys) keys.each_with_object({}) { |key, hash| hash[key] = delete(key) if key?(key) } end |
#hmap(&block) ⇒ Object
219 220 221 |
# File 'lib/lite/ruby/hash.rb', line 219 def hmap(&block) dup.hmap!(&block) end |
#hmap! ⇒ Object
223 224 225 |
# File 'lib/lite/ruby/hash.rb', line 223 def hmap! inject(self) { |hash, (key, val)| hash.merge(yield(key, val)) } end |
#insert(name, value) ⇒ Object
227 228 229 230 231 232 |
# File 'lib/lite/ruby/hash.rb', line 227 def insert(name, value) return false if key?(name) store(name, value) true end |
#invert ⇒ Object
234 235 236 237 238 239 240 241 242 |
# File 'lib/lite/ruby/hash.rb', line 234 def invert each_pair.with_object({}) do |(key, val), hash| if val.is_a?(Array) val.each { |x| hash[x] = (hash.key?(x) ? [key, hash[x]].flatten : key) } else hash[val] = (hash.key?(val) ? [key, hash[val]].flatten : key) end end end |
#keys?(*check_keys) ⇒ Boolean Also known as: has_keys?
244 245 246 247 |
# File 'lib/lite/ruby/hash.rb', line 244 def keys?(*check_keys) unknown_keys = check_keys - keys unknown_keys.empty? end |
#nillify ⇒ Object
249 250 251 |
# File 'lib/lite/ruby/hash.rb', line 249 def nillify dup.nillify! end |
#nillify! ⇒ Object
253 254 255 256 257 258 259 |
# File 'lib/lite/ruby/hash.rb', line 253 def nillify! each do |key, val| next if val.nil? self[key] = nil if respond_to?(:blank?) ? val.blank? : !val end end |
#only_fill(*keys, placeholder: nil) ⇒ Object
261 262 263 |
# File 'lib/lite/ruby/hash.rb', line 261 def only_fill(*keys, placeholder: nil) keys.each_with_object({}) { |key, hash| hash[key] = key?(key) ? self[key] : placeholder } end |
#only_fill!(*keys, placeholder: nil) ⇒ Object
265 266 267 |
# File 'lib/lite/ruby/hash.rb', line 265 def only_fill!(*keys, placeholder: nil) replace(only_fill(*keys, placeholder: placeholder)) end |
#only_keys?(*check_keys) ⇒ Boolean Also known as: has_only_keys?
269 270 271 272 |
# File 'lib/lite/ruby/hash.rb', line 269 def only_keys?(*check_keys) unknown_keys = keys - check_keys unknown_keys.empty? end |
#pair?(key, value) ⇒ Boolean
274 275 276 |
# File 'lib/lite/ruby/hash.rb', line 274 def pair?(key, value) self[key] == value end |
#promote(key) ⇒ Object
278 279 280 |
# File 'lib/lite/ruby/hash.rb', line 278 def promote(key) dup.promote!(key) end |
#promote!(key) ⇒ Object
282 283 284 285 286 |
# File 'lib/lite/ruby/hash.rb', line 282 def promote!(key) return self unless key?(key) { key => delete(key) }.merge(self) end |
#rename_keys(*keys) ⇒ Object
288 289 290 |
# File 'lib/lite/ruby/hash.rb', line 288 def rename_keys(*keys) dup.rename_keys!(*keys) end |
#rename_keys!(*keys) ⇒ Object
292 293 294 295 |
# File 'lib/lite/ruby/hash.rb', line 292 def rename_keys!(*keys) keys = Hash[*keys] keys.each_with_object(self) { |(key, val), hash| hash[val] = delete(key) if hash[key] } end |
#reverse_merge(other_hash) ⇒ Object
51 52 53 |
# File 'lib/lite/ruby/safe/hash.rb', line 51 def reverse_merge(other_hash) other_hash.merge(self) end |
#reverse_merge!(other_hash) ⇒ Object
55 56 57 |
# File 'lib/lite/ruby/safe/hash.rb', line 55 def reverse_merge!(other_hash) other_hash.merge!(self) end |
#sample ⇒ Object
297 298 299 300 |
# File 'lib/lite/ruby/hash.rb', line 297 def sample key = sample_key [key, fetch(key)] end |
#sample! ⇒ Object
302 303 304 305 306 |
# File 'lib/lite/ruby/hash.rb', line 302 def sample! key, value = sample delete(key) [key, value] end |
#sample_key ⇒ Object
308 309 310 311 |
# File 'lib/lite/ruby/hash.rb', line 308 def sample_key hash_keys = keys hash_keys.at(Random.rand(hash_keys.size - 1)) end |
#sample_key! ⇒ Object
313 314 315 316 317 |
# File 'lib/lite/ruby/hash.rb', line 313 def sample_key! key, _val = sample delete(key) key end |
#sample_value ⇒ Object
319 320 321 |
# File 'lib/lite/ruby/hash.rb', line 319 def sample_value fetch(sample_key) end |
#sample_value! ⇒ Object
323 324 325 326 327 |
# File 'lib/lite/ruby/hash.rb', line 323 def sample_value! key, value = sample delete(key) value end |
#shuffle ⇒ Object
329 330 331 |
# File 'lib/lite/ruby/hash.rb', line 329 def shuffle to_a.sample(size).to_h end |
#shuffle! ⇒ Object
333 334 335 |
# File 'lib/lite/ruby/hash.rb', line 333 def shuffle! replace(shuffle) end |
#slice!(*keys) ⇒ Object Also known as: only!
337 338 339 340 341 342 343 344 |
# File 'lib/lite/ruby/hash.rb', line 337 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
59 60 61 |
# File 'lib/lite/ruby/safe/hash.rb', line 59 def stringify_keys transform_keys(&:to_s) end |
#stringify_keys! ⇒ Object
63 64 65 |
# File 'lib/lite/ruby/safe/hash.rb', line 63 def stringify_keys! transform_keys!(&:to_s) end |
#strip ⇒ Object
346 347 348 |
# File 'lib/lite/ruby/hash.rb', line 346 def strip reject { |_, val| respond_to?(:blank?) ? val.blank? : !val } end |
#strip! ⇒ Object
350 351 352 |
# File 'lib/lite/ruby/hash.rb', line 350 def strip! reject! { |_, val| respond_to?(:blank?) ? val.blank? : !val } end |
#symbolize_and_underscore_keys ⇒ Object
rubocop:disable Metrics/MethodLength
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 |
# File 'lib/lite/ruby/hash.rb', line 355 def symbolize_and_underscore_keys each_with_object({}) do |(key, val), hash| new_key = begin str = key.dup.to_s str.gsub!(/::/, '/') || str str.gsub!(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2') || str str.gsub!(/([a-z\d])([A-Z])/, '\1_\2') || str str.tr!(' -', '_') || str str.downcase! str.to_sym rescue StandardError key end hash[new_key] = val end end |
#symbolize_and_underscore_keys! ⇒ Object
rubocop:enable Metrics/MethodLength
374 375 376 |
# File 'lib/lite/ruby/hash.rb', line 374 def symbolize_and_underscore_keys! replace(symbolize_and_underscore_keys) end |
#symbolize_keys ⇒ Object
67 68 69 70 71 72 73 |
# File 'lib/lite/ruby/safe/hash.rb', line 67 def symbolize_keys transform_keys do |key| key.to_sym rescue StandardError key end end |
#symbolize_keys! ⇒ Object
75 76 77 78 79 80 81 |
# File 'lib/lite/ruby/safe/hash.rb', line 75 def symbolize_keys! transform_keys! do |key| key.to_sym rescue StandardError key end end |
#to_object ⇒ Object Also known as: to_o
378 379 380 |
# File 'lib/lite/ruby/hash.rb', line 378 def to_object JSON.parse(to_json, object_class: OpenStruct) end |
#to_open_struct(lazy: true) ⇒ Object
382 383 384 385 386 |
# File 'lib/lite/ruby/hash.rb', line 382 def to_open_struct(lazy: true) struct = OpenStruct.new(self) struct.methods(lazy) struct end |
#to_struct ⇒ Object
388 389 390 391 |
# File 'lib/lite/ruby/hash.rb', line 388 def to_struct struct = Struct.new(*keys) struct.new(*values) end |
#update_each ⇒ Object
393 394 395 |
# File 'lib/lite/ruby/hash.rb', line 393 def update_each replace(each_with_object({}) { |(key, val), hash| hash.update(yield(key, val)) }) end |
#update_keys ⇒ Object
397 398 399 400 401 |
# File 'lib/lite/ruby/hash.rb', line 397 def update_keys return to_enum(:update_keys) unless defined?(yield) replace(each_with_object({}) { |(key, val), hash| hash[yield(key)] = val }) end |
#update_values ⇒ Object
403 404 405 406 407 |
# File 'lib/lite/ruby/hash.rb', line 403 def update_values return to_enum(:update_values) unless defined?(yield) replace(each_with_object({}) { |(key, val), hash| hash[key] = yield(val) }) end |
#vacant?(key) ⇒ Boolean
409 410 411 412 |
# File 'lib/lite/ruby/hash.rb', line 409 def vacant?(key) value = self[key] respond_to?(:blank?) ? value.blank? : !value end |