Class: CoinSync::PriceLoaders::Cache
- Inherits:
-
Object
- Object
- CoinSync::PriceLoaders::Cache
- Defined in:
- lib/coinsync/price_loaders/cache.rb
Instance Method Summary collapse
- #[](coin, time) ⇒ Object
- #[]=(coin, time, price) ⇒ 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/price_loaders/cache.rb', line 7 def initialize(name) @name = name @filename = "data/prices/#{name}.json" if File.exist?(@filename) @prices = JSON.parse(File.read(@filename)) else @prices = {} end end |
Instance Method Details
#[](coin, time) ⇒ Object
18 19 20 21 |
# File 'lib/coinsync/price_loaders/cache.rb', line 18 def [](coin, time) @prices[coin.code] ||= {} @prices[coin.code][time.to_i.to_s] end |
#[]=(coin, time, price) ⇒ Object
23 24 25 26 |
# File 'lib/coinsync/price_loaders/cache.rb', line 23 def []=(coin, time, price) @prices[coin.code] ||= {} @prices[coin.code][time.to_i.to_s] = price end |
#save ⇒ Object
28 29 30 31 |
# File 'lib/coinsync/price_loaders/cache.rb', line 28 def save FileUtils.mkdir_p(File.dirname(@filename)) File.write(@filename, JSON.generate(@prices)) end |