Class: WithTimedCache::JSONCache
- Defined in:
- lib/with_timed_cache/json_cache.rb
Instance Attribute Summary
Attributes inherited from Cache
Instance Method Summary collapse
-
#initialize(key, opts = {}) ⇒ JSONCache
constructor
A new instance of JSONCache.
- #read ⇒ Object
- #write(data) ⇒ Object
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
#read ⇒ Object
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 |