Sha256: bee87a07b12a23ccad529df698ee0fa79965fb1d2c3f6eaeaeca9677b2287b5f

Contents?: true

Size: 652 Bytes

Versions: 181

Compression:

Stored size: 652 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

181 entries across 181 versions & 4 rubygems

Version Path
jets-1.1.0 lib/jets/mega/hash_converter.rb