Merge pull request #32446 from sinsoku/add_dig_to_session
Add #dig to ActionDispatch::Request::Session
This commit is contained in:
commit
8d62ff0080
|
@ -93,6 +93,14 @@ module ActionDispatch
|
|||
@delegate[key.to_s]
|
||||
end
|
||||
|
||||
# Returns the nested value specified by the sequence of key, returning
|
||||
# nil if any intermediate step is nil.
|
||||
def dig(*keys)
|
||||
load_for_read!
|
||||
keys = keys.map.with_index { |key, i| i.zero? ? key.to_s : key }
|
||||
@delegate.dig(*keys)
|
||||
end
|
||||
|
||||
# Returns true if the session has the given key or false.
|
||||
def has_key?(key)
|
||||
load_for_read!
|
||||
|
|
|
@ -118,6 +118,18 @@ module ActionDispatch
|
|||
end
|
||||
end
|
||||
|
||||
def test_dig
|
||||
session = Session.create(store, req, {})
|
||||
session["one"] = { "two" => "3" }
|
||||
|
||||
assert_equal "3", session.dig("one", "two")
|
||||
assert_equal "3", session.dig(:one, "two")
|
||||
|
||||
assert_nil session.dig("three", "two")
|
||||
assert_nil session.dig("one", "three")
|
||||
assert_nil session.dig("one", :two)
|
||||
end
|
||||
|
||||
private
|
||||
def store
|
||||
Class.new {
|
||||
|
|
Loading…
Reference in New Issue