Class: CoinSync::CurrencyConverters::Cache
- Inherits:
-
Object
- Object
- CoinSync::CurrencyConverters::Cache
- Defined in:
- lib/coinsync/currency_converters/cache.rb
Instance Method Summary collapse
- #[](from, to, date) ⇒ Object
- #[]=(from, to, date, amount) ⇒ Object
-
#initialize(name) ⇒ Cache
constructor
A new instance of Cache.
- #save ⇒ Object
Constructor Details
#initialize(name) ⇒ Cache
Returns a new instance of Cache.
7 8 9 10 11 12 13 14 15 16 |
# File 'lib/coinsync/currency_converters/cache.rb', line 7 def initialize(name) @name = name @filename = "cache/#{name}.json" if File.exist?(@filename) @rates = JSON.parse(File.read(@filename)) else @rates = {} end end |
Instance Method Details
#[](from, to, date) ⇒ Object
18 19 20 21 |
# File 'lib/coinsync/currency_converters/cache.rb', line 18 def [](from, to, date) @rates["#{from.code}:#{to.code}"] ||= {} @rates["#{from.code}:#{to.code}"][date.to_s] end |
#[]=(from, to, date, amount) ⇒ Object
23 24 25 26 |
# File 'lib/coinsync/currency_converters/cache.rb', line 23 def []=(from, to, date, amount) @rates["#{from.code}:#{to.code}"] ||= {} @rates["#{from.code}:#{to.code}"][date.to_s] = amount end |
#save ⇒ Object
28 29 30 31 |
# File 'lib/coinsync/currency_converters/cache.rb', line 28 def save FileUtils.mkdir_p(File.dirname(@filename)) File.write(@filename, JSON.generate(@rates)) end |