frankfurter/lib/currency.rb
Hakan Ensari e5815737c1
Spring cleaning
- bumped gems
- rm bots
- rm pry byebug
- added rubocop-shopify and corrected generated warnings
2024-11-20 14:14:27 +01:00

36 lines
720 B
Ruby

# frozen_string_literal: true
require "day"
require "forwardable"
class Currency < Sequel::Model(Day.currencies)
class << self
extend Forwardable
def_delegators :dataset, :latest, :between
end
dataset_module do
def only(*iso_codes)
where(iso_code: iso_codes)
end
def between(interval)
case interval.last - interval.first
when 366.. then super.sample("week")
else super
end
end
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)
end
end
end