class Exedit::Editor

An exedit external editor instance

Attributes

edit_command[RW]

Public Class Methods

new(edit_command, file_path = nil, content: nil) click to toggle source
# File lib/exedit/editor.rb, line 8
def initialize(edit_command, file_path = nil, content: nil)
  @file_path = file_path
  @edit_command = edit_command
  @content = content
end

Public Instance Methods

open() click to toggle source
# File lib/exedit/editor.rb, line 14
def open
  (file.write(@content) && file.flush) if @content && !@file_path

  system("#{edit_command} #{file.path}")

  result = reload(file).read

  # Remove the tempfile if it exists
  tempfile.unlink if @tempfile

  result
end

Private Instance Methods

file() click to toggle source
# File lib/exedit/editor.rb, line 29
def file
  @file ||= @file_path ? File.new(@file_path) : tempfile
end
reload(file) click to toggle source
# File lib/exedit/editor.rb, line 37
def reload(file)
  file.reopen(file.path)
end
tempfile() click to toggle source
# File lib/exedit/editor.rb, line 33
def tempfile
  @tempfile ||= Tempfile.new('ex-edit')
end