On Mar 1, 12:58 pm, Jeremy Evans <[email protected]> wrote:
> On Feb 28, 10:08 pm, David Lee <[email protected]> wrote:
>
> > How about splitting where and filter to do different things?
>
> > Both these would be the same:
>
> > dataset.filter {|o| o.a > 1}
> > dataset.where {a > 1}
>
> Nope. They are aliases now and I don't think it is a good idea to
> change that. What would be the justification for changing it? I
> understand not breaking compatibility for filter, but why would you
> want to break compatibility for where instead?
Because I thought you were open to breaking backwards-compatibility
for a nicer DSL, I proposed splitting #filter and #where so that we
can have both a backwards-compatible method and the newer method.
However, I think there's a better way.
>
> > You can also refer to local variables in #where by passing them as
> > arguments:
>
> > price = 1
> > name = "hotdog"
> > # The following two statements result in the SQL: "price > 1 AND name
> > = 'hotdog'"
> > dataset.filter {|o| (o.price > price) & (o.name == name)}
> > dataset.where(price, name) {|p,n| (price > p) & (name > n)}
>
> In your example, if I'm understanding it correctly, price and p in the
> block will be equal (same with name and n). With an instance_eval'd
> closure, you still have access to local variables in the lexical
> scope. You can force the VirtualRow instance method to be called
> instead of the local variable by:
>
> dataset.where{(price() > price) & (name() > name)}
>
> Jeremy
I did not know that local variables in the lexical scope were
available to a block that is instance_eval'd. Now knowing this, I
think the following behavior for #filter and #where would be best
(it's even backwards-compatible):
def p
1
end
n = 'hotdog'
dataset.filter(self) { (price > p) & (name == n) }
Basically, the block will be instance_eval'd on a VirtualRow and the
VirtualRow will first proxy any method to the variable passed into
#filter, and default to your proposed behavior when the argument does
not have the proxied method defined.
If you don't need to reference any methods, then you can just leave
out the argument and it will default to your proposed behavior.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"sequel-talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/sequel-talk?hl=en
-~----------~----~----~----~------~----~------~--~---