Class: InteractiveEditor

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

Defined Under Namespace

Modules: Editors, Exec

Constant Summary collapse

VERSION =
'0.0.5.2'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(editor) ⇒ InteractiveEditor

Returns a new instance of InteractiveEditor.



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

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

Instance Attribute Details

#editorObject

Returns the value of attribute editor.



16
17
18
# File 'lib/interactive_editor.rb', line 16

def editor
  @editor
end

Class Method Details

.configObject



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

def self.config
  @@config ||= {}
end

.edit(editor, file = nil) ⇒ Object



48
49
50
51
52
# File 'lib/interactive_editor.rb', line 48

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



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/interactive_editor.rb', line 22

def edit(file=nil)
  @file = if file
     FileUtils.touch(file) unless File.exist?(file)
     File.new(file)
    else
      (@file && File.exist?(@file.path)) ? @file : if InteractiveEditor.config[:save]
          File.open(File.join(InteractiveEditor.config[:save_path] || ENV['HOME'], "irb_#{Time.now.strftime('%Y%m%d%H%M')}.rb"), 'wb')
        else
          Tempfile.new(["irb_tempfile", ".rb"])
      end
  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



44
45
46
# File 'lib/interactive_editor.rb', line 44

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