lib/relaton/db_cache.rb in relaton-0.5.19 vs lib/relaton/db_cache.rb in relaton-0.6.0
- old
+ new
@@ -8,12 +8,12 @@
# @param dir [String] DB directory
def initialize(dir, ext = "xml")
@dir = dir
@ext = ext
FileUtils::mkdir_p @dir
- file_version = "#{@dir}/version"
- set_version unless File.exist? file_version
+ # file_version = "#{@dir}/version"
+ # set_version # unless File.exist? file_version
end
# Save item
# @param key [String]
# @param value [String] Bibitem xml serialization
@@ -22,11 +22,14 @@
delete key
return
end
prefix_dir = "#{@dir}/#{prefix(key)}"
- FileUtils::mkdir_p prefix_dir
+ unless Dir.exist? prefix_dir
+ FileUtils::mkdir_p prefix_dir
+ set_version prefix_dir
+ end
File.write filename(key), value, encoding: "utf-8"
end
# Read item
# @param key [String]
@@ -61,11 +64,11 @@
doc.at("/bibitem/fetched|bibdata/fetched")&.text
end
end
# Returns all items
- # @return [Array<Hash>]
+ # @return [Array<String>]
def all
Dir.glob("#{@dir}/**/*.xml").sort.map do |f|
File.read(f, encoding: "utf-8")
end
end
@@ -75,21 +78,29 @@
def delete(key)
file = filename key
File.delete file if File.exist? file
end
- # Check if version of the DB match to the gem version.
+ # Check if version of the DB match to the gem grammar hash.
+ # @param fdir [String] dir pathe to flover cache
# @return [TrueClass, FalseClass]
- def check_version?
- v = File.read @dir + "/version", encoding: "utf-8"
- v.strip == VERSION
+ def check_version?(fdir)
+ version_dir = fdir + "/version"
+ return false unless File.exist? version_dir
+
+ v = File.read version_dir, encoding: "utf-8"
+ v.strip == grammar_hash(fdir)
end
- # Set version of the DB to the gem version.
+ # Set version of the DB to the gem grammar hash.
+ # @param fdir [String] dir pathe to flover cache
# @return [Relaton::DbCache]
- def set_version
- File.write @dir + "/version", VERSION, encoding: "utf-8"
+ def set_version(fdir)
+ file_version = "#{fdir}/version"
+ unless File.exist? file_version
+ File.write file_version, grammar_hash(fdir), encoding: "utf-8"
+ end
self
end
# if cached reference is undated, expire it after 60 days
# @param key [String]
@@ -101,9 +112,16 @@
date = Date.parse datestr
year || Date.today - date < 60
end
protected
+
+ # @param fdir [String] dir pathe to flover cache
+ # @return [String]
+ def grammar_hash(fdir)
+ type = fdir.split("/").last
+ Relaton::Registry.instance.by_type(type).grammar_hash
+ end
# Reads file by a key
#
# @param key [String]
# @return [String, NilClass]