Module: Legion::Extensions::Knowledge::Helpers::ManifestStore

Defined in:
lib/legion/extensions/knowledge/helpers/manifest_store.rb

Constant Summary collapse

STORE_DIR =
::File.expand_path('~/.legionio/knowledge').freeze

Class Method Summary collapse

Class Method Details

.load(corpus_path:) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/legion/extensions/knowledge/helpers/manifest_store.rb', line 17

def load(corpus_path:)
  path = store_path(corpus_path: corpus_path)
  return [] unless ::File.exist?(path)

  raw = ::File.read(path, encoding: 'utf-8')
  ::JSON.parse(raw, symbolize_names: true)
rescue StandardError => _e
  []
end

.save(corpus_path:, manifest:) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/legion/extensions/knowledge/helpers/manifest_store.rb', line 27

def save(corpus_path:, manifest:)
  ::FileUtils.mkdir_p(STORE_DIR)
  path = store_path(corpus_path: corpus_path)
  tmp  = "#{path}.tmp"
  ::File.write(tmp, ::JSON.generate(manifest.map { |e| serialize_entry(e) }))
  ::File.rename(tmp, path)
  true
rescue StandardError => _e
  false
end

.store_path(corpus_path:) ⇒ Object



38
39
40
41
# File 'lib/legion/extensions/knowledge/helpers/manifest_store.rb', line 38

def store_path(corpus_path:)
  hash = ::Digest::SHA256.hexdigest(corpus_path.to_s)[0, 16]
  ::File.join(STORE_DIR, "#{hash}.manifest.json")
end