lib/fontist/utils/cache.rb in fontist-1.5.1 vs lib/fontist/utils/cache.rb in fontist-1.6.0
- old
+ new
@@ -1,16 +1,20 @@
module Fontist
module Utils
class Cache
- def fetch(key)
+ def fetch(key, bar: nil)
map = load_cache
- return downloaded_path(map[key]) if cache_exist?(map[key])
+ if cache_exist?(map[key])
+ print_bar(bar, map[key]) if bar
+ return downloaded_file(map[key])
+ end
+
generated_file = yield
path = save_cache(generated_file, key)
- downloaded_path(path)
+ downloaded_file(path)
end
private
def cache_map_path
@@ -19,15 +23,27 @@
def load_cache
cache_map_path.exist? ? YAML.load_file(cache_map_path) : {}
end
- def downloaded_path(path)
- File.new(Fontist.downloads_path.join(path))
+ def downloaded_file(path)
+ File.new(downloaded_path(path))
end
def cache_exist?(path)
- path && File.exist?(Fontist.downloads_path.join(path))
+ path && File.exist?(downloaded_path(path))
+ end
+
+ def downloaded_path(path)
+ Fontist.downloads_path.join(path)
+ end
+
+ def print_bar(bar, path)
+ File.size(downloaded_path(path)).tap do |size|
+ bar.total = size
+ bar.increment(size)
+ bar.finish("cache")
+ end
end
def save_cache(generated_file, key)
path = move_to_downloads(generated_file)