Class: WithTimedCache::YAMLCache

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

Instance Attribute Summary

Attributes inherited from Cache

#key, #location, #max_age

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Cache

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

Constructor Details

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

Returns a new instance of YAMLCache.



9
10
11
12
13
14
# File 'lib/with_timed_cache/yaml_cache.rb', line 9

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

Class Method Details

.file_extensionObject



24
25
26
# File 'lib/with_timed_cache/yaml_cache.rb', line 24

def self.file_extension
  "yml"
end

Instance Method Details

#readObject



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

def read
  YAML.load @location
end

#write(data) ⇒ Object



20
21
22
# File 'lib/with_timed_cache/yaml_cache.rb', line 20

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