relaton 1.8.pre4 → 1.8.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/rake.yml +1 -1
  3. data/.rubocop.yml +1 -1
  4. data/lib/relaton/config.rb +2 -3
  5. data/lib/relaton/db.rb +53 -110
  6. data/lib/relaton/db_cache.rb +99 -55
  7. data/lib/relaton/registry.rb +0 -1
  8. data/lib/relaton/version.rb +1 -1
  9. data/lib/relaton.rb +0 -1
  10. data/relaton.gemspec +3 -6
  11. data/spec/relaton/db_cache_spec.rb +2 -2
  12. data/spec/relaton/db_spec.rb +0 -17
  13. data/spec/relaton/regirtry_spec.rb +1 -9
  14. data/spec/relaton_spec.rb +4 -45
  15. data/spec/spec_helper.rb +0 -4
  16. data/spec/vcr_cassetes/19133_2005.yml +18 -18
  17. data/spec/vcr_cassetes/async_fetch.yml +1249 -1361
  18. data/spec/vcr_cassetes/bsi_bs_en_iso_8848.yml +17 -17
  19. data/spec/vcr_cassetes/cc_dir_10005_2019.yml +20 -20
  20. data/spec/vcr_cassetes/cie_001_1980.yml +7 -7
  21. data/spec/vcr_cassetes/ecma_6.yml +7 -7
  22. data/spec/vcr_cassetes/fisp_140.yml +10 -8
  23. data/spec/vcr_cassetes/gb_t_20223_2006.yml +8 -8
  24. data/spec/vcr_cassetes/iec_60050_102_2007.yml +27 -27
  25. data/spec/vcr_cassetes/iec_combined_included.yml +89 -89
  26. data/spec/vcr_cassetes/ieee_528_2019.yml +38 -38
  27. data/spec/vcr_cassetes/iho_b_11.yml +7 -7
  28. data/spec/vcr_cassetes/iso_111111119115_1.yml +4 -4
  29. data/spec/vcr_cassetes/iso_19115.yml +17 -17
  30. data/spec/vcr_cassetes/iso_19115_1.yml +18 -18
  31. data/spec/vcr_cassetes/iso_19115_1_2.yml +34 -34
  32. data/spec/vcr_cassetes/iso_awi_14093.yml +18 -18
  33. data/spec/vcr_cassetes/iso_combined_applied.yml +34 -34
  34. data/spec/vcr_cassetes/iso_combined_included.yml +33 -33
  35. data/spec/vcr_cassetes/itu_combined_included.yml +147 -147
  36. data/spec/vcr_cassetes/ogc_19_025r1.yml +1165 -1239
  37. data/spec/vcr_cassetes/omg_ami4ccm_1_0.yml +4 -4
  38. data/spec/vcr_cassetes/rfc_8341.yml +7 -7
  39. data/spec/vcr_cassetes/sp_800_38b.yml +10 -8
  40. data/spec/vcr_cassetes/un_rtade_cefact_2004_32.yml +31 -29
  41. data/spec/vcr_cassetes/w3c_json_ld11.yml +12 -12
  42. metadata +11 -56
  43. data/spec/vcr_cassetes/api_relaton_org.yml +0 -51
  44. data/spec/vcr_cassetes/api_relaton_org_unavailable.yml +0 -182
  45. data/spec/vcr_cassetes/cen_en_10160_1999.yml +0 -249
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e0872533d5f93e35d777c6a84a4f38a5698b64cc00d9d103b77f7e10fc34738b
4
- data.tar.gz: 3f1cc128d958996cd8ea6eb31bacc530bba464a8d8b5cb855cd4e204214ef7d1
3
+ metadata.gz: da9850bded053378f5adfddf9df94595b2fc5f7bf332a683c6ef179ffc3e8526
4
+ data.tar.gz: 024d0ed9038f3d3cfd71d31e69037f22c7fe6a2bfcdb51b6ab3f0a1d83e4c919
5
5
  SHA512:
6
- metadata.gz: 1862b4eb76509da3ae2ac959d1ffd7bf6e785bc45033c5123236f5c3f9dc5cafc42940a7cba78222960a59ad542fb0786f2a0be8e5da926bbb4b3aa886278a5e
7
- data.tar.gz: b21c4797a9003696de1e5d79f017d19c417a9db72e549eeaab7edaa935e26ce9f1b009fdea83bf8896100ed30c63d5b7df730fbeaacedfc91243468cbc54a39b
6
+ metadata.gz: 2b3222bf273ef4b5391394c85754d1feaacc123c420f4ee868b1f76b930f62f9532b9970d3e40de64127a1f743a27ad6d5ed0f473fc4b6269a144a930d500555
7
+ data.tar.gz: 95895e7ab8061bf0d492efaf02b0f5c049b37bef5c7c21617a477bcd122d00d2a381fd05427856266a87631917a07f08a008518d0692141ce7aefede8d1e78e0
@@ -16,7 +16,7 @@ jobs:
16
16
  strategy:
17
17
  fail-fast: false
18
18
  matrix:
19
- ruby: [ '2.7', '2.6', '2.5' ]
19
+ ruby: [ '2.7', '2.6', '2.5', '2.4' ]
20
20
  os: [ ubuntu-latest, windows-latest, macos-latest ]
21
21
  experimental: [ false ]
22
22
  include:
data/.rubocop.yml CHANGED
@@ -5,6 +5,6 @@
5
5
  inherit_from:
6
6
  - https://raw.githubusercontent.com/riboseinc/oss-guides/master/ci/rubocop.yml
7
7
  AllCops:
8
- TargetRubyVersion: 2.5
8
+ TargetRubyVersion: 2.4
9
9
  Rails:
10
10
  Enabled: false
@@ -12,14 +12,13 @@ module Relaton
12
12
  end
13
13
 
14
14
  class Configuration
15
- attr_accessor :logs, :use_api, :api_host
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 = true
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 "../relaton/static_cache", __dir__
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 then @db&.mv new_dir
22
- when :local then @local_db&.mv new_dir
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,18 +89,22 @@ 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
- if (db = @db || @local_db)
84
- result += db.all { |f, x| search_xml f, x, text, edition, year }.compact
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
- if (stdclass = standard_class code)
103
+ stdclass = standard_class code
104
+ if stdclass
92
105
  unless @queues[stdclass]
93
106
  processor = @registry.processors[stdclass]
94
- wp = WorkersPool.new(processor.threads) { |args| yield fetch(*args) }
107
+ wp = WorkersPool.new(processor.threads) { |args| yield fetch *args }
95
108
  @queues[stdclass] = { queue: Queue.new, workers_pool: wp }
96
109
  Thread.new { process_queue @queues[stdclass] }
97
110
  end
@@ -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 { xml.parent.add_child db.all.join(" ") }
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 YAML format
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
- updates = check_bibliocache(refs[0], year, opts, stdclass)
259
+ ref = refs[0]
260
+ updates = check_bibliocache(ref, year, opts, stdclass)
281
261
  if updates
282
- doc.relation << RelatonBib::DocumentRelation.new(bibitem: updates, type: "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(refs[0] + divider + c, year, opts, stdclass)
267
+ bib = check_bibliocache(ref + divider + c, year, opts, stdclass)
287
268
  if bib
288
- d.relation << RelatonBib::DocumentRelation.new(type: reltype, description: reldesc, bibitem: bib)
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([]) { |m, (_k, v)| m << v.prefix }
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
- unless entry.nil? || entry.match?(/^not_found/)
350
- @registry.processors[stdclass].from_xml(entry)
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
- if (yaml = @static_db[id])
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, id: id)
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, id: id)
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, id: id)
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?(:to_xml) ? bib.to_xml(bibdata: true) : "not_found #{Date.today}"
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.", :warning
459
+ "cleared.",
460
+ :warning
489
461
  )
490
462
  end
491
463
  db
@@ -497,34 +469,5 @@ module Relaton
497
469
  def process_queue(qwp)
498
470
  while args = qwp[:queue].pop; qwp[:workers_pool] << args end
499
471
  end
500
-
501
- class << self
502
- # Initialse and return relaton instance, with local and global cache names
503
- # local_cache: local cache name; none created if nil; "relaton" created
504
- # if empty global_cache: boolean to create global_cache
505
- # flush_caches: flush caches
506
- def init_bib_caches(**opts) # rubocop:disable Metrics/CyclomaticComplexity
507
- globalname = global_bibliocache_name if opts[:global_cache]
508
- localname = local_bibliocache_name(opts[:local_cache])
509
- flush_caches globalname, localname if opts[:flush_caches]
510
- Relaton::Db.new(globalname, localname)
511
- end
512
-
513
- private
514
-
515
- def flush_caches(gcache, lcache)
516
- FileUtils.rm_rf gcache unless gcache.nil?
517
- FileUtils.rm_rf lcache unless lcache.nil?
518
- end
519
-
520
- def global_bibliocache_name
521
- "#{Dir.home}/.relaton/cache"
522
- end
523
-
524
- def local_bibliocache_name(cachename)
525
- cachename = "relaton" if cachename.empty?
526
- "#{cachename}/cache"
527
- end
528
- end
529
472
  end
530
473
  end
@@ -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" # if it isn't a static DB
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
- /^(?<pref>[^(]+)(?=\()/ =~ key.downcase
45
- prefix_dir = "#{@dir}/#{pref}"
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?(/^not_found/)
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(&block)
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
- block ? yield(f, content) : content
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 file
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 [Boolean]
116
+ # @return [TrueClass, FalseClass]
105
117
  def check_version?(fdir)
106
- version_file = "#{fdir}/version"
107
- return false unless File.exist? version_file
118
+ version_dir = fdir + "/version"
119
+ return false unless File.exist? version_dir
108
120
 
109
- v = File.read version_file, encoding: "utf-8"
110
- v.strip == self.class.grammar_hash(fdir)
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
- # @param value [String]
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 ext(value)
147
- case value
148
- when /^not_found/ then "notfound"
149
- when /^redirection/ then "redirect"
150
- else @ext
151
- end
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
- # Set version of the DB to the gem grammar hash.
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 filename(key)
183
- prefcode = key.downcase.match(/^(?<prefix>[^(]+)\((?<code>[^)]+)/)
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
@@ -9,7 +9,6 @@ module Relaton
9
9
  relaton_gb relaton_iec relaton_ietf relaton_iso relaton_itu relaton_nist
10
10
  relaton_ogc relaton_calconnect relaton_omg relaton_un relaton_w3c
11
11
  relaton_ieee relaton_iho relaton_bipm relaton_ecma relaton_cie relaton_bsi
12
- relaton_cen
13
12
  ].freeze
14
13
 
15
14
  include Singleton
@@ -1,3 +1,3 @@
1
1
  module Relaton
2
- VERSION = "1.8.pre4".freeze
2
+ VERSION = "1.8.0".freeze
3
3
  end
data/lib/relaton.rb CHANGED
@@ -1,6 +1,5 @@
1
1
  require "relaton/util"
2
2
  require "relaton/config"
3
- require "yaml"
4
3
 
5
4
  require "relaton/workers_pool"
6
5
  require "relaton/db"
data/relaton.gemspec CHANGED
@@ -27,13 +27,12 @@ Gem::Specification.new do |spec|
27
27
  spec.require_paths = ["lib"]
28
28
  spec.files = `git ls-files`.split("\n")
29
29
  spec.test_files = `git ls-files -- {spec}/*`.split("\n")
30
- spec.required_ruby_version = Gem::Requirement.new(">= 2.5.0")
30
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.4.0")
31
31
 
32
32
  # spec.add_dependency "algoliasearch"
33
33
  spec.add_dependency "relaton-bipm", "~> 1.8.0"
34
34
  spec.add_dependency "relaton-bsi", "~> 1.8.0"
35
35
  spec.add_dependency "relaton-calconnect", "~> 1.8.0"
36
- spec.add_dependency "relaton-cen", "~> 1.8.pre1"
37
36
  spec.add_dependency "relaton-cie", "~> 1.8.0"
38
37
  spec.add_dependency "relaton-ecma", "~> 1.8.0"
39
38
  spec.add_dependency "relaton-gb", "~> 1.8.0"
@@ -42,7 +41,7 @@ Gem::Specification.new do |spec|
42
41
  spec.add_dependency "relaton-ietf", "~> 1.8.0"
43
42
  spec.add_dependency "relaton-iho", "~> 1.8.0"
44
43
  spec.add_dependency "relaton-iso", ">= 1.8.0"
45
- spec.add_dependency "relaton-itu", ">= 1.8.1"
44
+ spec.add_dependency "relaton-itu", ">= 1.8.0"
46
45
  spec.add_dependency "relaton-nist", ">= 1.8.0"
47
46
  spec.add_dependency "relaton-ogc", "~> 1.8.0"
48
47
  spec.add_dependency "relaton-omg", "~> 1.8.0"
@@ -57,9 +56,7 @@ Gem::Specification.new do |spec|
57
56
  spec.add_development_dependency "pry-byebug", "~> 3.9.0"
58
57
  spec.add_development_dependency "rake", "~> 13.0"
59
58
  spec.add_development_dependency "rspec", "~> 3.6"
60
- spec.add_development_dependency "rubocop", "~> 1.17.0"
61
- spec.add_development_dependency "rubocop-performance", "~> 1.11.0"
62
- spec.add_development_dependency "rubocop-rails", "~> 2.10.0"
59
+ spec.add_development_dependency "rubocop", "= 0.54.0"
63
60
  # spec.add_development_dependency "ruby-debug-ide"
64
61
  spec.add_development_dependency "simplecov", "~> 0.15"
65
62
  spec.add_development_dependency "timecop", "~> 0.9"
@@ -6,8 +6,8 @@ RSpec.describe Relaton::DbCache do
6
6
  cache_path = File.expand_path("~/.relaton/cache")
7
7
  FileUtils.mv cache_path, "relaton1/cache", force: true
8
8
  FileUtils.rm_rf %w(relaton)
9
- Relaton::Db.init_bib_caches(
10
- global_cache: true, local_cache: "", flush_caches: true,
9
+ Relaton::DbCache.init_bib_caches(
10
+ global_cache: true, local_cache: "", flush_caches: true
11
11
  )
12
12
  expect(File.exist?(cache_path)).to be true
13
13
  expect(File.exist?("relaton")).to be true