On Tuesday, March 12, 2013 6:48:09 PM UTC-7, Jeremy Evans wrote:
> On Tuesday, March 12, 2013 5:41:10 PM UTC-7, Michael Lang wrote:
>
>> whiny_find(*args)
>>
>> :-D
>>
>> What about sticking with find, but pass an argument, say ":except =>
>> true" in when we want the behavior to raise an exception instead of
>> returning nil?
>>
>
> We could add the ability for .find to take an options hash, but I think a
> new method is a better API. Another issue with using .find is that you
> cannot pass a primary key value to it, unlike .[]. So any method name with
> find in it is probably out.
>
> I definitely want the method name to not be verbose. The best method name
> I can think of at this point is lookup:
>
> Model.lookup(1)
> Model.lookup(:bar=>'foo')
>
> I'm certainly open to better suggestions.
>
Another thing I just thought of is to add two methods instead of one. This
makes naming a non-issue. We could have first! for finding the first
record via an expression, and with_pk! for finding the first record by
primary key:
Model.with_pk!(1)
Model.first!(:bar=>'foo')
Since first and with_pk already exist, this should make things easier for
Sequel users. Another good thing about this is that first is a dataset
method, and with_pk is a model dataset method, so you could use these with
method chaining:
Model.where(:bar=>'foo').with_pk!(1)
Model.where(:bar=>'foo').first!
Model.where(:bar=>'foo').first!(:baz=>'quux')
Model.where(:bar=>'foo').first!{baz > 5}
The only issue I see is first can also be used as a limiter:
Model.first(5) # array of first five records
How should .first! handle something like that? Same result, or raise an
Error?
Jeremy
--
You received this message because you are subscribed to the Google Groups
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sequel-talk?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.