Sha256: 64fda19ca48c7e39505dc4f868bac08a29d2fbf0e4309f63f4472342f1bdccf2

Contents?: true

Size: 651 Bytes

Versions: 25

Compression:

Stored size: 651 Bytes

Contents

# Thanks https://mensfeld.pl/2012/01/converting-nested-hash-into-http-url-params-hash-version-in-ruby/
module Jets::Mega
  module HashConverter
    def self.encode(value, key = nil, out_hash = {})
      case value
      when Hash  then
        value.each { |k,v| encode(v, append_key(key,k), out_hash) }
        out_hash
      when Array then
        value.each { |v| encode(v, "#{key}", out_hash) }
        out_hash
      when nil   then ''
      else
        out_hash[key] = value
        out_hash
      end
    end

    private

    def self.append_key(root_key, key)
      root_key.nil? ? :"#{key}" : :"#{root_key}[#{key.to_s}]"
    end
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
jets-4.0.7 lib/jets/mega/hash_converter.rb
jets-4.0.6 lib/jets/mega/hash_converter.rb
jets-4.0.5 lib/jets/mega/hash_converter.rb
jets-4.0.4 lib/jets/mega/hash_converter.rb
jets-4.0.3 lib/jets/mega/hash_converter.rb