2018-10-03 15:48:30 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class Day < Sequel::Model
|
|
|
|
dataset_module do
|
|
|
|
def latest(date = Date.today)
|
2024-11-20 14:22:08 +01:00
|
|
|
where(date: _nearest_date_with_rates(date))
|
2018-10-03 15:48:30 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def between(interval)
|
2024-11-20 14:13:17 +01:00
|
|
|
return where(false) if interval.begin > Date.today
|
|
|
|
|
2024-11-20 14:22:08 +01:00
|
|
|
where(Sequel.expr(:date) >= Sequel.function(
|
|
|
|
:coalesce,
|
|
|
|
_nearest_date_with_rates(interval.begin),
|
|
|
|
interval.begin,
|
|
|
|
))
|
2024-11-20 14:13:17 +01:00
|
|
|
.where(Sequel.expr(:date) <= interval.end)
|
2018-10-03 15:48:30 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def currencies
|
2024-11-19 20:06:47 +01:00
|
|
|
select(
|
|
|
|
:date,
|
|
|
|
Sequel.lit("rates.key").as(:iso_code),
|
|
|
|
Sequel.lit("rates.value::text::float").as(:rate),
|
|
|
|
)
|
2018-10-03 15:48:30 +02:00
|
|
|
.join(Sequel.function(:jsonb_each, :rates).lateral.as(:rates), true)
|
2024-11-20 14:58:17 +01:00
|
|
|
.order(Sequel.asc(:date), Sequel.asc(Sequel.lit("rates.key")))
|
2018-10-03 15:48:30 +02:00
|
|
|
end
|
2024-11-20 14:22:08 +01:00
|
|
|
|
|
|
|
def _nearest_date_with_rates(date)
|
|
|
|
select(:date)
|
|
|
|
.where(Sequel[:date] <= date)
|
|
|
|
.order(Sequel.desc(:date))
|
|
|
|
.limit(1)
|
|
|
|
end
|
2018-10-03 15:48:30 +02:00
|
|
|
end
|
|
|
|
end
|