2018-03-08 02:05:19 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-10-03 15:48:30 +02:00
|
|
|
require 'day'
|
|
|
|
require 'forwardable'
|
|
|
|
|
|
|
|
class Currency < Sequel::Model(Day.currencies)
|
|
|
|
class << self
|
|
|
|
extend Forwardable
|
|
|
|
|
|
|
|
def_delegators :dataset, :latest, :between
|
|
|
|
end
|
|
|
|
|
2018-03-08 02:05:19 +01:00
|
|
|
dataset_module do
|
2018-10-03 15:48:30 +02:00
|
|
|
def only(*iso_codes)
|
|
|
|
where(iso_code: iso_codes)
|
2018-03-08 02:05:19 +01:00
|
|
|
end
|
2018-07-05 21:19:37 +02:00
|
|
|
|
2018-10-03 15:48:30 +02:00
|
|
|
def between(interval)
|
|
|
|
case interval.last - interval.first
|
2020-05-02 16:18:52 +02:00
|
|
|
when 366.. then super.sample('week')
|
2020-04-02 15:52:56 +02:00
|
|
|
else super
|
2018-07-05 21:19:37 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-10-03 15:48:30 +02:00
|
|
|
def sample(precision)
|
|
|
|
sampler = Sequel.function(:date_trunc, precision, :date)
|
|
|
|
|
|
|
|
select(:iso_code)
|
|
|
|
.select_append { avg(rate).as(rate) }
|
|
|
|
.select_append(sampler.as(:date))
|
|
|
|
.group(:iso_code, sampler)
|
|
|
|
.order(:date)
|
2018-07-05 21:19:37 +02:00
|
|
|
end
|
2018-03-08 02:05:19 +01:00
|
|
|
end
|
|
|
|
end
|