Class: InteractiveEditor
- Inherits:
-
Object
- Object
- InteractiveEditor
- Defined in:
- lib/interactive_editor.rb
Defined Under Namespace
Constant Summary collapse
- VERSION =
'0.0.7'
Instance Attribute Summary collapse
-
#editor ⇒ Object
Returns the value of attribute editor.
Class Method Summary collapse
Instance Method Summary collapse
- #edit(object, file = nil) ⇒ Object
- #execute ⇒ Object
-
#initialize(editor) ⇒ InteractiveEditor
constructor
A new instance of InteractiveEditor.
Constructor Details
#initialize(editor) ⇒ InteractiveEditor
Returns a new instance of InteractiveEditor.
16 17 18 |
# File 'lib/interactive_editor.rb', line 16 def initialize(editor) @editor = editor.to_s end |
Instance Attribute Details
#editor ⇒ Object
Returns the value of attribute editor.
14 15 16 |
# File 'lib/interactive_editor.rb', line 14 def editor @editor end |
Class Method Details
.edit(editor, self_, file = nil) ⇒ Object
57 58 59 60 61 |
# File 'lib/interactive_editor.rb', line 57 def self.edit(editor, self_, 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(self_, file) end |
Instance Method Details
#edit(object, file = nil) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/interactive_editor.rb', line 20 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, '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 ⇒ Object
53 54 55 |
# File 'lib/interactive_editor.rb', line 53 def execute eval(IO.read(@file.path), TOPLEVEL_BINDING) end |