2012-11-23 15:13:49 +01:00
|
|
|
|
require 'virtus'
|
2014-03-14 13:08:48 +01:00
|
|
|
|
require 'currency'
|
2012-11-20 17:36:12 +01:00
|
|
|
|
|
2015-08-30 12:54:13 +02:00
|
|
|
|
class Quote
|
|
|
|
|
include Virtus.value_object
|
|
|
|
|
|
|
|
|
|
DEFAULT_BASE = 'EUR'
|
|
|
|
|
|
|
|
|
|
values do
|
|
|
|
|
attribute :base, String, default: DEFAULT_BASE
|
|
|
|
|
attribute :date, Date, default: Currency.current_date
|
|
|
|
|
end
|
2012-11-28 03:16:33 +01:00
|
|
|
|
|
2015-08-31 11:56:01 +02:00
|
|
|
|
def rates
|
|
|
|
|
@rates ||= find_rates
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def attributes
|
|
|
|
|
super.merge(rates: rates)
|
2012-11-20 17:36:12 +01:00
|
|
|
|
end
|
|
|
|
|
|
2015-08-31 11:56:01 +02:00
|
|
|
|
alias_method :to_h, :attributes
|
|
|
|
|
|
2012-11-23 15:13:49 +01:00
|
|
|
|
private
|
|
|
|
|
|
2015-08-31 11:56:01 +02:00
|
|
|
|
def base=(base)
|
|
|
|
|
base.upcase!
|
|
|
|
|
super base
|
|
|
|
|
end
|
|
|
|
|
|
2015-08-30 12:54:13 +02:00
|
|
|
|
def date=(date)
|
|
|
|
|
current_date = Currency.current_date_before(date)
|
2015-09-02 23:11:57 +02:00
|
|
|
|
fail ArgumentError, 'Date too old' unless current_date
|
2015-08-30 12:54:13 +02:00
|
|
|
|
|
|
|
|
|
super current_date
|
|
|
|
|
end
|
|
|
|
|
|
2015-08-31 11:56:01 +02:00
|
|
|
|
def find_rates
|
|
|
|
|
quoted_against_default_base? ? find_default_rates : find_rebased_rates
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def quoted_against_default_base?
|
|
|
|
|
base == DEFAULT_BASE
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def find_default_rates
|
2013-04-30 00:56:40 +02:00
|
|
|
|
Currency.where(date: date).reduce({}) do |rates, currency|
|
2015-08-31 11:56:01 +02:00
|
|
|
|
rates.update(currency.to_h)
|
2013-04-30 00:56:40 +02:00
|
|
|
|
end
|
2012-11-20 17:36:12 +01:00
|
|
|
|
end
|
|
|
|
|
|
2015-08-31 11:56:01 +02:00
|
|
|
|
def find_rebased_rates
|
|
|
|
|
rates = find_default_rates
|
|
|
|
|
denominator = rates.update(DEFAULT_BASE => 1.0).delete(base)
|
2015-09-02 23:11:57 +02:00
|
|
|
|
fail ArgumentError, 'Invalid base' unless denominator
|
2015-08-31 11:56:01 +02:00
|
|
|
|
rates.each do |iso_code, rate|
|
|
|
|
|
rates[iso_code] = round_rate(rate / denominator)
|
2012-11-21 13:31:17 +01:00
|
|
|
|
end
|
|
|
|
|
|
2012-11-23 15:13:49 +01:00
|
|
|
|
rates
|
2012-11-20 17:36:12 +01:00
|
|
|
|
end
|
|
|
|
|
|
2015-09-02 23:12:42 +02:00
|
|
|
|
# I'm mimicking the apparent convention of the ECB here.
|
2015-08-31 11:56:01 +02:00
|
|
|
|
def round_rate(rate)
|
2015-09-02 23:13:53 +02:00
|
|
|
|
Float("%.#{5}g" % rate)
|
2012-11-20 17:36:12 +01:00
|
|
|
|
end
|
|
|
|
|
end
|