use json to cache descriptions
We need to use `atomic_write` when saving the cache. And it seems that CSV module doesn't support dump, so let's use JSON instead.
This commit is contained in:
parent
c5536e1e08
commit
65996b5887
@ -1,9 +1,8 @@
|
|||||||
require "formula"
|
require "formula"
|
||||||
require "formula_versions"
|
require "formula_versions"
|
||||||
require "csv"
|
|
||||||
|
|
||||||
class Descriptions
|
class Descriptions
|
||||||
CACHE_FILE = HOMEBREW_CACHE + "desc_cache"
|
CACHE_FILE = HOMEBREW_CACHE + "desc_cache.json"
|
||||||
|
|
||||||
def self.cache
|
def self.cache
|
||||||
@cache || self.load_cache
|
@cache || self.load_cache
|
||||||
@ -13,9 +12,7 @@ class Descriptions
|
|||||||
# return nil.
|
# return nil.
|
||||||
def self.load_cache
|
def self.load_cache
|
||||||
if CACHE_FILE.exist?
|
if CACHE_FILE.exist?
|
||||||
@cache = {}
|
@cache = Utils::JSON.load(CACHE_FILE.read)
|
||||||
CSV.foreach(CACHE_FILE) { |name, desc| @cache[name] = desc }
|
|
||||||
@cache
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -23,11 +20,7 @@ class Descriptions
|
|||||||
# directory.
|
# directory.
|
||||||
def self.save_cache
|
def self.save_cache
|
||||||
HOMEBREW_CACHE.mkpath
|
HOMEBREW_CACHE.mkpath
|
||||||
CSV.open(CACHE_FILE, 'w') do |csv|
|
CACHE_FILE.atomic_write Utils::JSON.dump(@cache)
|
||||||
@cache.each do |name, desc|
|
|
||||||
csv << [name, desc]
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Create a hash mapping all formulae to their descriptions;
|
# Create a hash mapping all formulae to their descriptions;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user