Class: Space::Architect::SessionSync::Cursor

Inherits:
Object
  • Object
show all
Defined in:
lib/space_architect/session_sync/cursor.rb

Overview

Machine-managed cursor at $XDG_STATE_HOME/space-architect/session-sync.yaml (imitates Space::Src::State::Store's load/write/emit shape). Keyed by absolute source path; each entry records the size + mtime observed the last time that file was uploaded.

Defined Under Namespace

Classes: Entry

Class Method Summary collapse

Class Method Details

.load(path) ⇒ Object



17
18
19
20
21
22
# File 'lib/space_architect/session_sync/cursor.rb', line 17

def self.load(path)
  raw = read_yaml(path)
  raw.each_with_object({}) do |(k, v), acc|
    acc[k] = Entry.new(size: v["size"], mtime: v["mtime"])
  end
end

.read_yaml(path) ⇒ Object



36
37
38
39
40
# File 'lib/space_architect/session_sync/cursor.rb', line 36

def self.read_yaml(path)
  return {} unless File.exist?(path)

  YAML.safe_load_file(path, permitted_classes: [], aliases: false) || {}
end

.write(path, entries) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/space_architect/session_sync/cursor.rb', line 24

def self.write(path, entries)
  FileUtils.mkdir_p(File.dirname(path))
  payload = entries.each_with_object({}) { |(k, v), acc| acc[k] = {"size" => v.size, "mtime" => v.mtime} }
  tmp = "#{path}.tmp.#{Process.pid}"
  begin
    File.write(tmp, YAML.dump(payload, line_width: -1))
    File.rename(tmp, path)
  ensure
    File.delete(tmp) if File.exist?(tmp)
  end
end