Class: Hash
- Inherits:
-
Object
- Object
- Hash
- Includes:
- ImmosquareExtensions::SharedMethods
- Defined in:
- lib/immosquare-extensions/hash.rb
Overview
##
This extension adds utility methods to the Hash class. It includes methods for handling and manipulating the keys and values of a hash in various ways.
##
Instance Method Summary collapse
-
#depth ⇒ Object
## Calculate the depth (or level) of nesting within a hash.
-
#flatten_hash ⇒ Object
## Flatten a nested hash into a single-level hash.
-
#sort_by_key(recursive: true, &block) ⇒ Object
## Sort a hash by its keys.
-
#to_beautiful_json(**options) ⇒ Object
## Returns a beautifully formatted JSON string representation of the hash.
-
#without(*keys) ⇒ Object
## Remove multiple keys from a hash in a single command.
Instance Method Details
#depth ⇒ Object
##
Calculate the depth (or level) of nesting within a hash.
Example: {b: {c: 1}}.depth => 3
##
31 32 33 34 35 |
# File 'lib/immosquare-extensions/hash.rb', line 31 def depth return 0 unless any? 1 + values.map {|v| v.is_a?(Hash) ? v.depth : 0 }.max end |
#flatten_hash ⇒ Object
##
Flatten a nested hash into a single-level hash. Nested keys are represented with dot notation.
Reference: stackoverflow.com/questions/23521230/flattening-nested-hash-to-a-single-hash-with-ruby-rails
Example: {b: {c: 1}}.flatten_hash => Hash.:a:a.b:a.b.c=>1
##
66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/immosquare-extensions/hash.rb', line 66 def flatten_hash each_with_object({}) do |(k, v), h| if v.is_a?(Hash) v.flatten_hash.map do |h_k, h_v| h["#{k}.#{h_k}".to_sym] = h_v end else h[k] = v end end end |
#sort_by_key(recursive: true, &block) ⇒ Object
##
Sort a hash by its keys. If the recursive flag is true, it will sort nested hashes as well. case-insensitive comparison and stripping of double quotes.
Reference: dan.doezema.com/2012/04/recursively-sort-ruby-hash-by-key/
Example: 1, a: {d: 4, c: 3}.sort_by_key => :d=>4, :b=>1}
##
48 49 50 51 52 53 54 |
# File 'lib/immosquare-extensions/hash.rb', line 48 def sort_by_key(recursive: true, &block) block ||= proc {|a, b| a.to_s.downcase.gsub("\"", "") <=> b.to_s.downcase.gsub("\"", "") } keys.sort(&block).each_with_object({}) do |key, seed| seed[key] = self[key] seed[key] = seed[key].sort_by_key(:recursive => recursive, &block) if recursive && seed[key].is_a?(Hash) end end |
#to_beautiful_json(**options) ⇒ Object
##
Returns a beautifully formatted JSON string representation of the hash.
Options:
-
:align => true | Whether to align the values in the output.
-
:indent_size => 2 | The number of spaces to indent.
Usage: hash.to_beautiful_json(:align => true, :indent_size => 2)
##
89 90 91 92 93 94 95 |
# File 'lib/immosquare-extensions/hash.rb', line 89 def to_beautiful_json(**) = {}.merge() [:align] = true if ![true, false].include?([:align]) [:indent_size] = 2 if [:indent_size].to_i == 0 || [:indent_size].to_i > 10 dump_beautify_json(self, [:align], [:indent_size]) end |
#without(*keys) ⇒ Object
##
Remove multiple keys from a hash in a single command.
Reference: apidock.com/ruby/Hash/delete
Examples: 1, b: 2, c: 3.without(:a, :b) => :c=>3
##
19 20 21 22 23 |
# File 'lib/immosquare-extensions/hash.rb', line 19 def without(*keys) cpy = dup keys.each {|key| cpy.delete(key) } cpy end |