Sha256: 7f102589beef38679dd6b764d0d246bfbcf0e64e699a61cd06ea943c3d8a5256

Contents?: true

Size: 750 Bytes

Versions: 3

Compression:

Stored size: 750 Bytes

Contents

module Chronicle
  module 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
          case value
          when Hash
            flatten_hash(value, current_key, result)
          when 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
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
chronicle-core-0.3.2 lib/chronicle/utils/hash_utils.rb
chronicle-core-0.3.1 lib/chronicle/utils/hash_utils.rb
chronicle-core-0.3.0 lib/chronicle/utils/hash_utils.rb