Class: MoneyWithDate::Bank::VariableExchange

Inherits:
Money::Bank::VariableExchange
  • Object
show all
Defined in:
lib/money_with_date/bank/variable_exchange.rb

Instance Method Summary collapse

Constructor Details

#initialize(store = MoneyWithDate::RatesStore::Memory.new, &block) ⇒ VariableExchange

Returns a new instance of VariableExchange.



6
7
8
# File 'lib/money_with_date/bank/variable_exchange.rb', line 6

def initialize(store = MoneyWithDate::RatesStore::Memory.new, &block)
  super
end

Instance Method Details

#add_rate(from, to, rate, date) ⇒ Object

rubocop:enable all



30
31
32
# File 'lib/money_with_date/bank/variable_exchange.rb', line 30

def add_rate(from, to, rate, date)
  set_rate(from, to, rate, date)
end

#exchange_with(from, to_currency, &block) ⇒ Object

rubocop:disable all



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/money_with_date/bank/variable_exchange.rb', line 11

def exchange_with(from, to_currency, &block)
  to_currency = ::Money::Currency.wrap(to_currency)
  if from.currency == to_currency
    from
  else
    if (rate = get_rate(from.currency, to_currency, from.date))
      fractional = calculate_fractional(from, to_currency)
      from.dup_with(
        fractional: exchange(fractional, rate, &block),
        currency: to_currency,
        bank: self
      )
    else
      raise ::Money::Bank::UnknownRate, "No conversion rate known for '#{from.currency.iso_code}' -> '#{to_currency}' on date #{from.date}"
    end
  end
end

#get_rate(from, to, date) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/money_with_date/bank/variable_exchange.rb', line 44

def get_rate(from, to, date)
  if store.method(:get_rate).parameters.size == 2
    store.get_rate(::Money::Currency.wrap(from).iso_code, ::Money::Currency.wrap(to).iso_code)
  else
    store.get_rate(::Money::Currency.wrap(from).iso_code, ::Money::Currency.wrap(to).iso_code,
                   ::Money.parse_date(date))
  end
end

#import_rates(format, s, opts = {}) ⇒ Object

rubocop:disable all

Raises:

  • (Money::Bank::UnknownRateFormat)


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/money_with_date/bank/variable_exchange.rb', line 63

def import_rates(format, s, opts = {}) # rubocop:disable all
  return super if store.method(:add_rate).parameters.size == 3

  raise Money::Bank::UnknownRateFormat unless RATE_FORMATS.include?(format)

  if format == :ruby
    warn "[WARNING] Using :ruby format when importing rates is potentially unsafe and " \
         "might lead to remote code execution via Marshal.load deserializer. Consider using " \
         "safe alternatives such as :json and :yaml."
  end

  store.transaction do
    data = FORMAT_SERIALIZERS[format].load(s)

    data.each do |date, rates|
      rates.each do |key, rate|
        from, to = key.split(SERIALIZER_SEPARATOR)
        add_rate(from, to, rate, date)
      end
    end
  end

  self
end

#ratesObject

rubocop:enable Metrics/AbcSize



54
55
56
57
58
59
60
61
# File 'lib/money_with_date/bank/variable_exchange.rb', line 54

def rates
  return super if store.method(:get_rate).parameters.size == 2

  store.each_rate.with_object({}) do |(from, to, rate, date), hash|
    hash[date.to_s] ||= {}
    hash[date.to_s][[from, to].join(SERIALIZER_SEPARATOR)] = rate
  end
end

#set_rate(from, to, rate, date) ⇒ Object

rubocop:disable Metrics/AbcSize



35
36
37
38
39
40
41
42
# File 'lib/money_with_date/bank/variable_exchange.rb', line 35

def set_rate(from, to, rate, date)
  if store.method(:add_rate).parameters.size == 3
    store.add_rate(::Money::Currency.wrap(from).iso_code, ::Money::Currency.wrap(to).iso_code, rate)
  else
    store.add_rate(::Money::Currency.wrap(from).iso_code, ::Money::Currency.wrap(to).iso_code, rate,
                   ::Money.parse_date(date))
  end
end