Class: Textus::Ports::SentinelStore

Inherits:
Object
  • Object
show all
Defined in:
lib/textus/ports/sentinel_store.rb

Overview

Persistence adapter for sentinel files. Owns the on-disk JSON shape, the path layout (<store_root>/sentinels/<target-rel-to-repo>.textus-managed.json), and all File/FileUtils I/O. Domain::Sentinel is a pure value object that depends on this port for reads and writes.

Constant Summary collapse

SUFFIX =
".textus-managed.json".freeze
DIR =
"sentinels".freeze

Instance Method Summary collapse

Instance Method Details

#load(path, repo_root) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/textus/ports/sentinel_store.rb', line 27

def load(path, repo_root)
  raw = JSON.parse(File.read(path))
  Textus::Domain::Sentinel.new(
    target: absolutize(raw["target"], repo_root),
    source: absolutize(raw["source"], repo_root),
    sha256: raw["sha256"],
    mode: raw["mode"],
  )
rescue JSON::ParserError, Errno::ENOENT
  nil
end

#sentinel_path(target, store_root) ⇒ Object



39
40
41
42
43
# File 'lib/textus/ports/sentinel_store.rb', line 39

def sentinel_path(target, store_root)
  repo_root = File.dirname(store_root)
  rel = relative_to(target, repo_root) || File.basename(target)
  File.join(store_root, DIR, rel + SUFFIX)
end

#write!(target:, source:, store_root:) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/textus/ports/sentinel_store.rb', line 15

def write!(target:, source:, store_root:)
  path = sentinel_path(target, store_root)
  FileUtils.mkdir_p(File.dirname(path))
  repo_root = File.dirname(store_root)
  File.write(path, JSON.generate(
                     "source" => rel_or_abs(source, repo_root),
                     "target" => rel_or_abs(target, repo_root),
                     "sha256" => Digest::SHA256.hexdigest(File.binread(target)),
                     "mode" => "copy",
                   ))
end