Module: Brute::SnapshotStore

Defined in:
lib/brute/snapshot_store.rb

Overview

Copy-on-write snapshot storage for file undo support. Saves the previous content of a file before mutation so it can be restored. Each file maintains a stack of snapshots, supporting multiple undo levels.

Class Method Summary collapse

Class Method Details

.clear!Object

Clear all snapshots (useful for testing or session reset).



44
45
46
# File 'lib/brute/snapshot_store.rb', line 44

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

.depth(path) ⇒ Object

Check how many undo levels are available for a file.



36
37
38
39
40
41
# File 'lib/brute/snapshot_store.rb', line 36

def depth(path)
  path = File.expand_path(path)
  @mutex.synchronize do
    @store[path]&.size || 0
  end
end

.pop(path) ⇒ Object

Pop the most recent snapshot for a file. Returns the content string, :did_not_exist, or nil if no history.



28
29
30
31
32
33
# File 'lib/brute/snapshot_store.rb', line 28

def pop(path)
  path = File.expand_path(path)
  @mutex.synchronize do
    @store[path]&.pop
  end
end

.save(path) ⇒ Object

Save the current state of a file before mutating it. If the file doesn’t exist, records :did_not_exist so undo can delete it.



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/brute/snapshot_store.rb', line 14

def save(path)
  path = File.expand_path(path)
  @mutex.synchronize do
    @store[path] ||= []
    if File.exist?(path)
      @store[path].push(File.read(path))
    else
      @store[path].push(:did_not_exist)
    end
  end
end