relaton-index 0.1.7 → 0.1.9

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: 94b98c8a3a2f93284d50aefe422e0fea795bf697c7f4cd4bce8fcf517ccd2f62
4
- data.tar.gz: 63dbd9440f0cf7dbb58a043bce06940d1ac50e4a43513ab148ce292177a37f22
3
+ metadata.gz: 116ce97493b2532df6617749a9b8b29c815222450a9a8195850c25f3326fcce1
4
+ data.tar.gz: 6ee22048d4b694b6055d5e5d866afb4ea8c69343bc9b9804fe42f9de9ad066c2
5
5
  SHA512:
6
- metadata.gz: ae44baa7e0019a6de91cdecbc23c5c832425f84fd53f2f9262fd7d295a4df8bf3e95d96628326561023a90c1fd9a90cb2cee6cd0e542033d011ce5975a2f2354
7
- data.tar.gz: cef211d3dbf82b2105bfc3035a1f7d140ca41e9cd4a7a35559fd2709189dc36528a744d927ccf969cbc8827bb6405b3dda3659a273fda9794635ff4a11313e28
6
+ metadata.gz: b497db789344edc0376b295ae27551c2240062bc882f71164f52e82cac453b724dc17c3ca2f12152d34967ad30f4afea481875f28bed4913f57923e84b327fc3
7
+ data.tar.gz: 3e8f5f10253631234ed6645785382537318265de9f12eb956cb4eb056bf893bc13147bc1c1c9566e43a4065ad7335d1a6928c206418ad8f2138a50de60181e68
data/README.adoc CHANGED
@@ -61,11 +61,11 @@ index.save
61
61
 
62
62
  === Searching
63
63
 
64
- In this case, the Relaton library should create an index object and search for a document by reference. The gem looks for the `.relaton/[TYPE]/index.yaml` file in the user's home directory. The `[TYPE]` is one of downcased ISO, IEC, IHO, etc. If the file is not found or is older than 24 hours then it will be downloaded from the URL specified in the `find_or_create` method.
64
+ In this case, the Relaton library should create an index object and search for a document by reference. The gem looks for the `.relaton/[TYPE]/index.yaml` file in the user's home directory. The `[TYPE]` is one of downcased ISO, IEC, IHO, etc. If the file is not found or is older than 24 hours then it will be downloaded from the URL specified in the `find_or_create` method. The URL can be specified as `true` if the index should not be fetched from the URL.
65
65
 
66
66
  [source,ruby]
67
67
  ----
68
- # Create a new index object or fetch an existing one. URL should be specified. If the index file is not found or is older than 24 hours, it will be downloaded from the URL. By default, the index is saved as `index.yaml` file to the `/[HOME]/.relaton/iho/` folder.
68
+ # Create a new index object or fetch an existing one. URL should be specified. If the index file is not found or is older than 24 hours, it will be downloaded from the URL. By default, the index is saved as `index.yaml` file to the `/[HOME]/.relaton/iho/` folder. If the URL is specified as `true`, the index won't be fetched from the URL.
69
69
  index = Relaton::Index.find_or_create :IHO, url: "https://raw.githubusercontent.com/relaton/relaton-data-iho/master/index.zip"
70
70
 
71
71
 
@@ -83,11 +83,22 @@ end
83
83
 
84
84
  === Remove all index records
85
85
 
86
+ This method removes all records from the index object. The index file is not removed.
87
+
86
88
  [source,ruby]
87
89
  ----
88
90
  index.remove_all
89
91
  ----
90
92
 
93
+ === Remove index file
94
+
95
+ This method removes the index file. The index object is cleared and can be used for indexing or loading a file from the specified URL.
96
+
97
+ [source,ruby]
98
+ ----
99
+ index.remove_file
100
+ ----
101
+
91
102
  === Configuration
92
103
 
93
104
  The gem can be configured by using the `Relaton::Index.config` method. The following settings are available:
@@ -12,7 +12,7 @@ module Relaton
12
12
  # Initialize FileIO
13
13
  #
14
14
  # @param [String] dir local directory in ~/.relaton to store index
15
- # @param [String, nil] url git repository URL to fetch index from
15
+ # @param [String, Boolean, nil] url git repository URL to fetch index from
16
16
  # (if not exists, or older than 24 hours) or nil if index is used to
17
17
  # index files
18
18
  #
@@ -23,20 +23,37 @@ module Relaton
23
23
  end
24
24
 
25
25
  #
26
- # Read index from storage or fetch from external repository
26
+ # If url is String, check if index file exists and is not older than 24
27
+ # hours. If not, fetch index from external repository and save it to
28
+ # storage.
29
+ # If url is true, remove index from storage.
30
+ # If url is nil, read index from file.
27
31
  #
28
32
  # @return [Array<Hash>] index
29
33
  #
30
34
  def read
31
- if url
32
- @file ||= File.join(Index.config.storage_dir, ".relaton", @dir, @filename)
35
+ case url
36
+ when String
37
+ @file ||= path_to_local_file
33
38
  check_file || fetch_and_save
39
+ when true
40
+ @file ||= path_to_local_file
41
+ read_file
34
42
  else
35
43
  @file ||= @filename
36
44
  read_file
37
45
  end
38
46
  end
39
47
 
48
+ #
49
+ # Create path to local file
50
+ #
51
+ # @return [<Type>] <description>
52
+ #
53
+ def path_to_local_file
54
+ File.join(Index.config.storage_dir, ".relaton", @dir, @filename)
55
+ end
56
+
40
57
  #
41
58
  # Check if index file exists and is not older than 24 hours
42
59
  #
@@ -78,13 +95,23 @@ module Relaton
78
95
  #
79
96
  # Save index to storage
80
97
  #
81
- # @param [Array<Hash>] index <description>
98
+ # @param [Array<Hash>] index index to save
82
99
  #
83
100
  # @return [void]
84
101
  #
85
102
  def save(index)
86
103
  Index.config.storage.write @file, index.to_yaml
87
104
  end
105
+
106
+ #
107
+ # Remove index file from storage
108
+ #
109
+ # @return [Array]
110
+ #
111
+ def remove
112
+ Index.config.storage.remove @filename
113
+ []
114
+ end
88
115
  end
89
116
  end
90
117
  end
@@ -42,6 +42,19 @@ module Relaton
42
42
  File.write file, data, encoding: "UTF-8"
43
43
  end
44
44
 
45
+ #
46
+ # Remove file
47
+ #
48
+ # @param [String] file file path
49
+ #
50
+ # @return [void]
51
+ #
52
+ def remove(file)
53
+ return unless File.exist? file
54
+
55
+ File.delete file
56
+ end
57
+
45
58
  extend self
46
59
  end
47
60
  end
@@ -15,7 +15,10 @@ module Relaton
15
15
  @file = file
16
16
  filename = file || Index.config.filename
17
17
  @file_io = FileIO.new type.to_s.downcase, url, filename
18
- @index = @file_io.read
18
+ end
19
+
20
+ def index
21
+ @index ||= @file_io.read
19
22
  end
20
23
 
21
24
  #
@@ -41,11 +44,11 @@ module Relaton
41
44
  # @return [void]
42
45
  #
43
46
  def add_or_update(id, file)
44
- item = @index.find { |i| i[:id] == id }
47
+ item = index.find { |i| i[:id] == id }
45
48
  if item
46
49
  item[:file] = file
47
50
  else
48
- @index << { id: id, file: file }
51
+ index << { id: id, file: file }
49
52
  end
50
53
  end
51
54
 
@@ -57,7 +60,7 @@ module Relaton
57
60
  # @return [Array<Hash>] search results
58
61
  #
59
62
  def search(id = nil)
60
- @index.select do |i|
63
+ index.select do |i|
61
64
  block_given? ? yield(i) : i[:id].include?(id)
62
65
  end
63
66
  end
@@ -71,6 +74,16 @@ module Relaton
71
74
  @file_io.save @index
72
75
  end
73
76
 
77
+ #
78
+ # Remove index file from storage and clear index
79
+ #
80
+ # @return [void]
81
+ #
82
+ def remove_file
83
+ @file_io.remove
84
+ @index = nil
85
+ end
86
+
74
87
  #
75
88
  # Remove all index items
76
89
  #
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Relaton
4
4
  module Index
5
- VERSION = "0.1.7"
5
+ VERSION = "0.1.9"
6
6
  end
7
7
  end
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.1.7
4
+ version: 0.1.9
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-10 00:00:00.000000000 Z
11
+ date: 2023-05-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rubyzip