lib/relaton/db_cache.rb in relaton-0.6.1 vs lib/relaton/db_cache.rb in relaton-0.6.2
- old
+ new
@@ -26,11 +26,15 @@
prefix_dir = "#{@dir}/#{prefix(key)}"
unless Dir.exist? prefix_dir
FileUtils::mkdir_p prefix_dir
set_version prefix_dir
end
- File.write filename(key), value, encoding: "utf-8"
+ ex = if value =~ /^not_found/ then "notfound"
+ elsif value =~ /^redirection/ then "redirect"
+ else @ext
+ end
+ File.write "#{filename(key)}.#{ex}", value, encoding: "utf-8"
end
# Read item
# @param key [String]
# @return [String]
@@ -75,11 +79,12 @@
# Delete item
# @param key [String]
def delete(key)
file = filename key
- File.delete file if File.exist? file
+ f = search_ext(file)
+ File.delete f if f
end
# Check if version of the DB match to the gem grammar hash.
# @param fdir [String] dir pathe to flover cache
# @return [TrueClass, FalseClass]
@@ -126,13 +131,13 @@
#
# @param key [String]
# @return [String, NilClass]
def get(key)
file = filename key
- return unless File.exist? file
+ return unless (f = search_ext(file))
- File.read(file, encoding: "utf-8")
+ File.read(f, encoding: "utf-8")
end
private
# Check if a file content is redirection
@@ -152,10 +157,25 @@
fn = if prefcode
"#{prefcode[:prefix]}/#{prefcode[:code].gsub(/[-:\s\/\()]/, '_').squeeze("_")}"
else
key.gsub(/[-:\s]/, "_")
end
- "#{@dir}/#{fn.sub(/(,|_$)/, '')}.#{@ext}"
+ "#{@dir}/#{fn.sub(/(,|_$)/, '')}"
+ end
+
+ #
+ # Checks if there is file with xml or txt extension and return filename with the extension.
+ #
+ # @param file [String]
+ # @return [String, NilClass]
+ def search_ext(file)
+ if File.exist?("#{file}.#{@ext}")
+ "#{file}.#{@ext}"
+ elsif File.exist? "#{file}.notfound"
+ "#{file}.notfound"
+ elsif File.exist? "#{file}.redirect"
+ "#{file}.redirect"
+ end
end
# Return item's subdir
# @param key [String]
# @return [String]