Module: Brute::Store::SnapshotStore

Defined in:
lib/brute/store/snapshot_store.rb

Overview

Per-path stack of file snapshots used by fs_write, fs_patch, fs_remove to enable undo. Each call to .save pushes the current content (or :did_not_exist for new files). .pop retrieves the most recent snapshot.

Class Method Summary collapse

Class Method Details

.clear!Object

Clear all snapshots. Used in tests and session resets.



30
31
32
# File 'lib/brute/store/snapshot_store.rb', line 30

def clear!
  @mutex.synchronize { @snapshots.clear }
end

.pop(path) ⇒ Object

Pop and return the most recent snapshot for path, or nil if there is no history.



24
25
26
27
# File 'lib/brute/store/snapshot_store.rb', line 24

def pop(path)
  key = File.expand_path(path)
  @mutex.synchronize { @snapshots[key].pop }
end

.save(path) ⇒ Object

Push the current content of path onto the snapshot stack. If the file doesn’t exist yet, records :did_not_exist.



16
17
18
19
20
# File 'lib/brute/store/snapshot_store.rb', line 16

def save(path)
  key = File.expand_path(path)
  content = File.exist?(key) ? File.read(key) : :did_not_exist
  @mutex.synchronize { @snapshots[key].push(content) }
end