Class: CoinSync::CurrencyConverters::Cache

Inherits:
Object
  • Object
show all
Defined in:
lib/coinsync/currency_converters/cache.rb

Instance Method Summary collapse

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

#saveObject



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