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

Returns a new instance of DbCache.

Parameters:

  • dir (String)

    DB directory



10
11
12
13
14
15
16
# File 'lib/relaton/db_cache.rb', line 10

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

#dirString (readonly)

Returns:

  • (String)


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

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



237
238
239
240
241
242
243
244
245
246
# File 'lib/relaton/db_cache.rb', line 237

def init_bib_caches(opts) # rubocop:disable Metrics/CyclomaticComplexity
  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

Parameters:

  • key (String)

Returns:

  • (String)


60
61
62
63
64
65
66
67
# File 'lib/relaton/db_cache.rb', line 60

def [](key)
  value = get(key)
  if (code = redirect? value)
    self[code]
  else
    value
  end
end

#[]=(key, value) ⇒ Object

Save item

Parameters:

  • key (String)
  • value (String)

    Bibitem xml serialization



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/relaton/db_cache.rb', line 35

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_safe_write "#{filename(key)}.#{ext(value)}", value
end

#allArray<String>

Returns all items

Returns:

  • (Array<String>)


93
94
95
96
97
98
# File 'lib/relaton/db_cache.rb', line 93

def all
  Dir.glob("#{@dir}/**/*.{xml,yml,yaml}").sort.map do |f|
    content = File.read(f, encoding: "utf-8")
    block_given? ? yield(f, content) : content
  end
end

#check_version?(fdir) ⇒ TrueClass, FalseClass

Check if version of the DB match to the gem grammar hash.

Parameters:

  • fdir (String)

    dir pathe to flover cache

Returns:

  • (TrueClass, FalseClass)


111
112
113
114
115
116
117
# File 'lib/relaton/db_cache.rb', line 111

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

#clearObject

Clear database



28
29
30
# File 'lib/relaton/db_cache.rb', line 28

def clear
  FileUtils.rm_rf Dir.glob "#{dir}/*" if @ext == "xml" # unless it's static DB
end

#clone_entry(key, db) ⇒ Object



69
70
71
72
73
74
# File 'lib/relaton/db_cache.rb', line 69

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

Parameters:

  • key (String)


102
103
104
105
106
# File 'lib/relaton/db_cache.rb', line 102

def delete(key)
  file = filename key
  f = search_ext(file)
  File.delete f if f
end

#ext(value) ⇒ String

Parameters:

  • value (String)

Returns:

  • (String)


49
50
51
52
53
54
55
# File 'lib/relaton/db_cache.rb', line 49

def ext(value)
  case value
  when /^not_found/ then "notfound"
  when /^redirection/ then "redirect"
  else @ext
  end
end

#fetched(key) ⇒ String

Return fetched date

Parameters:

  • key (String)

Returns:

  • (String)


79
80
81
82
83
84
85
86
87
88
89
# File 'lib/relaton/db_cache.rb', line 79

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

#mv(new_dir) ⇒ Object

Move caches to anothe dir

Parameters:

  • new_dir (String, nil)


20
21
22
23
24
25
# File 'lib/relaton/db_cache.rb', line 20

def mv(new_dir)
  return unless new_dir && @ext == "xml"

  FileUtils.mv dir, new_dir
  @dir = new_dir
end

#set_version(fdir) ⇒ Relaton::DbCache

Set version of the DB to the gem grammar hash.

Parameters:

  • fdir (String)

    dir pathe to flover cache

Returns:



122
123
124
125
126
127
128
# File 'lib/relaton/db_cache.rb', line 122

def set_version(fdir)
  file_version = "#{fdir}/version"
  unless File.exist? file_version
    file_safe_write file_version, grammar_hash(fdir)
  end
  self
end

#valid_entry?(key, year) ⇒ Boolean

if cached reference is undated, expire it after 60 days

Parameters:

  • key (String)
  • year (String)

Returns:

  • (Boolean)


133
134
135
136
137
138
139
# File 'lib/relaton/db_cache.rb', line 133

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

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