class InteractiveEditor

Constants

EDITORS
VERSION

Attributes

editor[RW]

Public Class Methods

edit(editor, self_, file=nil) click to toggle source
# File lib/sigterm_extensions/interactive_editor.rb, line 58
def self.edit(editor, self_, file=nil)
  find_editor[editor].edit(self_, file)
end
find_editor() click to toggle source
# File lib/sigterm_extensions/interactive_editor.rb, line 62
def self.find_editor
  #maybe serialise last file to disk, for recovery
  if defined?(Pry) and IRB == Pry
    IRB.config.interactive_editors ||= EDITORS
  else
    IRB.conf[:interactive_editors] ||= EDITORS
  end
end
new(editor) click to toggle source
# File lib/sigterm_extensions/interactive_editor.rb, line 17
def initialize(editor)
  @editor = editor.to_s
end

Public Instance Methods

edit(object, file=nil) click to toggle source
# File lib/sigterm_extensions/interactive_editor.rb, line 21
def edit(object, file=nil)
  object = object == TOPLEVEL_BINDING.eval('self') ? nil : object

  current_file = if file
    FileUtils.touch(file) unless File.exist?(file)
    File.new(file)
  else
    if @file && File.exist?(@file.path) && !object
      @file
    else
      Tempfile.new( object ? ["yobj_tempfile", ".yml"] : ["irb_tempfile", ".rb"] )
    end
  end

  if object
    File.open( current_file.path, 'w' ) { |f| f << object.to_yaml }
  else
    @file = current_file
    mtime = File.stat(@file.path).mtime
  end

  args = Shellwords.shellwords(@editor) #parse @editor as arguments could be complex
  args << current_file.path
  current_file.close rescue nil
  Exec.system(*args)

  if object
    File.exists?(current_file.path) ? YAML.load_file(current_file.path) : object
  elsif mtime < File.stat(@file.path).mtime
    execute
  end
end
execute() click to toggle source
# File lib/sigterm_extensions/interactive_editor.rb, line 54
def execute
  eval(IO.read(@file.path), TOPLEVEL_BINDING)
end