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
-
.init_bib_caches(opts) ⇒ Object
Initialse and return relaton instance, with local and global cache names local_cache: local cache name; none created if nil; “relaton” created if empty global_cache: boolean to create global_cache flush_caches: flush caches.
Instance Method Summary collapse
-
#[](key) ⇒ String
Read item.
-
#[]=(key, value) ⇒ Object
Save item.
-
#all ⇒ Array<Hash>
Returns all items.
-
#check_version? ⇒ TrueClass, FalseClass
Check if version of the DB match to the gem version.
-
#delete(key) ⇒ Object
Delete item.
-
#fetched(key) ⇒ String
Return fetched date.
-
#initialize(dir) ⇒ DbCache
constructor
A new instance of DbCache.
-
#set_version ⇒ Relaton::DbCache
Set version of the DB to the gem version.
-
#valid_entry?(key, year) ⇒ Boolean
if cached reference is undated, expire it after 60 days.
Constructor Details
#initialize(dir) ⇒ DbCache
Returns a new instance of DbCache.
9 10 11 12 13 14 |
# File 'lib/relaton/db_cache.rb', line 9 def initialize(dir) @dir = dir FileUtils::mkdir_p @dir file_version = "#{@dir}/version" set_version unless File.exist? file_version end |
Instance Attribute Details
#dir ⇒ String (readonly)
6 7 8 |
# File 'lib/relaton/db_cache.rb', line 6 def dir @dir end |
Class Method Details
.init_bib_caches(opts) ⇒ Object
Initialse and return relaton instance, with local and global cache names local_cache: local cache name; none created if nil; “relaton” created if empty global_cache: boolean to create global_cache flush_caches: flush caches
139 140 141 142 143 144 145 146 147 148 |
# File 'lib/relaton/db_cache.rb', line 139 def init_bib_caches(opts) globalname = global_bibliocache_name if opts[:global_cache] localname = local_bibliocache_name(opts[:local_cache]) localname = "relaton" if localname&.empty? if opts[:flush_caches] FileUtils.rm_f globalname unless globalname.nil? FileUtils.rm_f localname unless localname.nil? end Relaton::Db.new(globalname, localname) end |
Instance Method Details
#[](key) ⇒ String
Read item
33 34 35 36 37 38 |
# File 'lib/relaton/db_cache.rb', line 33 def [](key) file = filename key return unless File.exist? file File.read(file, encoding: "utf-8") end |
#[]=(key, value) ⇒ Object
Save item
19 20 21 22 23 24 25 26 27 28 |
# File 'lib/relaton/db_cache.rb', line 19 def []=(key, value) if value.nil? delete key return end prefix_dir = "#{@dir}/#{prefix(key)}" FileUtils::mkdir_p prefix_dir File.write filename(key), value, encoding: "utf-8" end |
#all ⇒ Array<Hash>
Returns all items
57 58 59 60 61 |
# File 'lib/relaton/db_cache.rb', line 57 def all Dir.glob("#{@dir}/**/*.xml").sort.map do |f| File.read(f, encoding: "utf-8") end end |
#check_version? ⇒ TrueClass, FalseClass
Check if version of the DB match to the gem version.
72 73 74 75 |
# File 'lib/relaton/db_cache.rb', line 72 def check_version? v = File.read @dir + "/version", encoding: "utf-8" v == VERSION end |
#delete(key) ⇒ Object
Delete item
65 66 67 68 |
# File 'lib/relaton/db_cache.rb', line 65 def delete(key) file = filename key File.delete file if File.exist? file end |
#fetched(key) ⇒ String
Return fetched date
43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/relaton/db_cache.rb', line 43 def fetched(key) value = self[key] return unless value if value =~ /^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 |
#set_version ⇒ Relaton::DbCache
Set version of the DB to the gem version.
79 80 81 82 |
# File 'lib/relaton/db_cache.rb', line 79 def set_version File.write @dir + "/version", VERSION, encoding: "utf-8" self end |
#valid_entry?(key, year) ⇒ Boolean
if cached reference is undated, expire it after 60 days
87 88 89 90 91 92 93 |
# File 'lib/relaton/db_cache.rb', line 87 def valid_entry?(key, year) datestr = fetched key return false unless datestr date = Date.parse datestr year || Date.today - date < 60 end |