class Hash

Public Instance Methods

flatten_keys()
flatten_keys_to_array() click to toggle source

Returns a flat hash where all nested keys are collapsed into an array of keys.

hash = { person: { name: { first: 'Rob' }, age: '28' } }
hash.flatten_keys_to_array
=> {[:person, :name, :first] => "Rob", [:person, :age]=>"28" }
# File lib/patches/core_ext/hash/flatten_keys.rb, line 11
def flatten_keys_to_array
  _flatten_keys(self)
end
Also aliased as: flatten_keys
flatten_keys_to_dotpath() click to toggle source

Returns a flat hash where all nested keys are collapsed into a dot-separated string of keys.

hash = { person: { name: { first: 'Rob' }, age: '28' } }
hash.flatten_keys_to_dotpath
=> { 'person.name.first' => "Rob", 'person.age'=>"28" }
# File lib/patches/core_ext/hash/flatten_keys.rb, line 21
def flatten_keys_to_dotpath
  _flatten_keys(self, ->(*keys) { keys.join('.') })
end
flatten_keys_to_html_attribute() click to toggle source

Returns a flat hash where all nested keys are collapsed into a dast-separated string of keys.

hash = { person: { name: { first: 'Rob' }, age: '28' } }
hash.flatten_keys_to_html_attribute
=> { 'person-name-first' => "Rob", 'person-age'=>"28" }
# File lib/patches/core_ext/hash/flatten_keys.rb, line 30
def flatten_keys_to_html_attribute
  _flatten_keys(self, ->(*keys) { keys.join('-') })
end
flatten_keys_to_rails_param() click to toggle source

Returns a flat hash where all nested keys are collapsed into a string of keys fitting the Rails request param pattern.

hash = { person: { name: { first: 'Rob' }, age: '28' } }
hash.flatten_keys_to_rails_param
=> { 'person[name][first]' => "Rob", 'person[age]'=>"28" }
# File lib/patches/core_ext/hash/flatten_keys.rb, line 40
def flatten_keys_to_rails_param
  _flatten_keys(self, ->(*keys) { keys.map(&:to_s).reduce { |memo, key| memo + "[#{key}]" } })
end

Private Instance Methods

_flatten_keys(input, keypath_gen = ->(*keys) { keys } click to toggle source

refactored from stackoverflow.com/a/23861946/2884386

# File lib/patches/core_ext/hash/flatten_keys.rb, line 47
def _flatten_keys(input, keypath_gen = ->(*keys) { keys }, keys = [], output = {})
  if input.is_a? Hash
    input.each { |k, v| _flatten_keys(v, keypath_gen, keys + Array(k), output) }
  # elsif input.is_a? Array
  #   input.each_with_index { |v, i| _flatten_keys(v, keypath_gen, keys + Array(i), output) }
  else
    return output.merge!(keypath_gen.call(*keys) => input)
  end

  output
end