New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add session#fetch #12692
Add session#fetch #12692
Conversation
@@ -127,6 +127,18 @@ def delete(key) | |||
@delegate.delete key.to_s | |||
end | |||
|
|||
def fetch(key, default=nil) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's also add some rDoc to explain the usage and maybe refer to Hash#fetch
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just saw that saw that Session
is :nodoc:
... wondering why that is...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would definitely do that.
But since no other method in this class has documentation, I would think it might not be the best place.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like all the rDoc related to sessions is currently stored in base.rb
. @fxn where should such methods be documented? As Session
is :nodoc
it looks like it's an implementation detail and not part of the public API. It's a bit sad that this way you won't be able to find the docs using the "Search" feature of api.rubyonrails.org:
Looks fine for me 👍 iirc, some core members are a bit skeptic about add new methods to params/session hashes but this shouldn't be te case since we have |
AFAIK, >> h = {}
=> {}
>> h.fetch(23) { :yo }
=> :yo
>> h.fetch(23)
KeyError: key not found: 23
from (irb):25:in `fetch'
from (irb):25
from /home/ps/.rvm/rubies/ruby-2.0.0-p195/bin/irb:16:in `<main>' Fetching a missing key in >> s = ActionDispatch::Request::Session.create(store, {}, {})
=> #<ActionDispatch::Request::Session:0x1976b88 not yet loaded>
>> s.fetch(23) { :yo }
=> :yo
>> s.fetch(23)
=> :yo The second call should raise a The implementation of |
Actually, I did this on purpose, as I would think this could lead to unexpected behaviors.
In this case, the saved value would be expected to be saved in the session. I agree I missed a test for this though. I can happily add it, or change the current behavior. |
I'm with @splattael on this one. The behaviour you implement might have very valid use cases (but the example you give here isn't one of them and seems artificial). But it is not okay if the feature is advertised as "I've made this fetch behave like Hash#fetch" and then it doesn't live up to this promise (and moreover has serious side-effects). On a side note, I strongly disagree with this opinion:
This is false on so many levels 😔 |
@dmathieu I agree with both about the different behavior between Hash#fetch and Session#fetch, but I also agree this is a nice thing. So I suggest to remove the sentence "fetch behaves like Hash#fetch." from changelog since it doesn't behave. |
@splattael, @rrrene: I just pushed a fix to the concern you raised, which has just been merged. Thanks @rafaelfranca 💛 |
This adds a
#fetch
method to thesession
object.I've made this fetch behave like Hash#fetch, which means it takes either a default value, a block or no value.