Class: Ucode::CodeChart::Sidecar

Inherits:
Object
  • Object
show all
Includes:
Repo::AtomicWrites
Defined in:
lib/ucode/code_chart/sidecar.rb

Overview

Writes a Provenance to disk as the sidecar JSON next to its corresponding SVG.

Path: <output_root>/<codepoint>.json — colocated with the SVG so a downstream consumer can find both files by a single directory listing.

Idempotent via Repo::AtomicWrites#write_atomic: a re-write of byte-identical content is a no-op (no temp-file rename). Provenance JSON is canonical (sorted keys via Ruby's stdlib JSON), so the byte-equality test is sound.

Instance Method Summary collapse

Methods included from Repo::AtomicWrites

#same_content?, #to_pretty_json, #write_atomic

Constructor Details

#initialize(output_root:) ⇒ Sidecar

Returns a new instance of Sidecar.

Parameters:

  • output_root (Pathname, String)

    directory the SVG + sidecar live in. Parent directories are created on demand.



26
27
28
# File 'lib/ucode/code_chart/sidecar.rb', line 26

def initialize(output_root:)
  @output_root = Pathname.new(output_root)
end

Instance Method Details

#path_for_id(codepoint_id) ⇒ Pathname

Returns the would-be path for a sidecar.

Parameters:

  • codepoint_id (String)

    e.g. "U+10920"

Returns:

  • (Pathname)

    the would-be path for a sidecar



41
42
43
# File 'lib/ucode/code_chart/sidecar.rb', line 41

def path_for_id(codepoint_id)
  @output_root.join("#{codepoint_id}.json")
end

#write(provenance) ⇒ Pathname

Returns the written sidecar path.

Parameters:

Returns:

  • (Pathname)

    the written sidecar path



32
33
34
35
36
37
# File 'lib/ucode/code_chart/sidecar.rb', line 32

def write(provenance)
  path = path_for(provenance)
  payload = JSON.pretty_generate(provenance.to_h) + "\n"
  write_atomic(path, payload)
  path
end