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 a variation of AbstractBlock#find_by that returns a single result #3135
Comments
I don't know if it's idiomatic Ruby but I always make a clear distinction between methods that return a collection and methods that return an (optional) single result: def get_by # Option[Node] def find_by # List[Node]
JPA is throwing exceptions:
I think it's more idiomatic in Ruby to return
I'm leaning toward "enforce" because the user specifically asked for a single result so if there is more than one result it's "unexpected". And we definitely don't want to hide it under the carpet and pretend there is only one result.
In my opinion this method has low value.
I don't like this implicit because it changes the "return type". |
In this case, I can't really comment on what's idiomatic Ruby or not. I'm more interested in what's intuitive. Am I correct in saying that you're suggesting the following signature for the method that returns and enforces a single result? def get_by selector, &block # Node That seems reasonable to me. I'm still wondering if we should have something that parallels |
Yes
The good thing is that we don't have to deal with the unexpected case (ie. when document.querySelector('p') === document.querySelectorAll('p')[0]
document.querySelector('h5') // null
document.querySelectorAll('h5')[0] // undefined I guess it depends on what we want to provide. |
in Ruby Enumerable module find_all with alias select is used for Array doc.find{| doc| doc.id} doc.select{|d| d.selector('p')} |
doc.find{|d| doc.id == '#term'} doc.select{|d| d.selector == 'p'} doc.select_with_query('p') doc.find_with_query('p') |
…ock that matches criteria
I really like the I don't think we need to throw an exception if more than one result is found. That just makes the method have to do more work than it needs to. The user can always find out if too many results are being returned by using This is by no means a perfect API. We are trying to adapt it to existing code as best we can. In a future version, we could always consider mapping this functionality to new method names. |
When using find_by with an ID selector, or another unique characteristic, it seems odd that the method returns a collection instead of a single result. It should be possible to use the method in "singular" mode.
One idea is to pass the option
:single_result
.When the ID selector is specified,
single_result: true
would be implied:The only question is whether
single_result
should enforce that there's only one match, or whether it should simply pick off the first result in the collection.We could follow the JPA approach and have
single_result
enforce and return a single result (or nil), andfirst_result
return the first result in the collection.The other option is to introduce separate methods to handle the result filtering:
find_one_by
- enforces and returns a single result or nilfind_first_by
- returns the first result onlyThe text was updated successfully, but these errors were encountered: