Class: WithTimedCache::JSONCache

Inherits:
Cache
  • Object
show all
Defined in:
lib/with_timed_cache/json_cache.rb

Instance Attribute Summary

Attributes inherited from Cache

#key, #location, #max_age

Instance Method Summary collapse

Methods inherited from Cache

#exists?, file_extension, #filename, local_class_name, #stale?

Constructor Details

#initialize(key, opts = {}) ⇒ JSONCache

Returns a new instance of JSONCache.



6
7
8
9
10
# File 'lib/with_timed_cache/json_cache.rb', line 6

def initialize(key, opts = {})
  super
  @location = opts[:location] || File.join("tmp", filename)
  @location = File.join(@location, filename) if File.directory?(@location)
end

Instance Method Details

#readObject



12
13
14
# File 'lib/with_timed_cache/json_cache.rb', line 12

def read
  JSON.parse(File.read(@location), symbolize_names: true)
end

#write(data) ⇒ Object



16
17
18
# File 'lib/with_timed_cache/json_cache.rb', line 16

def write(data)
  File.open(@location, 'w') { |f| f.write data.to_json }
end