class Morfo::Tools::FlattenHashKeys

Attributes

input_hash[R]

Public Class Methods

new(input_hash) click to toggle source
# File lib/morfo/tools.rb, line 23
def initialize(input_hash)
  @input_hash = input_hash.dup.freeze
end

Public Instance Methods

flatten() click to toggle source
# File lib/morfo/tools.rb, line 27
def flatten
  input_hash.inject({}) do |result_hash, (key, value)|
    inner_hash = false
    if value.is_a?(::Hash)
      inner_hash = true
      value.each do |inner_key, inner_value|
        if inner_value.is_a?(::Hash)
          inner_hash = true
        end
        result_hash.merge!("#{key}.#{inner_key}".to_sym => inner_value)
      end
    else
      result_hash.merge!(key.to_sym => value)
    end

    if inner_hash
      FlattenHashKeys.new(result_hash).flatten
    else
      result_hash
    end
  end
end