Class: Editor

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

Constant Summary collapse

DEFAULT_EDITOR =
"vi"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, command) ⇒ Editor

Returns a new instance of Editor.



12
13
14
15
16
# File 'lib/editor.rb', line 12

def initialize(path, command)
  @path    = path
  @command = command || DEFAULT_EDITOR
  @closed  = false
end

Class Method Details

.edit(path, command) {|editor| ... } ⇒ Object

Yields:

  • (editor)


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

def self.edit(path, command)
  editor = Editor.new(path, command)
  yield editor
  editor.edit_file
end

Instance Method Details

#closeObject



28
29
30
# File 'lib/editor.rb', line 28

def close
  @closed = true
end

#edit_fileObject



32
33
34
35
36
37
38
39
40
41
# File 'lib/editor.rb', line 32

def edit_file
  file.close
  editor_argv = Shellwords.shellsplit(@command) + [@path.to_s]

  unless @closed or system(*editor_argv)
    raise "There was a problem with the editor '#{ @command }'."
  end

  remove_notes(File.read(@path))
end

#note(string) ⇒ Object



23
24
25
26
# File 'lib/editor.rb', line 23

def note(string)
  return if @closed
  string.each_line { |line| file.puts("# #{ line }") }
end

#puts(string) ⇒ Object



18
19
20
21
# File 'lib/editor.rb', line 18

def puts(string)
  return if @closed
  file.puts(string)
end