Class: InteractiveEditor

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

Defined Under Namespace

Modules: Editors, Exec

Constant Summary collapse

VERSION =
'0.0.5'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(editor) ⇒ InteractiveEditor

Returns a new instance of InteractiveEditor.



14
15
16
# File 'lib/console_editor.rb', line 14

def initialize(editor)
  @editor = editor.to_s
end

Instance Attribute Details

#editorObject

Returns the value of attribute editor.



12
13
14
# File 'lib/console_editor.rb', line 12

def editor
  @editor
end

Class Method Details

.edit(editor, file = nil) ⇒ Object



40
41
42
43
44
# File 'lib/console_editor.rb', line 40

def self.edit(editor, file=nil)
  #maybe serialise last file to disk, for recovery
  (IRB.conf[:interactive_editors] ||=
    Hash.new { |h,k| h[k] = InteractiveEditor.new(k) })[editor].edit(file)
end

Instance Method Details

#edit(file = nil) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/console_editor.rb', line 18

def edit(file=nil)
  @file = if file
     FileUtils.touch(file) unless File.exist?(file)
     File.new(file)
    else
     (@file && File.exist?(@file.path)) ? @file : Tempfile.new(["irb_tempfile", ".rb"])
  end
  mtime = File.stat(@file.path).mtime

  if (args = @editor.split(/\s+/)).size > 1
    Exec.system(args[0], *(args[1..-1] << @file.path))
  else
    Exec.system(@editor, @file.path)
  end

  #execute if mtime < File.stat(@file.path).mtime
end

#executeObject



36
37
38
# File 'lib/console_editor.rb', line 36

def execute
  eval(IO.read(@file.path), TOPLEVEL_BINDING)
end