Class: MoneyExchange::Store
- Inherits:
-
Money::RatesStore::Memory
- Object
- Money::RatesStore::Memory
- MoneyExchange::Store
- Defined in:
- lib/money_exchange/store.rb
Constant Summary collapse
- CACHE_KEY =
'MoneyExchange::Store/cache'.freeze
Instance Method Summary collapse
- #get_rate(iso_from, iso_to) ⇒ Object
-
#initialize(opts = {}) ⇒ Store
constructor
A new instance of Store.
Constructor Details
#initialize(opts = {}) ⇒ Store
Returns a new instance of Store.
7 8 9 10 11 12 13 14 15 |
# File 'lib/money_exchange/store.rb', line 7 def initialize(opts = {}) super @client = opts[:client] @cache_store = opts[:cache_store] || Cache::NullStore.new @ttl = opts[:ttl] || 24 * 60 * 60 @loaded_at = nil @iso_base = nil end |
Instance Method Details
#get_rate(iso_from, iso_to) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/money_exchange/store.rb', line 17 def get_rate(iso_from, iso_to) load_rates! if stale? super || begin rate = if iso_from == @iso_base nil elsif inverse_rate = super(iso_to, iso_from) 1.0 / inverse_rate elsif iso_to == @iso_base nil else get_rate(iso_from, @iso_base) * get_rate(@iso_base, iso_to) end add_rate(iso_from, iso_to, rate) end end |