webapp/lib/extensions/hash_extensions.rb
# Return a new {Hash} with only one deep level
#
# Note: if the hash contains a hash in a nested array this value are skipped.
#
# @example
#   h = { foo: { bar: { baz: 1 }, bay: [2, 3, 4] } }
#   h.flatten_by_keys('.')
#   #=> { 'foo.bar.baz': 1, 'foo.bay': [2, 3, 4] }
#
# @return [Hash]
Return a new {Hash} with only one deep level
Note: if the hash contains a hash in a nested array this value are skipped.
Return a new { Hash } with only one deep level Note : if the hash contains a hash in a nested array this value are skipped .
def flatten_by_keys(separator = '.')
    h = {}
    each do |(k, v)|
      if v.is_a? Hash
        v.flatten_by_keys(separator).each { |nk, nv| h["#{k}#{separator}#{nk}".to_sym] = nv }
      elsif v.is_a? Array
        h[k.to_sym] = v.flat_map { |e| e.is_a?(Hash) ? nil : e }.compact
      else
        h[k.to_sym] = v
      end
    end
    h
  end
def flatten_by_keys ( separator = '.' ) h = { } each do | ( k , v ) | if v . is_a? Hash v . flatten_by_keys ( separator ) . each { | nk , nv | h [ "#{k}#{separator}#{nk}" . to_sym ] = nv } elsif v . is_a? Array h [ k . to_sym ] = v . flat_map { | e | e . is_a? ( Hash ) ? nil : e } . compact else h [ k . to_sym ] = v end end h end
Return a new {Hash} with only one deep level
Note: if the hash contains a hash in a nested array this value are skipped.
Return a new { Hash } with only one deep level Note : if the hash contains a hash in a nested array this value are skipped .
# skip hash nested in an array
None
Hash
separator
<not_specific>
example
None