Skip to content
Browse files

modify cookie[]= to take a hash of extra options

Using the syntax `cookie[:key] = 'value', {:options => 'hash'}` will now
merge the provided hash with the existing `@options` hash (which allows
for chainging cookie options on a per-cookie basis).
  • Loading branch information...
1 parent 1107072 commit 8371361303a4b59d47e828a41175f57c6d4f9c3b @kylewlacy committed Feb 26, 2013
Showing with 9 additions and 1 deletion.
  1. +2 −1 lib/sinatra/cookies.rb
  2. +7 −0 spec/cookies_spec.rb
View
3 lib/sinatra/cookies.rb
@@ -88,7 +88,8 @@ def [](key)
end
def []=(key, value)
- @response.set_cookie key.to_s, @options.merge(:value => value)
+ options = (value.is_a?(Array) && value[1].is_a?(Hash)) ? value[1] : {}
+ @response.set_cookie key.to_s, @options.merge(options).merge(:value => value)
end
def assoc(key)
View
7 spec/cookies_spec.rb
@@ -124,6 +124,13 @@ def cookies(*set_cookies)
cookie_jar['foo'].should be == 'bar'
end
+ it 'sets a cookie with extra options' do
+ cookie_route do
+ cookies['foo'] = 'bar', {:path => '/baz'}
+ response['Set-Cookie'].lines.detect { |l| l.start_with? 'foo=' }
+ end.should include('path=/baz')
+ end
+
it 'adds a value to the cookies hash' do
cookie_route do
cookies['foo'] = 'bar'

0 comments on commit 8371361

Please sign in to comment.
Something went wrong with that request. Please try again.