Thanks Jeremy!

Thanks for the example, I wrote that with the assumption I was 
monkey-patching Dataset directly but that's probably a better approach to 
use a plugin.


On Wednesday, July 24, 2019 at 3:10:48 PM UTC-7, Jeremy Evans wrote:
>
> On Wednesday, July 24, 2019 at 2:14:44 PM UTC-7, Karl He wrote:
>>
>> Bit of a weird situation here.
>>
>> So I have a dataset that I want to be able to pass around everywhere 
>> treated normally, except that I have a `WHERE
>> ` and `GROUP BY` I want to always apply to it before any rows are 
>> returned.
>>
>> I can't just do `ds.group(:a, :b)` and pass that around, because any 
>> conditions or joins that would be called on it in the meantime would be 
>> applied in the wrong order.
>>
>> So I basically want something similar to how `row_proc` works, except 
>> it's more of a `dataset_proc`.
>>
>> It seems to me like the most straightforward way to accomplish this would 
>> be modifying `each`:
>>
>> module Sequel
>>   class Dataset
>>     def with_dataset_proc(ds_proc)
>>       @opts[:dataset_proc] = ds_proc
>>     end
>>
>>     alias_method :each, :_each
>>
>>     def each
>>       @opts[:dataset_proc].call(self)._each
>>     end
>>   end
>> end
>>
>> ds = ds.with_dataset_proc(Proc.new(|ds| ds.where(:x => 1).group(:a, :b))
>>
>> Not sure if that's valid code above, but hopefully gets the idea across.
>>
>> Is this a good way to go about it, or is there perhaps a more elegant 
>> solution, or something already existing that does the same thing?
>>
>
> I'm not sure if there is something that does the same thing.  Your 
> solution seems fine, though in general Sequel's approach would be to 
> override each and call super (see the eager_each plugin for an example). 
>
> Thanks,
> 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 view this discussion on the web visit 
https://groups.google.com/d/msgid/sequel-talk/6b462e0e-a63e-4c03-b79f-10bfa2b943b8%40googlegroups.com.

Reply via email to