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<String>
Returns all items.
-
#check_version?(fdir) ⇒ TrueClass, FalseClass
Check if version of the DB match to the gem grammar hash.
- #clone_entry(key, db) ⇒ Object
-
#delete(key) ⇒ Object
Delete item.
- #ext(value) ⇒ String
-
#fetched(key) ⇒ String
Return fetched date.
-
#initialize(dir, ext = "xml") ⇒ DbCache
constructor
A new instance of DbCache.
-
#set_version(fdir) ⇒ Relaton::DbCache
Set version of the DB to the gem grammar hash.
-
#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.
9 10 11 12 13 14 15 |
# File 'lib/relaton/db_cache.rb', line 9 def initialize(dir, ext = "xml") @dir = dir @ext = ext 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
212 213 214 215 216 217 218 219 220 221 |
# File 'lib/relaton/db_cache.rb', line 212 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_rf globalname unless globalname.nil? FileUtils.rm_rf localname unless localname.nil? end Relaton::Db.new(globalname, localname) end |
Instance Method Details
#[](key) ⇒ String
Read item
45 46 47 48 49 50 51 52 |
# File 'lib/relaton/db_cache.rb', line 45 def [](key) value = get(key) if (code = redirect? value) self[code] else value end end |
#[]=(key, value) ⇒ Object
Save item
20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/relaton/db_cache.rb', line 20 def []=(key, value) if value.nil? delete key return end prefix_dir = "#{@dir}/#{prefix(key)}" FileUtils::mkdir_p prefix_dir unless Dir.exist? prefix_dir set_version prefix_dir File.write "#{filename(key)}.#{ext(value)}", value, encoding: "utf-8" end |
#all ⇒ Array<String>
Returns all items
78 79 80 81 82 |
# File 'lib/relaton/db_cache.rb', line 78 def all Dir.glob("#{@dir}/**/*.xml").sort.map do |f| File.read(f, encoding: "utf-8") end end |
#check_version?(fdir) ⇒ TrueClass, FalseClass
Check if version of the DB match to the gem grammar hash.
95 96 97 98 99 100 101 |
# File 'lib/relaton/db_cache.rb', line 95 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 |
#clone_entry(key, db) ⇒ Object
54 55 56 57 58 59 |
# File 'lib/relaton/db_cache.rb', line 54 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
86 87 88 89 90 |
# File 'lib/relaton/db_cache.rb', line 86 def delete(key) file = filename key f = search_ext(file) File.delete f if f end |
#ext(value) ⇒ String
34 35 36 37 38 39 40 |
# File 'lib/relaton/db_cache.rb', line 34 def ext(value) case value when /^not_found/ then "notfound" when /^redirection/ then "redirect" else @ext end end |
#fetched(key) ⇒ String
Return fetched date
64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/relaton/db_cache.rb', line 64 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(fdir) ⇒ Relaton::DbCache
Set version of the DB to the gem grammar hash.
106 107 108 109 110 111 112 |
# File 'lib/relaton/db_cache.rb', line 106 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 |
#valid_entry?(key, year) ⇒ Boolean
if cached reference is undated, expire it after 60 days
117 118 119 120 121 122 123 |
# File 'lib/relaton/db_cache.rb', line 117 def valid_entry?(key, year) datestr = fetched key return false unless datestr date = Date.parse datestr year || Date.today - date < 60 end |