Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add _why style yaml object editing if called on a object

  • Loading branch information...
commit c0c6c0850758e17abbf17151d0e423246afd803f 1 parent bbc6dea
@janlelis janlelis authored
Showing with 31 additions and 13 deletions.
  1. +31 −13 lib/interactive_editor.rb
View
44 lib/interactive_editor.rb
@@ -6,6 +6,7 @@
require 'fileutils'
require 'tempfile'
require 'shellwords'
+require 'yaml'
class InteractiveEditor
VERSION = '0.0.6'
@@ -16,30 +17,47 @@ def initialize(editor)
@editor = editor.to_s
end
- def edit(file=nil)
- @file = if file
- FileUtils.touch(file) unless File.exist?(file)
- File.new(file)
+ def edit(object, file=nil)
+ object = object.instance_of?(Object) ? 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
- (@file && File.exist?(@file.path)) ? @file : Tempfile.new(["irb_tempfile", ".rb"])
+ 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
- mtime = File.stat(@file.path).mtime
args = Shellwords.shellwords(@editor) #parse @editor as arguments could be complexe
- args << @file.path
- Exec.system(*args)
+ args << current_file.path
+ Exec.system(*args)
- execute if mtime < File.stat(@file.path).mtime
+ if object
+ return object unless File.exists?(current_file)
+ YAML::load( File.open(current_file) )
+ elsif mtime < File.stat(@file.path).mtime
+ execute
+ end
end
def execute
eval(IO.read(@file.path), TOPLEVEL_BINDING)
end
- def self.edit(editor, file=nil)
+ 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(file)
+ Hash.new { |h,k| h[k] = InteractiveEditor.new(k) })[editor].edit(self_, file)
end
module Exec
@@ -71,13 +89,13 @@ module Editors
:mvim => 'mvim -g -f -c "au VimLeave * !open -a Terminal"'
}.each do |k,v|
define_method(k) do |*args|
- InteractiveEditor.edit(v || k, *args)
+ InteractiveEditor.edit(v || k, self, *args)
end
end
def ed(*args)
if ENV['EDITOR'].to_s.size > 0
- InteractiveEditor.edit(ENV['EDITOR'], *args)
+ InteractiveEditor.edit(ENV['EDITOR'], self, *args)
else
raise "You need to set the EDITOR environment variable first"
end
Please sign in to comment.
Something went wrong with that request. Please try again.