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



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

#dirString (readonly)

Returns:

  • (String)


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



210
211
212
213
214
215
216
217
218
219
# File 'lib/relaton/db_cache.rb', line 210

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

Parameters:

  • key (String)

Returns:

  • (String)


41
42
43
44
45
46
47
48
# File 'lib/relaton/db_cache.rb', line 41

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



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/relaton/db_cache.rb', line 20

def []=(key, value)
  if value.nil?
    delete key
    return
  end

  prefix_dir = "#{@dir}/#{prefix(key)}"
  unless Dir.exist? prefix_dir
    FileUtils::mkdir_p prefix_dir
    set_version prefix_dir
  end
  ex = if value =~ /^not_found/ then "notfound"
       elsif value =~ /^redirection/ then "redirect"
       else @ext
       end
  File.write "#{filename(key)}.#{ex}", value, encoding: "utf-8"
end

#allArray<String>

Returns all items

Returns:

  • (Array<String>)


74
75
76
77
78
# File 'lib/relaton/db_cache.rb', line 74

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.

Parameters:

  • fdir (String)

    dir pathe to flover cache

Returns:

  • (TrueClass, FalseClass)


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

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



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

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)


82
83
84
85
86
# File 'lib/relaton/db_cache.rb', line 82

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

#fetched(key) ⇒ String

Return fetched date

Parameters:

  • key (String)

Returns:

  • (String)


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

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.

Parameters:

  • fdir (String)

    dir pathe to flover cache

Returns:



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

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

Parameters:

  • key (String)
  • year (String)

Returns:

  • (Boolean)


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

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

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