class Fontist::Utils::Cache
Public Instance Methods
delete(key)
click to toggle source
# File lib/fontist/utils/cache.rb, line 20 def delete(key) lock(lock_path) do map = load_cache return unless map[key] value = map.delete(key) File.write(cache_map_path, YAML.dump(map)) value end end
fetch(key) { || ... }
click to toggle source
# File lib/fontist/utils/cache.rb, line 6 def fetch(key) map = load_cache if cache_exist?(map[key]) print(map[key]) return downloaded_file(map[key]) end generated_file = yield path = save_cache(generated_file, key) downloaded_file(path) end
set(key, value)
click to toggle source
# File lib/fontist/utils/cache.rb, line 31 def set(key, value) lock(lock_path) do map = load_cache map[key] = value File.write(cache_map_path, YAML.dump(map)) end end
Private Instance Methods
cache_exist?(path)
click to toggle source
# File lib/fontist/utils/cache.rb, line 53 def cache_exist?(path) path && File.exist?(downloaded_path(path)) end
cache_map_path()
click to toggle source
# File lib/fontist/utils/cache.rb, line 41 def cache_map_path Fontist.downloads_path.join("map.yml") end
create_downloads_directory()
click to toggle source
# File lib/fontist/utils/cache.rb, line 92 def create_downloads_directory unless Fontist.downloads_path.exist? FileUtils.mkdir_p(Fontist.downloads_path) end end
downloaded_file(path)
click to toggle source
# File lib/fontist/utils/cache.rb, line 49 def downloaded_file(path) File.new(downloaded_path(path), "rb") end
downloaded_path(path)
click to toggle source
# File lib/fontist/utils/cache.rb, line 57 def downloaded_path(path) Fontist.downloads_path.join(path) end
generate_file_path(source)
click to toggle source
# File lib/fontist/utils/cache.rb, line 98 def generate_file_path(source) dir = Dir.mktmpdir(nil, Fontist.downloads_path) filename = source.original_filename File.join(dir, filename) end
load_cache()
click to toggle source
# File lib/fontist/utils/cache.rb, line 45 def load_cache cache_map_path.exist? ? YAML.load_file(cache_map_path) : {} end
lock_path()
click to toggle source
# File lib/fontist/utils/cache.rb, line 81 def lock_path cache_map_path.to_s + ".lock" end
move(source_file, target_path)
click to toggle source
# File lib/fontist/utils/cache.rb, line 104 def move(source_file, target_path) # Windows requires file descriptors to be closed before files are moved source_file.close FileUtils.mv(source_file.path, target_path) end
move_to_downloads(source)
click to toggle source
# File lib/fontist/utils/cache.rb, line 85 def move_to_downloads(source) create_downloads_directory path = generate_file_path(source) move(source, path) relative_to_downloads(path) end
print(path)
click to toggle source
# File lib/fontist/utils/cache.rb, line 61 def print(path) Fontist.ui.say("Fetched from cache: #{size(path)} MiB.") end
relative_to_downloads(path)
click to toggle source
# File lib/fontist/utils/cache.rb, line 110 def relative_to_downloads(path) Pathname.new(path).relative_path_from(Fontist.downloads_path).to_s end
save_cache(generated_file, key)
click to toggle source
# File lib/fontist/utils/cache.rb, line 69 def save_cache(generated_file, key) path = move_to_downloads(generated_file) lock(lock_path) do map = load_cache map[key] = path File.write(cache_map_path, YAML.dump(map)) end path end
size(path)
click to toggle source
# File lib/fontist/utils/cache.rb, line 65 def size(path) File.size(downloaded_path(path)) / (1024 * 1024) end