Sha256: cfba352cf76f3398136f1e81971476c943b1265f2ba4259096fb285a5454efbd

Contents?: true

Size: 698 Bytes

Versions: 1

Compression:

Stored size: 698 Bytes

Contents

module Chronicle::Utils
  module HashUtils
    def self.flatten_hash(hash, parent_key = '', result = {})
      hash.each do |key, value|
        current_key = parent_key + (parent_key.empty? ? '' : '.') + key.to_s
        if value.is_a?(Hash)
          flatten_hash(value, current_key, result)
        elsif value.is_a?(Array)
          value.each_with_index do |item, index|
            if item.is_a?(Hash)
              flatten_hash(item, "#{current_key}[#{index}]", result)
            else
              result["#{current_key}[#{index}]".to_sym] = item
            end
          end
        else
          result[current_key.to_sym] = value
        end
      end
      result
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
chronicle-core-0.2.2 lib/chronicle/utils/hash_utils.rb