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

Extended by:
JSON::Helper, Logging::Helper
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



20
21
22
23
24
25
26
27
28
29
# File 'lib/legion/extensions/knowledge/helpers/manifest_store.rb', line 20

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)
rescue StandardError => e
  handle_exception(e, level: :warn, operation: 'knowledge.manifest_store.load', corpus_path: corpus_path)
  []
end

.save(corpus_path:, manifest:) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/legion/extensions/knowledge/helpers/manifest_store.rb', line 31

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
  handle_exception(e, level: :warn, operation: 'knowledge.manifest_store.save', corpus_path: corpus_path)
  false
end

.store_path(corpus_path:) ⇒ Object



43
44
45
46
# File 'lib/legion/extensions/knowledge/helpers/manifest_store.rb', line 43

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