Class: GemOnDemand::FileCache
- Inherits:
-
Object
- Object
- GemOnDemand::FileCache
- Defined in:
- lib/gem_on_demand/file_cache.rb
Instance Method Summary collapse
- #delete(file) ⇒ Object
- #fetch(file, &block) ⇒ Object
-
#initialize(dir) ⇒ FileCache
constructor
A new instance of FileCache.
- #read(file) ⇒ Object
- #write(file, value) ⇒ Object
Constructor Details
#initialize(dir) ⇒ FileCache
Returns a new instance of FileCache.
3 4 5 |
# File 'lib/gem_on_demand/file_cache.rb', line 3 def initialize(dir) @dir = dir end |
Instance Method Details
#delete(file) ⇒ Object
19 20 21 22 |
# File 'lib/gem_on_demand/file_cache.rb', line 19 def delete(file) file = "#{@dir}/#{file}" File.unlink(file) if File.exist?(file) end |
#fetch(file, &block) ⇒ Object
24 25 26 |
# File 'lib/gem_on_demand/file_cache.rb', line 24 def fetch(file, &block) read(file) || write(file, yield) end |
#read(file) ⇒ Object
14 15 16 17 |
# File 'lib/gem_on_demand/file_cache.rb', line 14 def read(file) file = "#{@dir}/#{file}" Marshal.load(File.read(file)) if File.exist?(file) end |
#write(file, value) ⇒ Object
7 8 9 10 11 12 |
# File 'lib/gem_on_demand/file_cache.rb', line 7 def write(file, value) Utils.ensure_directory(@dir) file = "#{@dir}/#{file}" File.write(file, Marshal.dump(value)) value end |