Class: Pantry::FileEditor
- Inherits:
-
Object
- Object
- Pantry::FileEditor
- Defined in:
- lib/pantry/file_editor.rb
Overview
Use EDITOR to edit the contents of a remote file locally The editor can validate the updated content to be YAML (more to be added as needed) and will show errors and re-edit the file if validation fails.
If the user chooses to cancel editing, #edit will return the original content given to it.
Usage is simple:
editor = FileEditor.new
updated_content = editor.edit(file_contents, file_type)
Instance Method Summary collapse
- #edit(original_content, file_type) ⇒ Object
-
#initialize ⇒ FileEditor
constructor
A new instance of FileEditor.
Constructor Details
#initialize ⇒ FileEditor
17 18 19 20 |
# File 'lib/pantry/file_editor.rb', line 17 def initialize @editor = ENV['EDITOR'] raise "Please set EDITOR environment variable to a text editor." unless @editor end |
Instance Method Details
#edit(original_content, file_type) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/pantry/file_editor.rb', line 22 def edit(original_content, file_type) file = create_temp_file(original_content, file_type) new_content = "" loop do new_content = edit_file(file) is_valid, = validate_content(new_content, file_type) break if is_valid Pantry.ui.say() if !Pantry.ui.continue?("Continue editing?") new_content = original_content break end end file.unlink new_content end |