Permalink
Browse files

CookieJar is enumerable. fixes #2795

  • Loading branch information...
1 parent 8b049a1 commit af7c9b9ceffecc54588b65582c27546eb76d5721 @tenderlove tenderlove committed Sep 1, 2011
View
2 actionpack/CHANGELOG
@@ -1,5 +1,7 @@
*Rails 3.1.1 (unreleased)*
+* CookieJar is now Enumerable. Fixes #2795
+
* Fixed AssetNotPrecompiled error raised when rake assets:precompile is compiling certain .erb files. [Guillermo Iguaran]
* Manifest is correctly placed in assets path when default assets prefix is changed. [Guillermo Iguaran]
View
5 actionpack/lib/action_dispatch/middleware/cookies.rb
@@ -84,6 +84,7 @@ class Cookies
class CookieOverflow < StandardError; end
class CookieJar #:nodoc:
+ include Enumerable
# This regular expression is used to split the levels of a domain.
# The top level domain can be any string without a period or
@@ -123,6 +124,10 @@ def initialize(secret = nil, host = nil, secure = false)
alias :closed? :closed
def close!; @closed = true end
+ def each(&block)
+ @cookies.each(&block)
+ end
+
# Returns the value of the cookie by +name+, or +nil+ if no such cookie exists.
def [](name)
@cookies[name.to_s]
View
16 actionpack/test/dispatch/cookies_test.rb
@@ -148,6 +148,22 @@ def setup
@request.host = "www.nextangle.com"
end
+ def test_each
+ request.cookie_jar['foo'] = :bar
+ list = []
+ request.cookie_jar.each do |k,v|
+ list << [k, v]
+ end
+
+ assert_equal [['foo', :bar]], list
+ end
+
+ def test_enumerable
+ request.cookie_jar['foo'] = :bar
+ actual = request.cookie_jar.map { |k,v| [k.to_s, v.to_s] }
+ assert_equal [['foo', 'bar']], actual
+ end
+
def test_key_methods
assert !request.cookie_jar.key?(:foo)
assert !request.cookie_jar.has_key?("foo")

0 comments on commit af7c9b9

Please sign in to comment.