Class: Relaton::DbCache

Inherits:
Object
  • Object
show all
Defined in:
lib/relaton/db_cache.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dir, ext = "xml") ⇒ 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

#dirString (readonly)



7
8
9
# File 'lib/relaton/db_cache.rb', line 7

def dir
  @dir
end

Class Method Details

.grammar_hash(fdir) ⇒ String



159
160
161
162
# File 'lib/relaton/db_cache.rb', line 159

def self.grammar_hash(fdir)
  type = fdir.split("/").last
  Relaton::Registry.instance.by_type(type)&.grammar_hash
end

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_code 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.



127
128
129
130
131
132
133
# File 'lib/relaton/db_cache.rb', line 127

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

#clearObject

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_code get(key))
    clone_entry code, db
  end
end

#delete(key) ⇒ Object

Delete item



112
113
114
115
116
117
118
119
120
121
122
# File 'lib/relaton/db_cache.rb', line 112

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



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



150
151
152
153
154
155
# File 'lib/relaton/db_cache.rb', line 150

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



138
139
140
141
142
143
144
# File 'lib/relaton/db_cache.rb', line 138

def valid_entry?(key, year)
  datestr = fetched key
  return false unless datestr

  date = Date.parse datestr
  year || Date.today - date < 60
end