Class: Pvectl::EditorSession

Inherits:
Object
  • Object
show all
Defined in:
lib/pvectl/editor_session.rb

Overview

Manages the editor lifecycle for interactive config editing.

Creates a temporary file with content, opens it in an editor, reads the result, and supports a retry loop with error injection when validation fails.

Examples:

Production usage with system editor

session = EditorSession.new
result = session.edit("cpu:\n  cores: 4\n")

Testing with injected editor

editor = ->(path) { File.write(path, "modified") }
session = EditorSession.new(editor: editor)
result = session.edit("original")

With validator

validator = ->(content) { content.include?("bad") ? ["Error: bad value"] : [] }
session = EditorSession.new(editor: editor, validator: validator)
result = session.edit("original")

Constant Summary collapse

ERROR_SEPARATOR =
"# -----------------------------------------------"

Instance Method Summary collapse

Constructor Details

#initialize(editor: nil, validator: nil) ⇒ EditorSession

Creates a new EditorSession.

Parameters:

  • editor (#call, nil) (defaults to: nil)

    callable that receives a file path and opens it for editing. Defaults to #system_editor which uses $EDITOR/$VISUAL/vi.

  • validator (#call, nil) (defaults to: nil)

    callable that receives edited content and returns an array of error strings. Empty array means valid.



35
36
37
38
# File 'lib/pvectl/editor_session.rb', line 35

def initialize(editor: nil, validator: nil)
  @editor = editor || method(:system_editor)
  @validator = validator
end

Instance Method Details

#edit(original_content) ⇒ String?

Opens content in an editor and returns the edited result.

Creates a temp file, invokes the editor, and reads back the content. Supports validation with retry loop and error injection.

Parameters:

  • original_content (String)

    the initial content to edit

Returns:

  • (String, nil)

    edited content, or nil if cancelled

Raises:

  • (RuntimeError)

    if no editor is found (system editor mode)



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/pvectl/editor_session.rb', line 48

def edit(original_content)
  tempfile = Tempfile.new(["pvectl-edit-", ".yaml"])
  tempfile.write(original_content)
  tempfile.flush
  path = tempfile.path

  edit_loop(path, original_content)
ensure
  tempfile&.close
  tempfile&.unlink
end