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:13:17 +01:00
|
|
|
where(date: select(:date).where(Sequel[:date] <= date)
|
2018-10-03 15:48:30 +02:00
|
|
|
.order(Sequel.desc(:date))
|
|
|
|
.limit(1))
|
|
|
|
end
|
|
|
|
|
2024-11-20 14:13:17 +01:00
|
|
|
# Returns rates for a given date interval
|
|
|
|
#
|
|
|
|
# If the start date falls on a holiday/weekend, rates start from the closest preceding business day.
|
2018-10-03 15:48:30 +02:00
|
|
|
def between(interval)
|
2024-11-20 14:13:17 +01:00
|
|
|
return where(false) if interval.begin > Date.today
|
|
|
|
|
|
|
|
previous_date = select(:date)
|
|
|
|
.where(Sequel[:date] <= interval.begin)
|
|
|
|
.order(Sequel.desc(:date))
|
|
|
|
.limit(1)
|
|
|
|
|
|
|
|
where(Sequel.expr(:date) >= Sequel.function(:coalesce, previous_date, interval.begin))
|
|
|
|
.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)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|