Class: Exedit::Editor

Inherits:
Object
  • Object
show all
Defined in:
lib/exedit/editor.rb

Overview

An exedit external editor instance

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(edit_command, file_path = nil, content: nil) ⇒ Editor

Returns a new instance of Editor.



8
9
10
11
12
# 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

Instance Attribute Details

#edit_commandObject

Returns the value of attribute edit_command.



6
7
8
# File 'lib/exedit/editor.rb', line 6

def edit_command
  @edit_command
end

Instance Method Details

#openObject



14
15
16
17
18
19
20
21
22
23
24
25
# 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