module FileCache
Provides caching functionality for files downloaded from the internet
Constants
- CACHEDIR
Public Class Methods
Source
# File lib/rbbt/util/filecache.rb, line 33 def self.add(filename, content) path = path(filename) FileUtils.makedirs(File.dirname(path), :mode => 0777) Misc.sensiblewrite(path, content) FileUtils.chmod 0666, path path end
Source
# File lib/rbbt/util/filecache.rb, line 63 def self.cache_online_elements(ids, pattern = nil, &block) ids = [ids] unless Array === ids result_files = {} missing = [] ids.each do |id| filename = pattern ? pattern.sub("{ID}", id.to_s) : id.to_s if FileCache.found(filename) result_files[id] = FileCache.path(filename) else missing << id end end yield(missing).each do |id, content| filename = pattern ? pattern.sub("{ID}", id.to_s) : id.to_s path = FileCache.path(filename) Open.write(path, content) result_files[id] = path end if missing.any? result_files end
Source
# File lib/rbbt/util/filecache.rb, line 13 def self.cachedir=(cachedir) CACHEDIR.replace cachedir Open.mkdir CACHEDIR unless Open.exist? CACHEDIR end
Source
# File lib/rbbt/util/filecache.rb, line 57 def self.del(filename) path = path(filename) FileUtils.rm path if File.exist? path end
Source
# File lib/rbbt/util/filecache.rb, line 45 def self.found(filename) File.exist? FileCache.path(filename) end
Source
# File lib/rbbt/util/filecache.rb, line 49 def self.get(filename) path = path(filename) return nil if ! File.exist? path File.open(path) end
Source
# File lib/rbbt/util/filecache.rb, line 22 def self.path(filename) filename = File.basename filename filename.match(/(.+)\.(.+)/) base = filename.sub(/\..+/,'') dirs = base.scan(/./).reverse.values_at(0,1,2,3,4).compact File.join(File.join(CACHEDIR, *dirs), filename) end