mirror of
https://github.com/hakanensari/frankfurter.git
synced 2024-11-25 04:22:28 +01:00
Refactor date lookup
This commit is contained in:
parent
7c99f214e6
commit
6faa265834
25
lib/day.rb
25
lib/day.rb
@ -3,23 +3,17 @@
|
|||||||
class Day < Sequel::Model
|
class Day < Sequel::Model
|
||||||
dataset_module do
|
dataset_module do
|
||||||
def latest(date = Date.today)
|
def latest(date = Date.today)
|
||||||
where(date: select(:date).where(Sequel[:date] <= date)
|
where(date: _nearest_date_with_rates(date))
|
||||||
.order(Sequel.desc(:date))
|
|
||||||
.limit(1))
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Returns rates for a given date interval
|
|
||||||
#
|
|
||||||
# If the start date falls on a holiday/weekend, rates start from the closest preceding business day.
|
|
||||||
def between(interval)
|
def between(interval)
|
||||||
return where(false) if interval.begin > Date.today
|
return where(false) if interval.begin > Date.today
|
||||||
|
|
||||||
previous_date = select(:date)
|
where(Sequel.expr(:date) >= Sequel.function(
|
||||||
.where(Sequel[:date] <= interval.begin)
|
:coalesce,
|
||||||
.order(Sequel.desc(:date))
|
_nearest_date_with_rates(interval.begin),
|
||||||
.limit(1)
|
interval.begin,
|
||||||
|
))
|
||||||
where(Sequel.expr(:date) >= Sequel.function(:coalesce, previous_date, interval.begin))
|
|
||||||
.where(Sequel.expr(:date) <= interval.end)
|
.where(Sequel.expr(:date) <= interval.end)
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -31,5 +25,12 @@ class Day < Sequel::Model
|
|||||||
)
|
)
|
||||||
.join(Sequel.function(:jsonb_each, :rates).lateral.as(:rates), true)
|
.join(Sequel.function(:jsonb_each, :rates).lateral.as(:rates), true)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def _nearest_date_with_rates(date)
|
||||||
|
select(:date)
|
||||||
|
.where(Sequel[:date] <= date)
|
||||||
|
.order(Sequel.desc(:date))
|
||||||
|
.limit(1)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user