Module: HashTools
Defined Under Namespace
Classes: Indifferent
Constant Summary collapse
- VERSION =
'1.1.2'- FWD_SLASH =
Used as the default separator for deep_fetch
'/'- INT_KEY_RE =
Regular expression to detect array indices in the path ("phones/0/code")
/^\-?\d+$/
Instance Method Summary collapse
-
#deep_fetch(hash, path, separator: FWD_SLASH, &default_blk) ⇒ Object
Fetch a deeply-nested hash key from a hash, using a String representing a path.
-
#deep_fetch_multi(hash, *key_paths, separator: FWD_SLASH) ⇒ Array
Fetches multiple keys from a deep hash, using a Strings representing paths.
-
#deep_map_value(enum_of_hashes, path, separator: FWD_SLASH) ⇒ Array
Fetches a deeply nested key from each of the Hashes in a given Array.
-
#indifferent(hash) ⇒ Indifferent
Returns an Indifferent wrapper for the given Hash.
-
#transform_keys_of(any, &transformer) ⇒ Hash
Recursively convert hash keys using a block.
-
#transform_string_keys_and_values_of(any, &transformer) ⇒ Object
Recursively transform string keys and values of a passed Hash or Array using the passed transformer.
-
#transform_string_values_of(any, &transformer) ⇒ Object
Recursively convert string values in nested hashes and arrays using a passed block.
Instance Method Details
#deep_fetch(hash, path, separator: FWD_SLASH, &default_blk) ⇒ Object
Fetch a deeply-nested hash key from a hash, using a String representing a path
deep_fetch({
'a' => {
'b' => {
'c' => value}}
}, 'a/b/c') #=> value
22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/hash_tools.rb', line 22 def deep_fetch(hash, path, separator: FWD_SLASH, &default_blk) keys = path.split(separator) keys.inject(hash) do |hash_or_array, k| if !hash_or_array.respond_to?(:fetch) raise "#{hash_or_array.inspect} does not respond to #fetch" elsif hash_or_array.is_a?(Array) && k =~ INT_KEY_RE hash_or_array.fetch(k.to_i, &default_blk) else hash_or_array.fetch(k, &default_blk) end end end |
#deep_fetch_multi(hash, *key_paths, separator: FWD_SLASH) ⇒ Array
Fetches multiple keys from a deep hash, using a Strings representing paths
deep_fetch({
'z' => 1,
'a' => {
'b' => {
'c' => value}}
}, 'a/b', 'z') #=> [value, 1]
48 49 50 |
# File 'lib/hash_tools.rb', line 48 def deep_fetch_multi(hash, *key_paths, separator: FWD_SLASH) key_paths.map{|k| deep_fetch(hash, k, separator: separator) } end |
#deep_map_value(enum_of_hashes, path, separator: FWD_SLASH) ⇒ Array
Fetches a deeply nested key from each of the Hashes in a given Array.
arr = [
{'age' => 12, 'name' => 'Jack'},
{'age' => 25, 'name' => 'Joe'},
]
deep_map_value(arr, 'age') => [12, 25]
64 65 66 |
# File 'lib/hash_tools.rb', line 64 def deep_map_value(enum_of_hashes, path, separator: FWD_SLASH) enum_of_hashes.map{|h| deep_fetch(h, path, separator: separator)} end |
#indifferent(hash) ⇒ Indifferent
Returns an Indifferent wrapper for the given Hash.
130 131 132 |
# File 'lib/hash_tools.rb', line 130 def indifferent(hash) Indifferent.new(hash) end |
#transform_keys_of(any, &transformer) ⇒ Hash
Recursively convert hash keys using a block. using a passed block. The block will receive a hash key to be transformed and should return a transformed key For example, to go from uderscored notation to camelized:
h = {'foo_bar' => 1}
transform_keys_of(h) {|k| k.to_s.camelize(:lower) } # => {'fooBar' => 1}
112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/hash_tools.rb', line 112 def transform_keys_of(any, &transformer) if any.is_a?(Array) return any.map{|e| transform_keys_of(e, &transformer) } elsif any.is_a?(Hash) h = {} any.each_pair do |k, v| h[transformer.call(k.to_s)] = transform_keys_of(v, &transformer) end h else any end end |
#transform_string_keys_and_values_of(any, &transformer) ⇒ Object
Recursively transform string keys and values of a passed Hash or Array using the passed transformer
74 75 76 |
# File 'lib/hash_tools.rb', line 74 def transform_string_keys_and_values_of(any, &transformer) transform_string_values_of(transform_keys_of(any, &transformer), &transformer) end |
#transform_string_values_of(any, &transformer) ⇒ Object
Recursively convert string values in nested hashes and arrays using a passed block. The block will receive the String to transform and should return a transformed string.
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/hash_tools.rb', line 85 def transform_string_values_of(any, &transformer) if any.is_a?(String) transformer.call(any) elsif any.is_a?(Array) any.map{|e| transform_string_values_of(e, &transformer) } elsif any.is_a?(Hash) h = {} any.each_pair do |k, v| h[k] = transform_string_values_of(v, &transformer) end h else any end end |