relaton 1.8.pre4 → 1.9.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/rake.yml +1 -11
- data/lib/relaton/config.rb +2 -3
- data/lib/relaton/db.rb +53 -81
- data/lib/relaton/db_cache.rb +99 -55
- data/lib/relaton/processor.rb +13 -2
- data/lib/relaton/registry.rb +11 -0
- data/lib/relaton/version.rb +1 -1
- data/lib/relaton.rb +0 -1
- data/relaton.gemspec +18 -21
- data/spec/relaton/db_cache_spec.rb +2 -2
- data/spec/relaton/db_spec.rb +0 -17
- data/spec/relaton/processor_spec.rb +4 -0
- data/spec/relaton/{regirtry_spec.rb → registry_spec.rb} +7 -1
- data/spec/relaton_spec.rb +27 -54
- data/spec/spec_helper.rb +0 -4
- data/spec/vcr_cassetes/api_relaton_org.yml +14 -8
- data/spec/vcr_cassetes/api_relaton_org_unavailable.yml +56 -40
- data/spec/vcr_cassetes/async_fetch.yml +1297 -1297
- data/spec/vcr_cassetes/bsi_bs_en_iso_8848.yml +117 -114
- data/spec/vcr_cassetes/cc_dir_10005_2019.yml +12 -12
- data/spec/vcr_cassetes/cen_en_10160_1999.yml +26 -26
- data/spec/vcr_cassetes/cie_001_1980.yml +7 -7
- data/spec/vcr_cassetes/ecma_6.yml +7 -7
- data/spec/vcr_cassetes/fisp_140.yml +6 -57
- data/spec/vcr_cassetes/gb_t_20223_2006.yml +6 -6
- data/spec/vcr_cassetes/iec_60050_102_2007.yml +27 -27
- data/spec/vcr_cassetes/iec_combined_included.yml +97 -97
- data/spec/vcr_cassetes/ieee_528_2019.yml +18 -18
- data/spec/vcr_cassetes/iho_b_11.yml +7 -7
- data/spec/vcr_cassetes/iso_111111119115_1.yml +45 -26
- data/spec/vcr_cassetes/iso_19115_1.yml +57 -41
- data/spec/vcr_cassetes/iso_19115_1_2.yml +111 -79
- data/spec/vcr_cassetes/iso_19115_all_parts.yml +198 -0
- data/spec/vcr_cassetes/{19133_2005.yml → iso_19133_2005.yml} +55 -39
- data/spec/vcr_cassetes/iso_cd_14093.yml +202 -0
- data/spec/vcr_cassetes/iso_combined_applied.yml +111 -79
- data/spec/vcr_cassetes/iso_combined_included.yml +113 -81
- data/spec/vcr_cassetes/itu_combined_included.yml +164 -164
- data/spec/vcr_cassetes/ogc_19_025r1.yml +1171 -1163
- data/spec/vcr_cassetes/omg_ami4ccm_1_0.yml +4 -4
- data/spec/vcr_cassetes/rfc_8341.yml +7 -7
- data/spec/vcr_cassetes/sp_800_38b.yml +6 -57
- data/spec/vcr_cassetes/un_rtade_cefact_2004_32.yml +29 -29
- data/spec/vcr_cassetes/w3c_json_ld11.yml +12 -12
- metadata +50 -53
- data/spec/vcr_cassetes/hist_cmbined_included.yml +0 -105
- data/spec/vcr_cassetes/iso_19115.yml +0 -182
- data/spec/vcr_cassetes/iso_awi_14093.yml +0 -182
- data/spec/vcr_cassetes/iso_iec_guide_2.yml +0 -232
- data/spec/vcr_cassetes/rfc_unsuccess.yml +0 -70
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d217d10128648eabc4dbeed414e931b6bb4bfcb9f51f74a11e49fdc0af513c6e
|
4
|
+
data.tar.gz: 8eb975a6ebbbee9176de5b8f20e928cd6512b04c29c412f9b3dbe8006a6383f8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: '09009418c348753e9ef2222c8887120dc12e467e83c30cc2053d711ff53726cd57885ea07cd8bc21d15bd7174415cd977e2f225db13c1451444e97cffa49ebfd'
|
7
|
+
data.tar.gz: d6da61ac6052d8bd15fde15cd03916e6521f7ebc11e62ff4a51600316b2d058eb1ccd2a4378fbc6403625599e484d372bf077abbe27a6ebb169b4272add17aa5
|
data/.github/workflows/rake.yml
CHANGED
@@ -16,19 +16,9 @@ jobs:
|
|
16
16
|
strategy:
|
17
17
|
fail-fast: false
|
18
18
|
matrix:
|
19
|
-
ruby: [ '2.7', '2.6', '2.5' ]
|
19
|
+
ruby: [ '3.0', '2.7', '2.6', '2.5' ]
|
20
20
|
os: [ ubuntu-latest, windows-latest, macos-latest ]
|
21
21
|
experimental: [ false ]
|
22
|
-
include:
|
23
|
-
- ruby: '3.0'
|
24
|
-
os: 'ubuntu-latest'
|
25
|
-
experimental: true
|
26
|
-
- ruby: '3.0'
|
27
|
-
os: 'windows-latest'
|
28
|
-
experimental: true
|
29
|
-
- ruby: '3.0'
|
30
|
-
os: 'macos-latest'
|
31
|
-
experimental: true
|
32
22
|
steps:
|
33
23
|
- uses: actions/checkout@v2
|
34
24
|
with:
|
data/lib/relaton/config.rb
CHANGED
@@ -12,14 +12,13 @@ module Relaton
|
|
12
12
|
end
|
13
13
|
|
14
14
|
class Configuration
|
15
|
-
attr_accessor :logs, :use_api
|
15
|
+
attr_accessor :logs, :use_api
|
16
16
|
|
17
17
|
def initialize
|
18
18
|
@logs = %i(info error) # allowed values: :info, :warning, :error, :debug
|
19
19
|
|
20
20
|
# @TODO change to true when we start using api.relaton.org
|
21
|
-
@use_api =
|
22
|
-
@api_host = "https://api.relaton.org/api/v1"
|
21
|
+
@use_api = false
|
23
22
|
end
|
24
23
|
end
|
25
24
|
|
data/lib/relaton/db.rb
CHANGED
@@ -1,14 +1,21 @@
|
|
1
|
+
require "yaml"
|
2
|
+
require_relative "registry"
|
3
|
+
require_relative "db_cache"
|
4
|
+
|
1
5
|
module Relaton
|
6
|
+
class RelatonError < StandardError; end
|
7
|
+
|
2
8
|
class Db
|
3
9
|
# @param global_cache [String] directory of global DB
|
4
10
|
# @param local_cache [String] directory of local DB
|
5
11
|
def initialize(global_cache, local_cache)
|
6
12
|
@registry = Relaton::Registry.instance
|
7
13
|
gpath = global_cache && File.expand_path(global_cache)
|
8
|
-
lpath = local_cache && File.expand_path(local_cache)
|
9
14
|
@db = open_cache_biblio(gpath, type: :global)
|
15
|
+
lpath = local_cache && File.expand_path(local_cache)
|
10
16
|
@local_db = open_cache_biblio(lpath, type: :local)
|
11
|
-
@static_db = open_cache_biblio File.expand_path
|
17
|
+
@static_db = open_cache_biblio File.expand_path("../relaton/static_cache",
|
18
|
+
__dir__)
|
12
19
|
@queues = {}
|
13
20
|
end
|
14
21
|
|
@@ -18,8 +25,10 @@ module Relaton
|
|
18
25
|
# @return [String, nil]
|
19
26
|
def mv(new_dir, type: :global)
|
20
27
|
case type
|
21
|
-
when :global
|
22
|
-
|
28
|
+
when :global
|
29
|
+
@db&.mv new_dir
|
30
|
+
when :local
|
31
|
+
@local_db&.mv new_dir
|
23
32
|
end
|
24
33
|
end
|
25
34
|
|
@@ -80,15 +89,19 @@ module Relaton
|
|
80
89
|
result = @static_db.all do |file, yml|
|
81
90
|
search_yml file, yml, text, edition, year
|
82
91
|
end.compact
|
83
|
-
|
84
|
-
|
92
|
+
db = @db || @local_db
|
93
|
+
if db
|
94
|
+
result += db.all do |file, xml|
|
95
|
+
search_xml file, xml, text, edition, year
|
96
|
+
end.compact
|
85
97
|
end
|
86
98
|
result
|
87
99
|
end
|
88
100
|
|
89
101
|
# Fetch asynchronously
|
90
102
|
def fetch_async(code, year = nil, opts = {}, &_block) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
|
91
|
-
|
103
|
+
stdclass = standard_class code
|
104
|
+
if stdclass
|
92
105
|
unless @queues[stdclass]
|
93
106
|
processor = @registry.processors[stdclass]
|
94
107
|
wp = WorkersPool.new(processor.threads) { |args| yield fetch(*args) }
|
@@ -160,43 +173,14 @@ module Relaton
|
|
160
173
|
def to_xml
|
161
174
|
db = @local_db || @db || return
|
162
175
|
Nokogiri::XML::Builder.new(encoding: "UTF-8") do |xml|
|
163
|
-
xml.documents
|
176
|
+
xml.documents do
|
177
|
+
xml.parent.add_child db.all.join(" ")
|
178
|
+
end
|
164
179
|
end.to_xml
|
165
180
|
end
|
166
181
|
|
167
182
|
private
|
168
183
|
|
169
|
-
#
|
170
|
-
# @param code [String]
|
171
|
-
# @param year [String]
|
172
|
-
#
|
173
|
-
# @param opts [Hash]
|
174
|
-
# @option opts [Boolean] :all_parts If all-parts reference is required
|
175
|
-
# @option opts [Boolean] :keep_year If undated reference should return
|
176
|
-
# actual reference with year
|
177
|
-
#
|
178
|
-
# @param stdclass [Symbol]
|
179
|
-
def fetch_api(code, year, opts, stdclass)
|
180
|
-
return unless Relaton.configuration.use_api
|
181
|
-
|
182
|
-
url = "#{Relaton.configuration.api_host}/document?#{params(code, year, opts)}"
|
183
|
-
rsp = Net::HTTP.get_response URI(url)
|
184
|
-
@registry.processors[stdclass].from_xml rsp.body if rsp.code == "200"
|
185
|
-
end
|
186
|
-
|
187
|
-
#
|
188
|
-
# Make string of parametrs
|
189
|
-
#
|
190
|
-
# @param [String] code
|
191
|
-
# @param [String] year
|
192
|
-
# @param [Hash] opts
|
193
|
-
#
|
194
|
-
# @return [String]
|
195
|
-
#
|
196
|
-
def params(code, year, opts)
|
197
|
-
opts.merge(code: code, year: year).map { |k, v| "#{k}=#{v}" }.join "&"
|
198
|
-
end
|
199
|
-
|
200
184
|
# @param file [String] file path
|
201
185
|
# @param yml [String] content in YAML format
|
202
186
|
# @param text [String, nil] text to serach
|
@@ -223,7 +207,7 @@ module Relaton
|
|
223
207
|
end
|
224
208
|
|
225
209
|
# @param file [String] file path
|
226
|
-
# @param content [String] content in XML or
|
210
|
+
# @param content [String] content in XML or YAmL format
|
227
211
|
# @param edition [String, nil] edition to filter
|
228
212
|
# @param year [Integer, nil] year to filter
|
229
213
|
# @return [BibliographicItem, nil]
|
@@ -236,14 +220,9 @@ module Relaton
|
|
236
220
|
item.date.detect { |d| d.type == "published" && d.on(:year).to_s == year.to_s })
|
237
221
|
end
|
238
222
|
|
239
|
-
#
|
240
|
-
# Look up text in the XML elements attributes and content
|
241
|
-
#
|
242
223
|
# @param xml [String] content in XML format
|
243
224
|
# @param text [String, nil] text to serach
|
244
|
-
#
|
245
225
|
# @return [Boolean]
|
246
|
-
#
|
247
226
|
def match_xml_text(xml, text)
|
248
227
|
%r{((?<attr>=((?<apstr>')|"))|>).*?#{text}.*?(?(<attr>)(?(<apstr>)'|")|<)}mi.match?(xml)
|
249
228
|
end
|
@@ -277,15 +256,19 @@ module Relaton
|
|
277
256
|
end
|
278
257
|
|
279
258
|
doc = @registry.processors[stdclass].hash_to_bib docid: { id: code }
|
280
|
-
|
259
|
+
ref = refs[0]
|
260
|
+
updates = check_bibliocache(ref, year, opts, stdclass)
|
281
261
|
if updates
|
282
|
-
doc.relation << RelatonBib::DocumentRelation.new(bibitem: updates,
|
262
|
+
doc.relation << RelatonBib::DocumentRelation.new(bibitem: updates,
|
263
|
+
type: "updates")
|
283
264
|
end
|
284
265
|
divider = stdclass == :relaton_itu ? " " : "/"
|
285
266
|
refs[1..-1].each_with_object(doc) do |c, d|
|
286
|
-
bib = check_bibliocache(
|
267
|
+
bib = check_bibliocache(ref + divider + c, year, opts, stdclass)
|
287
268
|
if bib
|
288
|
-
d.relation << RelatonBib::DocumentRelation.new(
|
269
|
+
d.relation << RelatonBib::DocumentRelation.new(
|
270
|
+
type: reltype, description: reldesc, bibitem: bib
|
271
|
+
)
|
289
272
|
end
|
290
273
|
end
|
291
274
|
end
|
@@ -297,7 +280,9 @@ module Relaton
|
|
297
280
|
return name if /^(urn:)?#{processor.prefix}/i.match?(code) ||
|
298
281
|
processor.defaultprefix.match(code)
|
299
282
|
end
|
300
|
-
allowed = @registry.processors.reduce([])
|
283
|
+
allowed = @registry.processors.reduce([]) do |m, (_k, v)|
|
284
|
+
m << v.prefix
|
285
|
+
end
|
301
286
|
Util.log <<~WARN, :info
|
302
287
|
[relaton] #{code} does not have a recognised prefix: #{allowed.join(', ')}.
|
303
288
|
See https://github.com/relaton/relaton/ for instructions on prefixing and wrapping document identifiers to disambiguate them.
|
@@ -346,8 +331,8 @@ module Relaton
|
|
346
331
|
# RelatonBipm::BipmBibliographicItem, RelatonIho::IhoBibliographicItem,
|
347
332
|
# RelatonOmg::OmgBibliographicItem, RelatonW3c::W3cBibliographicItem]
|
348
333
|
def bib_retval(entry, stdclass)
|
349
|
-
|
350
|
-
|
334
|
+
if entry.nil? || entry.match?(/^not_found/) then nil
|
335
|
+
else @registry.processors[stdclass].from_xml(entry)
|
351
336
|
end
|
352
337
|
end
|
353
338
|
|
@@ -371,7 +356,8 @@ module Relaton
|
|
371
356
|
# RelatonOmg::OmgBibliographicItem, RelatonW3c::W3cBibliographicItem]
|
372
357
|
def check_bibliocache(code, year, opts, stdclass) # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity
|
373
358
|
id, searchcode = std_id(code, year, opts, stdclass)
|
374
|
-
|
359
|
+
yaml = @static_db[id]
|
360
|
+
if yaml
|
375
361
|
return @registry.processors[stdclass].hash_to_bib YAML.safe_load(yaml)
|
376
362
|
end
|
377
363
|
|
@@ -380,7 +366,8 @@ module Relaton
|
|
380
366
|
if db.nil?
|
381
367
|
return if opts[:fetch_db]
|
382
368
|
|
383
|
-
bibentry = new_bib_entry(searchcode, year, opts, stdclass, db: db,
|
369
|
+
bibentry = new_bib_entry(searchcode, year, opts, stdclass, db: db,
|
370
|
+
id: id)
|
384
371
|
return bib_retval(bibentry, stdclass)
|
385
372
|
end
|
386
373
|
|
@@ -389,17 +376,18 @@ module Relaton
|
|
389
376
|
return bib_retval(altdb[id], stdclass) if opts[:fetch_db]
|
390
377
|
|
391
378
|
db.clone_entry id, altdb if altdb.valid_entry? id, year
|
392
|
-
db[id] ||= new_bib_entry(searchcode, year, opts, stdclass, db: db,
|
379
|
+
db[id] ||= new_bib_entry(searchcode, year, opts, stdclass, db: db,
|
380
|
+
id: id)
|
393
381
|
altdb.clone_entry(id, db) if !altdb.valid_entry?(id, year)
|
394
382
|
else
|
395
383
|
return bib_retval(db[id], stdclass) if opts[:fetch_db]
|
396
384
|
|
397
|
-
db[id] ||= new_bib_entry(searchcode, year, opts, stdclass, db: db,
|
385
|
+
db[id] ||= new_bib_entry(searchcode, year, opts, stdclass, db: db,
|
386
|
+
id: id)
|
398
387
|
end
|
399
388
|
bib_retval(db[id], stdclass)
|
400
389
|
end
|
401
390
|
|
402
|
-
#
|
403
391
|
# @param code [String]
|
404
392
|
# @param year [String]
|
405
393
|
#
|
@@ -412,9 +400,7 @@ module Relaton
|
|
412
400
|
# @param stdclass [Symbol]
|
413
401
|
# @param db [Relaton::DbCache,`NilClass]
|
414
402
|
# @param id [String] docid
|
415
|
-
#
|
416
403
|
# @return [String]
|
417
|
-
#
|
418
404
|
def new_bib_entry(code, year, opts, stdclass, **args) # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity
|
419
405
|
bib = net_retry(code, year, opts, stdclass, opts.fetch(:retries, 1))
|
420
406
|
bib_id = bib&.docidentifier&.first&.id
|
@@ -429,27 +415,8 @@ module Relaton
|
|
429
415
|
end
|
430
416
|
end
|
431
417
|
|
432
|
-
#
|
433
|
-
# @param code [String]
|
434
|
-
# @param year [String]
|
435
|
-
#
|
436
|
-
# @param opts [Hash]
|
437
|
-
# @option opts [Boolean] :all_parts If all-parts reference is required
|
438
|
-
# @option opts [Boolean] :keep_year If undated reference should return
|
439
|
-
# actual reference with year
|
440
|
-
#
|
441
|
-
# @param stdclass [Symbol]
|
442
|
-
# @param retries [Integer] remain Number of network retries
|
443
|
-
#
|
444
418
|
# @raise [RelatonBib::RequestError]
|
445
|
-
# @return [RelatonBib::BibliographicItem]
|
446
|
-
#
|
447
419
|
def net_retry(code, year, opts, stdclass, retries)
|
448
|
-
doc = fetch_api code, year, opts, stdclass
|
449
|
-
return doc if doc
|
450
|
-
|
451
|
-
@registry.processors[stdclass].get(code, year, opts)
|
452
|
-
rescue Errno::ECONNREFUSED
|
453
420
|
@registry.processors[stdclass].get(code, year, opts)
|
454
421
|
rescue RelatonBib::RequestError => e
|
455
422
|
raise e unless retries > 1
|
@@ -467,7 +434,11 @@ module Relaton
|
|
467
434
|
# RelatonOmg::OmgBibliographicItem, RelatonW3c::W3cBibliographicItem]
|
468
435
|
# @return [String] XML or "not_found mm-dd-yyyy"
|
469
436
|
def bib_entry(bib)
|
470
|
-
bib.respond_to?
|
437
|
+
if bib.respond_to? :to_xml
|
438
|
+
bib.to_xml(bibdata: true)
|
439
|
+
else
|
440
|
+
"not_found #{Date.today}"
|
441
|
+
end
|
471
442
|
end
|
472
443
|
|
473
444
|
# @param dir [String, nil] DB directory
|
@@ -485,7 +456,8 @@ module Relaton
|
|
485
456
|
FileUtils.rm_rf(fdir, secure: true)
|
486
457
|
Util.log(
|
487
458
|
"[relaton] WARNING: cache #{fdir}: version is obsolete and cache is "\
|
488
|
-
"cleared.",
|
459
|
+
"cleared.",
|
460
|
+
:warning
|
489
461
|
)
|
490
462
|
end
|
491
463
|
db
|
@@ -522,7 +494,7 @@ module Relaton
|
|
522
494
|
end
|
523
495
|
|
524
496
|
def local_bibliocache_name(cachename)
|
525
|
-
cachename = "relaton" if cachename.empty?
|
497
|
+
cachename = "relaton" if cachename.nil? || cachename.empty?
|
526
498
|
"#{cachename}/cache"
|
527
499
|
end
|
528
500
|
end
|
data/lib/relaton/db_cache.rb
CHANGED
@@ -10,7 +10,9 @@ module Relaton
|
|
10
10
|
def initialize(dir, ext = "xml")
|
11
11
|
@dir = dir
|
12
12
|
@ext = ext
|
13
|
-
FileUtils::mkdir_p dir
|
13
|
+
FileUtils::mkdir_p @dir
|
14
|
+
# file_version = "#{@dir}/version"
|
15
|
+
# set_version # unless File.exist? file_version
|
14
16
|
end
|
15
17
|
|
16
18
|
# Move caches to anothe dir
|
@@ -30,7 +32,7 @@ module Relaton
|
|
30
32
|
|
31
33
|
# Clear database
|
32
34
|
def clear
|
33
|
-
FileUtils.rm_rf Dir.glob "#{dir}/*" if @ext == "xml" #
|
35
|
+
FileUtils.rm_rf Dir.glob "#{dir}/*" if @ext == "xml" # unless it's static DB
|
34
36
|
end
|
35
37
|
|
36
38
|
# Save item
|
@@ -41,13 +43,23 @@ module Relaton
|
|
41
43
|
delete key
|
42
44
|
return
|
43
45
|
end
|
44
|
-
|
45
|
-
prefix_dir = "#{@dir}/#{
|
46
|
+
|
47
|
+
prefix_dir = "#{@dir}/#{prefix(key)}"
|
46
48
|
FileUtils::mkdir_p prefix_dir unless Dir.exist? prefix_dir
|
47
49
|
set_version prefix_dir
|
48
50
|
file_safe_write "#{filename(key)}.#{ext(value)}", value
|
49
51
|
end
|
50
52
|
|
53
|
+
# @param value [String]
|
54
|
+
# @return [String]
|
55
|
+
def ext(value)
|
56
|
+
case value
|
57
|
+
when /^not_found/ then "notfound"
|
58
|
+
when /^redirection/ then "redirect"
|
59
|
+
else @ext
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
51
63
|
# Read item
|
52
64
|
# @param key [String]
|
53
65
|
# @return [String]
|
@@ -74,7 +86,7 @@ module Relaton
|
|
74
86
|
value = self[key]
|
75
87
|
return unless value
|
76
88
|
|
77
|
-
if value.match?
|
89
|
+
if value.match? /^not_found/
|
78
90
|
value.match(/\d{4}-\d{2}-\d{2}/).to_s
|
79
91
|
else
|
80
92
|
doc = Nokogiri::XML value
|
@@ -84,10 +96,10 @@ module Relaton
|
|
84
96
|
|
85
97
|
# Returns all items
|
86
98
|
# @return [Array<String>]
|
87
|
-
def all
|
99
|
+
def all
|
88
100
|
Dir.glob("#{@dir}/**/*.{xml,yml,yaml}").sort.map do |f|
|
89
101
|
content = File.read(f, encoding: "utf-8")
|
90
|
-
|
102
|
+
block_given? ? yield(f, content) : content
|
91
103
|
end
|
92
104
|
end
|
93
105
|
|
@@ -95,19 +107,30 @@ module Relaton
|
|
95
107
|
# @param key [String]
|
96
108
|
def delete(key)
|
97
109
|
file = filename key
|
98
|
-
f = search_ext
|
110
|
+
f = search_ext(file)
|
99
111
|
File.delete f if f
|
100
112
|
end
|
101
113
|
|
102
114
|
# Check if version of the DB match to the gem grammar hash.
|
103
115
|
# @param fdir [String] dir pathe to flover cache
|
104
|
-
# @return [
|
116
|
+
# @return [TrueClass, FalseClass]
|
105
117
|
def check_version?(fdir)
|
106
|
-
|
107
|
-
return false unless File.exist?
|
118
|
+
version_dir = fdir + "/version"
|
119
|
+
return false unless File.exist? version_dir
|
108
120
|
|
109
|
-
v = File.read
|
110
|
-
v.strip ==
|
121
|
+
v = File.read version_dir, encoding: "utf-8"
|
122
|
+
v.strip == grammar_hash(fdir)
|
123
|
+
end
|
124
|
+
|
125
|
+
# Set version of the DB to the gem grammar hash.
|
126
|
+
# @param fdir [String] dir pathe to flover cache
|
127
|
+
# @return [Relaton::DbCache]
|
128
|
+
def set_version(fdir)
|
129
|
+
file_version = "#{fdir}/version"
|
130
|
+
unless File.exist? file_version
|
131
|
+
file_safe_write file_version, grammar_hash(fdir)
|
132
|
+
end
|
133
|
+
self
|
111
134
|
end
|
112
135
|
|
113
136
|
# if cached reference is undated, expire it after 60 days
|
@@ -121,6 +144,15 @@ module Relaton
|
|
121
144
|
year || Date.today - date < 60
|
122
145
|
end
|
123
146
|
|
147
|
+
protected
|
148
|
+
|
149
|
+
# @param fdir [String] dir pathe to flover cache
|
150
|
+
# @return [String]
|
151
|
+
def grammar_hash(fdir)
|
152
|
+
type = fdir.split("/").last
|
153
|
+
Relaton::Registry.instance.by_type(type)&.grammar_hash
|
154
|
+
end
|
155
|
+
|
124
156
|
# Reads file by a key
|
125
157
|
#
|
126
158
|
# @param key [String]
|
@@ -132,23 +164,28 @@ module Relaton
|
|
132
164
|
File.read(f, encoding: "utf-8")
|
133
165
|
end
|
134
166
|
|
135
|
-
# @param fdir [String] dir pathe to flover cache
|
136
|
-
# @return [String]
|
137
|
-
def self.grammar_hash(fdir)
|
138
|
-
type = fdir.split("/").last
|
139
|
-
Relaton::Registry.instance.by_type(type)&.grammar_hash
|
140
|
-
end
|
141
|
-
|
142
167
|
private
|
143
168
|
|
144
|
-
#
|
169
|
+
# Check if a file content is redirection
|
170
|
+
#
|
171
|
+
# @prarm value [String] file content
|
172
|
+
# @return [String, NilClass] redirection code or nil
|
173
|
+
def redirect?(value)
|
174
|
+
%r{redirection\s(?<code>.*)} =~ value
|
175
|
+
code
|
176
|
+
end
|
177
|
+
|
178
|
+
# Return item's file name
|
179
|
+
# @param key [String]
|
145
180
|
# @return [String]
|
146
|
-
def
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
181
|
+
def filename(key)
|
182
|
+
prefcode = key.downcase.match /^(?<prefix>[^\(]+)\((?<code>[^\)]+)/
|
183
|
+
fn = if prefcode
|
184
|
+
"#{prefcode[:prefix]}/#{prefcode[:code].gsub(/[-:\s\/\()]/, '_').squeeze('_')}"
|
185
|
+
else
|
186
|
+
key.gsub(/[-:\s]/, "_")
|
187
|
+
end
|
188
|
+
"#{@dir}/#{fn.sub(/(,|_$)/, '')}"
|
152
189
|
end
|
153
190
|
|
154
191
|
#
|
@@ -167,36 +204,11 @@ module Relaton
|
|
167
204
|
end
|
168
205
|
end
|
169
206
|
|
170
|
-
#
|
171
|
-
# @param fdir [String] dir pathe to flover cache
|
172
|
-
def set_version(fdir)
|
173
|
-
file_version = "#{fdir}/version"
|
174
|
-
unless File.exist? file_version
|
175
|
-
file_safe_write file_version, self.class.grammar_hash(fdir)
|
176
|
-
end
|
177
|
-
end
|
178
|
-
|
179
|
-
# Return item's file name
|
207
|
+
# Return item's subdir
|
180
208
|
# @param key [String]
|
181
209
|
# @return [String]
|
182
|
-
def
|
183
|
-
|
184
|
-
fn = if prefcode
|
185
|
-
"#{prefcode[:prefix]}/#{prefcode[:code].gsub(/[-:\s\/()]/, '_')
|
186
|
-
.squeeze('_')}"
|
187
|
-
else
|
188
|
-
key.gsub(/[-:\s]/, "_")
|
189
|
-
end
|
190
|
-
"#{@dir}/#{fn.sub(/(,|_$)/, '')}"
|
191
|
-
end
|
192
|
-
|
193
|
-
# Check if a file content is redirection
|
194
|
-
#
|
195
|
-
# @prarm value [String] file content
|
196
|
-
# @return [String, NilClass] redirection code or nil
|
197
|
-
def redirect?(value)
|
198
|
-
%r{redirection\s(?<code>.*)} =~ value
|
199
|
-
code
|
210
|
+
def prefix(key)
|
211
|
+
key.downcase.match(/^[^\(]+(?=\()/).to_s
|
200
212
|
end
|
201
213
|
|
202
214
|
# @param file [String]
|
@@ -207,5 +219,37 @@ module Relaton
|
|
207
219
|
f.write content
|
208
220
|
end
|
209
221
|
end
|
222
|
+
|
223
|
+
class << self
|
224
|
+
private
|
225
|
+
|
226
|
+
def global_bibliocache_name
|
227
|
+
"#{Dir.home}/.relaton/cache"
|
228
|
+
end
|
229
|
+
|
230
|
+
def local_bibliocache_name(cachename)
|
231
|
+
return nil if cachename.nil?
|
232
|
+
|
233
|
+
cachename = "relaton" if cachename.empty?
|
234
|
+
"#{cachename}/cache"
|
235
|
+
end
|
236
|
+
|
237
|
+
public
|
238
|
+
|
239
|
+
# Initialse and return relaton instance, with local and global cache names
|
240
|
+
# local_cache: local cache name; none created if nil; "relaton" created
|
241
|
+
# if empty global_cache: boolean to create global_cache
|
242
|
+
# flush_caches: flush caches
|
243
|
+
def init_bib_caches(opts) # rubocop:disable Metrics/CyclomaticComplexity
|
244
|
+
globalname = global_bibliocache_name if opts[:global_cache]
|
245
|
+
localname = local_bibliocache_name(opts[:local_cache])
|
246
|
+
localname = "relaton" if localname&.empty?
|
247
|
+
if opts[:flush_caches]
|
248
|
+
FileUtils.rm_rf globalname unless globalname.nil?
|
249
|
+
FileUtils.rm_rf localname unless localname.nil?
|
250
|
+
end
|
251
|
+
Relaton::Db.new(globalname, localname)
|
252
|
+
end
|
253
|
+
end
|
210
254
|
end
|
211
255
|
end
|