relaton-index 0.2.0 → 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: 0eaf31518d3ecfb78041891165d24d8f0e9673d2aebcfa02608694ec9f562843
4
- data.tar.gz: 206c0f5fcf0b5336ff186f35da3efc79fda86cf25c93f1c499dd525cccdd6128
3
+ metadata.gz: bc21a3161ae2d23d6b2178a5947d07c83dd1758f6db4e093340b90f525968edd
4
+ data.tar.gz: 8bf654f9260aff06bb3be34e6b14a35adf1280e5d90085a57fc3c608db976b94
5
5
  SHA512:
6
- metadata.gz: 3426d0303d776d0c7ecaeb17bf957a20228858ae859f85aec02d52dc833d643ead02cd14c522d6fcecc72b9f920e2efbc3667f31c86e6dcd363ebc4ef9eae516
7
- data.tar.gz: 0e8809f80cc34c92100a081917ce561ccaeb287e9002171f558d62e0c6148ac68a5b4b72f96e8720815f4d3be9390cda026fe39b08c3ba0895706c15a74e8865
6
+ metadata.gz: faf0f893ec70ae6c5186e6fa2afe5619d9ab4757d6fef73f1637163690a1a371df999e4d85a5af896e8ae2bae479d690891bedc704f5227aa55d52a02f582217
7
+ data.tar.gz: b9cf08da7a80849861da0c999d0d603d0b8c1466b93ed0e898d1bdb670bb5e49eb82ce592ba75d0d276d28d0309c3e85cccf5d317263791b16784132b55da8cf
data/README.adoc CHANGED
@@ -22,18 +22,34 @@ 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
34
50
 
35
51
  # If the URL or filename is specified and has a new value (different from what it was before), the index object will be recreated.
36
- Relaton::Index.find_or_create :IHO, url: nil, filename: "index.yaml"
52
+ Relaton::Index.find_or_create :IHO, url: nil, file: "index.yaml"
37
53
 
38
54
  # Remove the index from the pool.
39
55
  Relaton::Index.close :IHO
@@ -48,12 +64,12 @@ 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
 
55
71
  # Add a document to the index or update it if it already exists.
56
- index.add_or_update id: "B-4 2.19.0", file: "data/b-4_2_19_0.xml"
72
+ index.add_or_update "B-4 2.19.0", "data/b-4_2_19_0.xml"
57
73
 
58
74
  # Save the index to the `index.yaml` file in the current directory.
59
75
  index.save
@@ -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
@@ -11,23 +11,26 @@ module Relaton
11
11
  #
12
12
  # Initialize FileIO
13
13
  #
14
- # @param [String] dir local directory in ~/.relaton to store index
15
- # @param [String, Boolean, nil] url git repository URL to fetch index from
16
- # (if not exists, or older than 24 hours) or nil if index is used to
17
- # index files
18
- #
19
- def initialize(dir, url, filename)
14
+ # @param [String] dir falvor specific local directory in ~/.relaton to store index
15
+ # @param [String, Boolean, nil] url
16
+ # if String then the URL is used to fetch an index from a Git repository
17
+ # and save it to the storage (if not exists, or older than 24 hours)
18
+ # if true then the index is read from the storage (used to remove index file)
19
+ # if nil then the fiename is used to read and write file (used to create indes in GH actions)
20
+ #
21
+ def initialize(dir, url, filename, id_keys)
20
22
  @dir = dir
21
23
  @url = url
22
24
  @filename = filename
25
+ @id_keys = id_keys || []
23
26
  end
24
27
 
25
28
  #
26
29
  # If url is String, check if index file exists and is not older than 24
27
30
  # hours. If not, fetch index from external repository and save it to
28
31
  # storage.
29
- # If url is true, remove index from storage.
30
- # If url is nil, read index from file.
32
+ # If url is true, read index from path to local file.
33
+ # If url is nil, read index from filename.
31
34
  #
32
35
  # @return [Array<Hash>] index
33
36
  #
@@ -36,7 +39,7 @@ module Relaton
36
39
  when String
37
40
  check_file || fetch_and_save
38
41
  else
39
- read_file
42
+ read_file || []
40
43
  end
41
44
  end
42
45
 
@@ -65,6 +68,24 @@ module Relaton
65
68
  read_file
66
69
  end
67
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
+
68
89
  #
69
90
  # Read index from storage
70
91
  #
@@ -72,9 +93,11 @@ module Relaton
72
93
  #
73
94
  def read_file
74
95
  yaml = Index.config.storage.read(file)
75
- return [] unless yaml
96
+ return unless yaml
76
97
 
77
- YAML.safe_load yaml, permitted_classes: [Symbol]
98
+ index = YAML.safe_load yaml, permitted_classes: [Symbol]
99
+ return index if check_format index
100
+ rescue Psych::SyntaxError
78
101
  end
79
102
 
80
103
  #
@@ -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.0"
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.0
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-05-14 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