relaton-index 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 39ea35c85bb825679fc73792345d3c11217e80972f2eb32c1a4cc5b31b1a41de
4
- data.tar.gz: eedc5afbe9b1fcbd76763d26b628b510e29a9f0a9c74d1a2bb4ab554b0ffb0f0
3
+ metadata.gz: bc21a3161ae2d23d6b2178a5947d07c83dd1758f6db4e093340b90f525968edd
4
+ data.tar.gz: 8bf654f9260aff06bb3be34e6b14a35adf1280e5d90085a57fc3c608db976b94
5
5
  SHA512:
6
- metadata.gz: e420ccb8c3d7eaa8834d02c901b0ff495ec6f16552fb614b81905d15758228553eb399234b2575a6bf905931dc96c11491f90082355c28f5be8ba3fa2a0edb44
7
- data.tar.gz: 63971b59580a47e70782a0e6a1b98acb270953b415ed451f2b634a5baebafbc46ea7ca08b83a5a76f5110e95d96af27ba64ab8a63fbc6feb850118553b4b6611
6
+ metadata.gz: faf0f893ec70ae6c5186e6fa2afe5619d9ab4757d6fef73f1637163690a1a371df999e4d85a5af896e8ae2bae479d690891bedc704f5227aa55d52a02f582217
7
+ data.tar.gz: b9cf08da7a80849861da0c999d0d603d0b8c1466b93ed0e898d1bdb670bb5e49eb82ce592ba75d0d276d28d0309c3e85cccf5d317263791b16784132b55da8cf
data/README.adoc CHANGED
@@ -22,12 +22,28 @@ If bundler is not being used to manage dependencies, install the gem by executin
22
22
 
23
23
  == Usage
24
24
 
25
+ === Creating an index object
26
+
27
+ The gem provides the `Relaton::Index.find_or_create {type}, url: {url}, file: {filename}, id_keys: {keys}` method to create an index object. The first argument is the type of dataset (ISO, IEC, IHO, etc.). The second argument is the URL to the zipped remote index file. The third argument is the filename of the local index file. The fourth argument is an array of ID's parts names. The URL, filename, and keys are optional.
28
+
29
+ If the URL is specified and the local file in a `/{home}/.relaton/{type}` dir doesn't exist or is outdated, the index file will be downloaded from the URL saved as a local file and an index object will be created from the file. If the file in the `/{home}/.relaton/{type}` exists and is actual, the index object will be created from the local file.
30
+
31
+ If the URL isn't specified, the index object will be created from the local file in the current dir `./{filename}`. If the file doesn't exist, the empty index object will be created.
32
+
33
+ If the filename isn't specified, a default `index,yaml` filename will be used.
34
+
35
+ If the keys are specified, the local index file will be checked for the presence of the keys. If the keys are not found, the index file will be downloaded from the URL or an empty index object will be created if the URL isn't specified.
36
+
37
+ It's possible to create many index objects for different types of datasets. The index objects are saved in the pool. The pool is a hash with the type of dataset as a key and the index object as a value. The pool is a class variable of the `Relaton::Index` class. The pool is used to find an existing index object by the type of dataset. The `Relaton::Index.find_or_create` method returns an existing index object if it was created before.
38
+
39
+ If the URL or filename is specified and has a new value, different from what was used to create the index object before, the index object will be recreated.
40
+
25
41
  [source,ruby]
26
42
  ----
27
43
  require 'relaton/index'
28
44
 
29
45
  # Create a new index object. The first argument is the type of dataset (ISO, IEC, IHO, etc.) URL and filename are optional.
30
- Relaton::Index.find_or_create :IHO, url: "https://raw.githubusercontent.com/relaton/relaton-data-iho/master/index.zip", filename: "index-iho.yaml"
46
+ Relaton::Index.find_or_create :IHO, url: "https://raw.githubusercontent.com/relaton/relaton-data-iho/master/index.zip", file: "index-iho.yaml", id_keys: %i[number part year]
31
47
 
32
48
  # Find an existing index object (created before).
33
49
  Relaton::Index.find_or_create :IHO
@@ -48,7 +64,7 @@ The aim of this gem is to be used by Relaton libraries in two ways:
48
64
  In this case, the Relaton library creates an index object and adds documents' files to it. By default, the index object is saved to the `index.yaml` file in the root of the repository. The filename can be changed using the `filename` setting.
49
65
 
50
66
  [source,ruby]
51
- ----
67
+ ----
52
68
  # Create a new index object or fetch an existing one. The first argument is the type of dataset (ISO, IEC, IHO, etc.) URL should not be specified.
53
69
  index = Relaton::Index.find_or_create :IHO
54
70
 
@@ -69,16 +85,16 @@ In this case, the Relaton library should create an index object and search for a
69
85
  index = Relaton::Index.find_or_create :IHO, url: "https://raw.githubusercontent.com/relaton/relaton-data-iho/master/index.zip"
70
86
 
71
87
 
72
- # Search for a document by reference
73
- index.search "B-4"
74
- # => [{ id: "B-4 2.19.0", file: "data/b-4_2_19_0.xml" }]
88
+ # Search for a document by reference
89
+ index.search "B-4"
90
+ # => [{ id: "B-4 2.19.0", file: "data/b-4_2_19_0.xml" }]
75
91
 
76
92
  # Search for a document by reference using a block
77
93
  index.search do |row|
78
94
  # do something with the index row
79
95
  row[:id] == "B-4"
80
96
  end
81
- # => [{ id: "B-4 2.19.0", file: "data/b-4_2_19_0.xml" }]
97
+ # => [{ id: "B-4 2.19.0", file: "data/b-4_2_19_0.xml" }]
82
98
  ----
83
99
 
84
100
  === Remove all index records
@@ -110,7 +126,7 @@ The gem can be configured by using the `Relaton::Index.config` method. The follo
110
126
  [source,ruby]
111
127
  ----
112
128
  Relaton::Index.config do |config|
113
- config.filename = "index-new.yaml"
129
+ config.filename = "index-v1.yaml"
114
130
  config.storage = S3Storage
115
131
  config.storage_dir = "/"
116
132
  end
@@ -119,7 +135,7 @@ end
119
135
  It's also possible to redefine file name for a specific type of index:
120
136
  [source,ruby]
121
137
  ----
122
- index = Relaton::Index.find_or_create :IHO, filename: "index-iho.yaml"
138
+ index = Relaton::Index.find_or_create :IHO, file: "index-v2.yaml"
123
139
  ----
124
140
 
125
141
  == Development
@@ -18,10 +18,11 @@ module Relaton
18
18
  # if true then the index is read from the storage (used to remove index file)
19
19
  # if nil then the fiename is used to read and write file (used to create indes in GH actions)
20
20
  #
21
- def initialize(dir, url, filename)
21
+ def initialize(dir, url, filename, id_keys)
22
22
  @dir = dir
23
23
  @url = url
24
24
  @filename = filename
25
+ @id_keys = id_keys || []
25
26
  end
26
27
 
27
28
  #
@@ -67,6 +68,24 @@ module Relaton
67
68
  read_file
68
69
  end
69
70
 
71
+ #
72
+ # Check if index has correct format
73
+ #
74
+ # @param [Array<Hash>] index index to check
75
+ #
76
+ # @return [Boolean] <description>
77
+ #
78
+ def check_format(index) # rubocop:disable Metrics/AbcSize
79
+ keys = index.reduce(Set.new) do |acc, item|
80
+ if item[:id].is_a?(Hash) && @id_keys.any?
81
+ acc + item.keys + item[:id].keys
82
+ else
83
+ acc + item.keys
84
+ end
85
+ end
86
+ keys == [:id, :file, *@id_keys].to_set
87
+ end
88
+
70
89
  #
71
90
  # Read index from storage
72
91
  #
@@ -76,7 +95,8 @@ module Relaton
76
95
  yaml = Index.config.storage.read(file)
77
96
  return unless yaml
78
97
 
79
- YAML.safe_load yaml, permitted_classes: [Symbol]
98
+ index = YAML.safe_load yaml, permitted_classes: [Symbol]
99
+ return index if check_format index
80
100
  rescue Psych::SyntaxError
81
101
  end
82
102
 
@@ -14,6 +14,7 @@ module Relaton
14
14
  # @param [String] type <description>
15
15
  # @param [String, nil] url external URL to index, used to fetch index for searching files
16
16
  # @param [String, nil] file output file name
17
+ # @param [Array<Symbol>, nil] id_keys keys to check if index is correct
17
18
  #
18
19
  # @return [Relaton::Index::Type] typed index
19
20
  #
@@ -21,7 +22,7 @@ module Relaton
21
22
  if @pool[type.upcase.to_sym]&.actual?(**args)
22
23
  @pool[type.upcase.to_sym]
23
24
  else
24
- @pool[type.upcase.to_sym] = Type.new(type, args[:url], args[:file])
25
+ @pool[type.upcase.to_sym] = Type.new(type, args[:url], args[:file], args[:id_keys])
25
26
  end
26
27
  end
27
28
 
@@ -11,10 +11,10 @@ module Relaton
11
11
  # @param [String, nil] url external URL to index, used to fetch index for searching files
12
12
  # @param [String, nil] file output file name
13
13
  #
14
- def initialize(type, url = nil, file = nil)
14
+ def initialize(type, url = nil, file = nil, id_keys = nil)
15
15
  @file = file
16
16
  filename = file || Index.config.filename
17
- @file_io = FileIO.new type.to_s.downcase, url, filename
17
+ @file_io = FileIO.new type.to_s.downcase, url, filename, id_keys
18
18
  end
19
19
 
20
20
  def index
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Relaton
4
4
  module Index
5
- VERSION = "0.2.1"
5
+ VERSION = "0.2.2"
6
6
  end
7
7
  end
data/lib/relaton/index.rb CHANGED
@@ -21,11 +21,12 @@ module Relaton
21
21
  # @param [String] type index type (ISO, IEC, etc.)
22
22
  # @param [String, nil] url external URL to index, used to fetch index for searching files
23
23
  # @param [String, nil] file output file name, default is config.filename
24
+ # @param [Array<Symbol>, nil] id_keys keys to check if index is correct
24
25
  #
25
26
  # @return [Relaton::Index::Type] typed index
26
27
  #
27
- def find_or_create(type, url: nil, file: nil)
28
- pool.type(type, url: url, file: file)
28
+ def find_or_create(type, url: nil, file: nil, id_keys: nil)
29
+ pool.type(type, url: url, file: file, id_keys: id_keys)
29
30
  end
30
31
 
31
32
  def close(type)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: relaton-index
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ribose Inc.
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-06-10 00:00:00.000000000 Z
11
+ date: 2023-06-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rubyzip