Class: Iev::DbCache
- Inherits:
-
Object
- Object
- Iev::DbCache
- Defined in:
- lib/iev/db_cache.rb
Instance Attribute Summary collapse
- #dir ⇒ String readonly
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 ⇒ Iev::DbCache
Set version of the DB to the gem version.
Constructor Details
Instance Attribute Details
#dir ⇒ String (readonly)
6 7 8 |
# File 'lib/iev/db_cache.rb', line 6 def dir @dir end |
Instance Method Details
#[](key) ⇒ String
Read item
31 32 33 34 35 36 |
# File 'lib/iev/db_cache.rb', line 31 def [](key) file = filename key return unless File.exist? file File.read(file, encoding: "utf-8") end |
#[]=(key, value) ⇒ Object
Save item
20 21 22 23 24 25 26 |
# File 'lib/iev/db_cache.rb', line 20 def []=(key, value) return if value.nil? 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
55 56 57 58 59 |
# File 'lib/iev/db_cache.rb', line 55 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.
70 71 72 73 |
# File 'lib/iev/db_cache.rb', line 70 def check_version? v = File.read "#{@dir}/version", encoding: "utf-8" v == VERSION end |
#delete(key) ⇒ Object
Delete item
63 64 65 66 |
# File 'lib/iev/db_cache.rb', line 63 def delete(key) file = filename key File.delete file if File.exist? file end |
#fetched(key) ⇒ String
Return fetched date
41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/iev/db_cache.rb', line 41 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")&.text # end end |
#set_version ⇒ Iev::DbCache
Set version of the DB to the gem version.
77 78 79 80 |
# File 'lib/iev/db_cache.rb', line 77 def set_version File.write "#{@dir}/version", VERSION, encoding: "utf-8" self end |