Class: Relaton::Db::Cache
- Inherits:
-
Object
- Object
- Relaton::Db::Cache
- Defined in:
- lib/relaton/db/cache.rb
Instance Attribute Summary collapse
- #dir ⇒ String readonly
Class Method Summary collapse
Instance Method Summary collapse
-
#[](key) ⇒ String
Read item.
-
#[]=(key, value) ⇒ Object
Save item.
-
#all(&block) ⇒ Array<String>
Returns all items.
-
#check_version?(fdir) ⇒ Boolean
Check if version of the DB match to the gem grammar hash.
-
#clear ⇒ Object
Clear database.
-
#clone_entry(key, db) ⇒ Object
Save entry from cache of ‘db` to this cache.
-
#delete(key) ⇒ Object
Delete item.
- #ext(value) ⇒ String
-
#fetched(key) ⇒ String
Return fetched date.
-
#get(key) ⇒ String, NilClass
Reads file by a key.
-
#initialize(dir, ext = "xml") ⇒ Cache
constructor
A new instance of Cache.
-
#mv(new_dir) ⇒ String?
Move caches to anothe dir.
-
#valid_entry?(key, year) ⇒ Boolean
if cached reference is undated, expire it after 60 days.
Constructor Details
#initialize(dir, ext = "xml") ⇒ Cache
Returns a new instance of Cache.
11 12 13 14 15 |
# File 'lib/relaton/db/cache.rb', line 11 def initialize(dir, ext = "xml") @dir = dir @ext = ext FileUtils::mkdir_p dir end |
Instance Attribute Details
#dir ⇒ String (readonly)
8 9 10 |
# File 'lib/relaton/db/cache.rb', line 8 def dir @dir end |
Class Method Details
.grammar_hash(fdir) ⇒ String
160 161 162 163 |
# File 'lib/relaton/db/cache.rb', line 160 def self.grammar_hash(fdir) type = fdir.split("/").last Registry.instance.by_type(type)&.grammar_hash end |
Instance Method Details
#[](key) ⇒ String
Read item
65 66 67 68 69 70 71 72 |
# File 'lib/relaton/db/cache.rb', line 65 def [](key) value = get(key) if (code = redirect_code value) self[code] else value end end |
#[]=(key, value) ⇒ Object
Save item
40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/relaton/db/cache.rb', line 40 def []=(key, value) if value.nil? delete key return end prefix_dir = "#{@dir}/#{prefix(key)}" FileUtils::mkdir_p prefix_dir set_version prefix_dir file_safe_write "#{filename(key)}.#{ext(value)}", value end |
#all(&block) ⇒ Array<String>
Returns all items
104 105 106 107 108 109 |
# File 'lib/relaton/db/cache.rb', line 104 def all(&block) Dir.glob("#{@dir}/**/*.{xml,yml,yaml}").map do |f| content = File.read(f, encoding: "utf-8") block ? yield(f, content) : content end end |
#check_version?(fdir) ⇒ Boolean
Check if version of the DB match to the gem grammar hash.
128 129 130 131 132 133 134 |
# File 'lib/relaton/db/cache.rb', line 128 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 == self.class.grammar_hash(fdir) end |
#clear ⇒ Object
Clear database
33 34 35 |
# File 'lib/relaton/db/cache.rb', line 33 def clear FileUtils.rm_rf Dir.glob "#{dir}/*" end |
#clone_entry(key, db) ⇒ Object
Save entry from cache of ‘db` to this cache.
80 81 82 83 84 85 |
# File 'lib/relaton/db/cache.rb', line 80 def clone_entry(key, db) self[key] ||= db.get(key) if (code = redirect_code get(key)) clone_entry code, db end end |
#delete(key) ⇒ Object
Delete item
113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/relaton/db/cache.rb', line 113 def delete(key) file = filename key f = search_ext file return unless f if File.extname(f) == ".redirect" code = redirect_code get(key) delete code if code end File.delete f end |
#ext(value) ⇒ String
54 55 56 57 58 59 60 |
# File 'lib/relaton/db/cache.rb', line 54 def ext(value) case value when /^not_found/ then "notfound" when /^redirection/ then "redirect" else @ext end end |
#fetched(key) ⇒ String
Return fetched date
90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/relaton/db/cache.rb', line 90 def fetched(key) value = self[key] return unless value if value.match?(/^not_found/) value.match(/\d{4}-\d{2}-\d{2}/).to_s else doc = Nokogiri::XML value doc.at("/bibitem/fetched|bibdata/fetched")&.text end end |
#get(key) ⇒ String, NilClass
Reads file by a key
151 152 153 154 155 156 |
# File 'lib/relaton/db/cache.rb', line 151 def get(key) file = filename key return unless (f = search_ext(file)) File.read(f, encoding: "utf-8") end |
#mv(new_dir) ⇒ String?
Move caches to anothe dir
20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/relaton/db/cache.rb', line 20 def mv(new_dir) return unless new_dir && @ext == "xml" if File.exist? new_dir Util.info "target directory exists `#{new_dir}`" return end FileUtils.mv dir, new_dir @dir = new_dir end |
#valid_entry?(key, year) ⇒ Boolean
if cached reference is undated, expire it after 60 days
139 140 141 142 143 144 145 |
# File 'lib/relaton/db/cache.rb', line 139 def valid_entry?(key, year) datestr = fetched key return false unless datestr date = Date.parse datestr year || Date.today - date < 60 end |