On Tuesday, February 7, 2017 at 1:07:59 AM UTC-8, Satyanarayana Gandham
wrote:
>
> Hi Jeremy,
>
> Thanks for the reply. Is there any way to define things like this in a
> common place? I meant to ask, Can we define a method that contains this
> case statement and call it inside the virtual row block?
>
> Something like this:
> Sequel.define_method_in_row_block(:day_of_week) do |column|
> case DB.database_type
> when :mysql
> # ...
> when :postgres
> # ...
> when :sqlite
> # ...
> end
>
> Then, we can simply call "Event.where{day_of_week(starts_at) =~ 1}" and
> make our code cleaner. That would be really helpful.
>
This is ruby, you can define such a method on any object you want
(Sequel::VirtualRow in your example). If I were to define such a method, I
would do so on the Database object, so you could do:
Event.where(DB.day_of_week(starts_at) => 1)
One benefit of this approach is that you can use a case statement to setup
different methods per database:
case DB.database_type
when :mysql
def DB.day_of_week(date)
end
when :postgres
# ...
This makes such methods faster to call at runtime.
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 post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.