Skip to content

Instantly share code, notes, and snippets.

@imanel
Created December 24, 2013 15:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save imanel/087da70cd147e69d755a to your computer and use it in GitHub Desktop.
Save imanel/087da70cd147e69d755a to your computer and use it in GitHub Desktop.
Test for deep_munge pull requests
Output for rails_master.rb
Input | Output
{:person=>nil} | {:person=>nil}
{:person=>[]} | {:person=>nil}
{:person=>[]} | {:person=>nil}
{:person=>[]} | {:person=>nil}
{:person=>["foo"]} | {:person=>["foo"]}
Output for pull_11044.rb
Input | Output
{:person=>nil} | {:person=>nil}
{:person=>[]} | {:person=>[]}
{:person=>[]} | {:person=>nil}
{:person=>[]} | {:person=>nil}
{:person=>["foo"]} | {:person=>["foo"]}
Output for pull_12251.rb
Input | Output
{:person=>nil} | {:person=>nil}
{:person=>[]} | {:person=>[]}
{:person=>[]} | {:person=>[]}
{:person=>[]} | {:person=>[]}
{:person=>["foo"]} | {:person=>["foo"]}
module ActionDispatch
class Request < Rack::Request
class Utils # :nodoc:
class << self
# Remove nils from the params hash
def deep_munge(hash)
hash.each do |k, v|
case v
when Array
v.grep(Hash) { |x| deep_munge(x) }
if v.empty?
hash[k] = []
else
v.compact!
hash[k] = nil if v.empty?
end
when Hash
deep_munge(v)
end
end
hash
end
end
end
end
end
module ActionDispatch
class Request < Rack::Request
class Utils # :nodoc:
class << self
# Remove nils from the params hash
def deep_munge(hash)
hash.each do |k, v|
case v
when Array
v.grep(Hash) { |x| deep_munge(x) }
v.compact!
when Hash
deep_munge(v)
end
end
hash
end
end
end
end
end
module ActionDispatch
class Request < Rack::Request
class Utils # :nodoc:
class << self
# Remove nils from the params hash
def deep_munge(hash)
hash.each do |k, v|
case v
when Array
v.grep(Hash) { |x| deep_munge(x) }
v.compact!
hash[k] = nil if v.empty?
when Hash
deep_munge(v)
end
end
hash
end
end
end
end
end
#!/usr/bin/env ruby
# USAGE
# $ ruby tester.rb filename
require 'rack/request'
require "./#{ARGV.first}"
EXAMPLES = [
{ person: nil },
{ person: [] },
{ person: [nil] },
{ person: [nil, nil, nil] },
{ person: ["foo", nil] }
]
# puts ActionDispatch::Request::Utils.deep_munge(EXAMPLES.first).inspect
separator = " | "
column_size = 20
puts "Output for #{ARGV.first}"
puts "Input".ljust(column_size) + separator + "Output"
EXAMPLES.each do |input|
output = ActionDispatch::Request::Utils.deep_munge(input.dup)
puts input.inspect.ljust(column_size) + separator + output.inspect
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment