Class: DTC::Utils::InteractiveEditor
- Inherits:
- 
      Object
      
        - Object
- DTC::Utils::InteractiveEditor
 
- Defined in:
- lib/dtc/utils/interactive_edit.rb
Overview
Copied from gem utility_belt (and modified, so get your own copy utilitybelt.rubyforge.org) Giles Bowkett, Greg Brown, and several audience members from Giles’ Ruby East presentation.
Constant Summary collapse
- DEBIAN_SENSIBLE_EDITOR =
- "/usr/bin/sensible-editor"
- MACOSX_OPEN_CMD =
- ["open", "--wait-apps", "-e"] 
- WIN_START_CMD =
- ["start", "-w"] 
- XDG_OPEN =
- "/usr/bin/xdg-open"
Instance Attribute Summary collapse
- 
  
    
      #editor  ⇒ Object 
    
    
  
  
  
  
    
    
  
  
  
  
  
  
    Returns the value of attribute editor. 
Class Method Summary collapse
- .edit(content, extension = ".yaml", editor = InteractiveEditor.sensible_editor) ⇒ Object
- .edit_file(filename, editor = InteractiveEditor.sensible_editor) ⇒ Object
- .edit_in_yaml(object, editor = InteractiveEditor.sensible_editor) ⇒ Object
- .sensible_editor ⇒ Object
- 
  
    
      .with_temp_file(base_name, extension, content, &block)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    :yields: temp_filename. 
Instance Method Summary collapse
- #edit_file_interactively(filename) ⇒ Object
- #edit_interactively(content) ⇒ Object
- #filename ⇒ Object
- 
  
    
      #initialize(editor = InteractiveEditor.sensible_editor, extension = ".yaml")  ⇒ InteractiveEditor 
    
    
  
  
  
    constructor
  
  
  
  
  
  
  
    A new instance of InteractiveEditor. 
Constructor Details
#initialize(editor = InteractiveEditor.sensible_editor, extension = ".yaml") ⇒ InteractiveEditor
Returns a new instance of InteractiveEditor.
| 35 36 37 38 39 | # File 'lib/dtc/utils/interactive_edit.rb', line 35 def initialize(editor = InteractiveEditor.sensible_editor, extension = ".yaml") @editor = @editor == "mate" ? ["mate", "-w"] : editor @extension = extension @file = nil end | 
Instance Attribute Details
#editor ⇒ Object
Returns the value of attribute editor.
| 34 35 36 | # File 'lib/dtc/utils/interactive_edit.rb', line 34 def editor @editor end | 
Class Method Details
.edit(content, extension = ".yaml", editor = InteractiveEditor.sensible_editor) ⇒ Object
| 77 78 79 | # File 'lib/dtc/utils/interactive_edit.rb', line 77 def self.edit(content, extension = ".yaml", editor = InteractiveEditor.sensible_editor) InteractiveEditor.new(editor, extension).edit_interactively(content) end | 
.edit_file(filename, editor = InteractiveEditor.sensible_editor) ⇒ Object
| 69 70 71 72 73 74 75 76 | # File 'lib/dtc/utils/interactive_edit.rb', line 69 def self.edit_file(filename, editor = InteractiveEditor.sensible_editor) editor = InteractiveEditor.new editor editor.edit_file_interactively(filename) rescue Exception => error puts "# !!!" + error.inspect raise return nil end | 
.edit_in_yaml(object, editor = InteractiveEditor.sensible_editor) ⇒ Object
| 80 81 82 83 84 85 86 87 88 89 | # File 'lib/dtc/utils/interactive_edit.rb', line 80 def self.edit_in_yaml(object, editor = InteractiveEditor.sensible_editor) input = "# Just empty and save this document to abort !\n" + object.to_yaml editor = InteractiveEditor.new editor res = editor.edit_interactively(input) YAML::load(res) rescue Exception => error puts "# !!!" + error.inspect raise return nil end | 
.sensible_editor ⇒ Object
| 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | # File 'lib/dtc/utils/interactive_edit.rb', line 17 def self.sensible_editor return Shellwords::split(ENV["VISUAL"]) if ENV["VISUAL"] return Shellwords::split(ENV["EDITOR"]) if ENV["EDITOR"] if defined?(Platform) return WIN_START_CMD if Platform::IMPL == :mswin return MACOSX_OPEN_CMD if Platform::IMPL == :macosx if Platform::IMPL == :linux if File.executable?(XDG_OPEN) return XDG_OPEN end if File.executable?(DEBIAN_SENSIBLE_EDITOR) return DEBIAN_SENSIBLE_EDITOR end end end raise "Could not determine what editor to use. Please specify (or use platform gem)." end | 
.with_temp_file(base_name, extension, content, &block) ⇒ Object
:yields: temp_filename
| 46 47 48 49 50 51 52 53 54 55 | # File 'lib/dtc/utils/interactive_edit.rb', line 46 def self.with_temp_file base_name, extension, content, &block # :yields: temp_filename file = Tempfile.new([base_name, extension]) begin file << content file.close yield file.path ensure file.unlink end end | 
Instance Method Details
#edit_file_interactively(filename) ⇒ Object
| 43 44 45 | # File 'lib/dtc/utils/interactive_edit.rb', line 43 def edit_file_interactively(filename) Exec.sys(@editor, filename) end | 
#edit_interactively(content) ⇒ Object
| 56 57 58 59 60 61 62 63 64 65 66 67 68 | # File 'lib/dtc/utils/interactive_edit.rb', line 56 def edit_interactively(content) unless @file @file = Tempfile.new(["#{File.basename(__FILE__, File.extname(__FILE__))}-edit", @extension]) @file << content @file.close end edit_file_interactively(@file.path) IO::read(@file.path) rescue Exception => error @file.unlink @file = nil puts error end | 
#filename ⇒ Object
| 40 41 42 | # File 'lib/dtc/utils/interactive_edit.rb', line 40 def filename @file ? @file.path : nil end |