Skip to content

Commit

Permalink
Pulled in the indifferent_params method from Sinatra so that params c…
Browse files Browse the repository at this point in the history
…an be indexed with Symbols
  • Loading branch information
stevemohapibanks committed Jun 21, 2011
1 parent b947512 commit 49ab8c7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
16 changes: 15 additions & 1 deletion lib/goliath/rack/params.rb
Expand Up @@ -58,8 +58,22 @@ def retrieve_params(env)
params.merge!(post_params)
end

params
indifferent_params(params)
end

def indifferent_params(params)
params = indifferent_hash.merge(params)
params.each do |key, value|
next unless value.is_a?(Hash)
params[key] = indifferent_params(value)
end
end

# Creates a Hash with indifferent access.
def indifferent_hash
Hash.new {|hash,key| hash[key.to_s] if Symbol === key }
end

end
end
end
6 changes: 6 additions & 0 deletions spec/unit/rack/params_spec.rb
Expand Up @@ -19,6 +19,7 @@

ret = @params.retrieve_params(@env)
ret['foo'].should == 'bar'
ret[:foo].should == 'bar'
ret['baz'].should == 'bonkey'
end

Expand All @@ -38,6 +39,8 @@
ret['foo'].should == 'bar'
ret['baz'].should == 'bonkey'
ret['zonk'].should == {'donk' => 'monk'}
ret[:zonk].should == {'donk' => 'monk'}
ret[:zonk][:donk].should == 'monk'
end

it 'parses arrays of data' do
Expand All @@ -47,6 +50,7 @@
ret['foo'].is_a?(Array).should be_true
ret['foo'].length.should == 3
ret['foo'].should == %w(bar baz foos)
ret[:foo].should == %w(bar baz foos)
end

it 'parses multipart data' do
Expand All @@ -67,6 +71,7 @@

ret = @params.retrieve_params(@env)
ret['submit-name'].should == 'Larry'
ret[:"submit-name"].should == 'Larry'
ret['submit-name-with-content'].should == 'Berry'
end

Expand Down Expand Up @@ -140,6 +145,7 @@

ret = @params.retrieve_params(@env)
ret['foo'].should == 'bar'
ret[:foo].should == 'bar'
end

it "handles empty input gracefully on JSON" do
Expand Down

0 comments on commit 49ab8c7

Please sign in to comment.