Class: Mistri::Workspace::Single

Inherits:
Object
  • Object
show all
Defined in:
lib/mistri/workspace/single.rb

Overview

One document living wherever the host says: a column on a record, a cache key, anything readable and writable. This is the shape of an agent that edits one page:

workspace = Mistri::Workspace::Single.new(
path: "page.html",
read: -> { page.reload.draft_html },
write: ->(html) { page.update!(draft_html: html) }
)

The document tools then read and edit that column like any document.

Instance Method Summary collapse

Constructor Details

#initialize(read:, write:, path: "document") ⇒ Single

Returns a new instance of Single.



17
18
19
20
21
# File 'lib/mistri/workspace/single.rb', line 17

def initialize(read:, write:, path: "document")
  @path = path.to_s
  @read = read
  @write = write
end

Instance Method Details

#delete(path) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/mistri/workspace/single.rb', line 34

def delete(path)
  if path.to_s == @path
    raise SchemaError,
          "#{@path.inspect} cannot be deleted, only rewritten"
  end

  nil
end

#list(prefix = nil) ⇒ Object



43
44
45
# File 'lib/mistri/workspace/single.rb', line 43

def list(prefix = nil)
  prefix.nil? || @path.start_with?(prefix.to_s) ? [@path] : []
end

#read(path) ⇒ Object



23
24
25
# File 'lib/mistri/workspace/single.rb', line 23

def read(path)
  path.to_s == @path ? @read.call : nil
end

#write(path, content) ⇒ Object

Raises:



27
28
29
30
31
32
# File 'lib/mistri/workspace/single.rb', line 27

def write(path, content)
  raise SchemaError, "this workspace holds only #{@path.inspect}" unless path.to_s == @path

  @write.call(content.to_s)
  nil
end