mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Make ActionDispatch::Request::Session#fetch behave like Hash#fetch
Session#fetch was mutating the session when given a default argument and/or a block. Since Session duck-types as a Hash, it should behave like one in these cases.
This commit is contained in:
parent
5853c64a4b
commit
d0fc760d09
2 changed files with 10 additions and 11 deletions
|
@ -7,6 +7,9 @@ module ActionDispatch
|
|||
ENV_SESSION_KEY = Rack::Session::Abstract::ENV_SESSION_KEY # :nodoc:
|
||||
ENV_SESSION_OPTIONS_KEY = Rack::Session::Abstract::ENV_SESSION_OPTIONS_KEY # :nodoc:
|
||||
|
||||
# Singleton object used to determine if an optional param wasn't specified
|
||||
Unspecified = Object.new
|
||||
|
||||
def self.create(store, env, default_options)
|
||||
session_was = find env
|
||||
session = Request::Session.new(store, env)
|
||||
|
@ -127,15 +130,12 @@ module ActionDispatch
|
|||
@delegate.delete key.to_s
|
||||
end
|
||||
|
||||
def fetch(key, default=nil)
|
||||
if self.key?(key)
|
||||
self[key]
|
||||
elsif default
|
||||
self[key] = default
|
||||
elsif block_given?
|
||||
self[key] = yield(key)
|
||||
def fetch(key, default=Unspecified, &block)
|
||||
load_for_read!
|
||||
if default == Unspecified
|
||||
@delegate.fetch(key.to_s, &block)
|
||||
else
|
||||
raise KeyError
|
||||
@delegate.fetch(key.to_s, default, &block)
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -68,13 +68,12 @@ module ActionDispatch
|
|||
assert_equal '1', session.fetch(:one)
|
||||
|
||||
assert_equal '2', session.fetch(:two, '2')
|
||||
assert_equal '2', session.fetch(:two)
|
||||
assert_nil session.fetch(:two, nil)
|
||||
|
||||
assert_equal 'three', session.fetch(:three) {|el| el.to_s }
|
||||
assert_equal 'three', session.fetch(:three)
|
||||
|
||||
assert_raise KeyError do
|
||||
session.fetch(:four)
|
||||
session.fetch(:three)
|
||||
end
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in a new issue