Class: Relaton::DbCache
- Inherits:
-
Object
- Object
- Relaton::DbCache
- 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") ⇒ DbCache
constructor
A new instance of DbCache.
-
#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") ⇒ DbCache
Returns a new instance of DbCache.
10 11 12 13 14 |
# File 'lib/relaton/db_cache.rb', line 10 def initialize(dir, ext = "xml") @dir = dir @ext = ext FileUtils::mkdir_p dir end |
Instance Attribute Details
#dir ⇒ String (readonly)
7 8 9 |
# File 'lib/relaton/db_cache.rb', line 7 def dir @dir end |
Class Method Details
Instance Method Details
#[](key) ⇒ String
Read item
64 65 66 67 68 69 70 71 |
# File 'lib/relaton/db_cache.rb', line 64 def [](key) value = get(key) if (code = redirect? value) self[code] else value end end |
#[]=(key, value) ⇒ Object
Save item
39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/relaton/db_cache.rb', line 39 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
103 104 105 106 107 108 |
# File 'lib/relaton/db_cache.rb', line 103 def all(&block) Dir.glob("#{@dir}/**/*.{xml,yml,yaml}").sort.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.
121 122 123 124 125 126 127 |
# File 'lib/relaton/db_cache.rb', line 121 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
32 33 34 |
# File 'lib/relaton/db_cache.rb', line 32 def clear FileUtils.rm_rf Dir.glob "#{dir}/*" end |
#clone_entry(key, db) ⇒ Object
Save entry from cache of ‘db` to this cache.
79 80 81 82 83 84 |
# File 'lib/relaton/db_cache.rb', line 79 def clone_entry(key, db) self[key] ||= db.get(key) if (code = redirect? get(key)) clone_entry code, db end end |
#delete(key) ⇒ Object
Delete item
112 113 114 115 116 |
# File 'lib/relaton/db_cache.rb', line 112 def delete(key) file = filename key f = search_ext file File.delete f if f end |
#ext(value) ⇒ String
53 54 55 56 57 58 59 |
# File 'lib/relaton/db_cache.rb', line 53 def ext(value) case value when /^not_found/ then "notfound" when /^redirection/ then "redirect" else @ext end end |
#fetched(key) ⇒ String
Return fetched date
89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/relaton/db_cache.rb', line 89 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
144 145 146 147 148 149 |
# File 'lib/relaton/db_cache.rb', line 144 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
19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/relaton/db_cache.rb', line 19 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
132 133 134 135 136 137 138 |
# File 'lib/relaton/db_cache.rb', line 132 def valid_entry?(key, year) datestr = fetched key return false unless datestr date = Date.parse datestr year || Date.today - date < 60 end |