Class: Mbeditor::EditorStateService

Inherits:
Object
  • Object
show all
Defined in:
app/services/mbeditor/editor_state_service.rb

Constant Summary collapse

PayloadTooLargeError =
Class.new(StandardError)
InvalidBranchError =
Class.new(StandardError)
STATE_MAX_BYTES =
1 * 1024 * 1024
SAFE_BRANCH_NAME =
/\A[a-zA-Z0-9._\-\/]+\z/

Instance Method Summary collapse

Constructor Details

#initialize(workspace_root) ⇒ EditorStateService

Returns a new instance of EditorStateService.



11
12
13
# File 'app/services/mbeditor/editor_state_service.rb', line 11

def initialize(workspace_root)
  @root = workspace_root
end

Instance Method Details

#prune_branch_states(active_branches:) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/services/mbeditor/editor_state_service.rb', line 49

def prune_branch_states(active_branches:)
  path = branch_states_path
  return [] unless File.exist?(path)
  pruned = []
  File.open(path, File::RDWR) do |f|
    f.flock(File::LOCK_EX)
    all = JSON.parse(f.read) rescue {}
    pruned = all.keys - active_branches
    if pruned.any?
      pruned.each { |b| all.delete(b) }
      f.truncate(0)
      f.rewind
      f.write(all.to_json)
    end
  end
  pruned
end

#read_branch_state(branch) ⇒ Object



23
24
25
26
27
28
29
30
# File 'app/services/mbeditor/editor_state_service.rb', line 23

def read_branch_state(branch)
  path = branch_states_path
  return {} unless File.exist?(path)
  all = JSON.parse(File.read(path))
  all[branch] || {}
rescue JSON::ParserError, Errno::ENOENT
  {}
end

#read_stateObject



15
16
17
18
19
20
21
# File 'app/services/mbeditor/editor_state_service.rb', line 15

def read_state
  path = workspace_path
  return {} unless File.exist?(path)
  JSON.parse(File.read(path))
rescue JSON::ParserError, Errno::ENOENT
  {}
end

#write_branch_state(branch, state) ⇒ Object

Raises:



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/services/mbeditor/editor_state_service.rb', line 32

def write_branch_state(branch, state)
  raise InvalidBranchError, "Invalid branch name" unless branch.match?(SAFE_BRANCH_NAME)
  payload_json = state.to_json
  raise PayloadTooLargeError, "State payload too large" if payload_json.bytesize > STATE_MAX_BYTES
  path = branch_states_path
  FileUtils.mkdir_p(path.dirname)
  File.open(path, File::RDWR | File::CREAT) do |f|
    f.flock(File::LOCK_EX)
    existing = f.size > 0 ? JSON.parse(f.read) : {}
    existing[branch] = state
    f.truncate(0)
    f.rewind
    f.write(existing.to_json)
  end
  nil
end

#write_state(state) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'app/services/mbeditor/editor_state_service.rb', line 67

def write_state(state)
  payload = state.to_json
  raise PayloadTooLargeError, "State payload too large" if payload.bytesize > STATE_MAX_BYTES
  path = workspace_path
  FileUtils.mkdir_p(path.dirname)
  File.open(path, File::RDWR | File::CREAT) do |f|
    f.flock(File::LOCK_EX)
    f.truncate(0)
    f.rewind
    f.write(payload)
  end
  nil
end