Class: YAMLCommand::Command::EditCommand

Inherits:
YAMLCommand::Command show all
Defined in:
lib/yaml_command/edit.rb

Overview

Read a YAML file, open in default editor, validate and save. If directory instead of a file, slurp together as temporary YAML file and then re-splat to save.

Constant Summary collapse

TMP_FILE =

TODO: real tmpfile

".tmp.yml"
EDITOR =
ENV['EDITOR'] || 'vi'

Instance Attribute Summary

Attributes inherited from YAMLCommand::Command

#file

Instance Method Summary collapse

Methods inherited from YAMLCommand::Command

command_name, #debug=, #debug?, #h!, #help!, #inspect=, #inspect?, #json=, #json?, #mute=, #mute?, #yaml=, #yaml?

Instance Method Details

#call(file_or_dir) ⇒ Object

Edit a YAML file, or edit a directory via slurp and splt.



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/yaml_command/edit.rb', line 14

def call(file_or_dir)
  if File.directory?(file_or_dir)
    data0 = Console.slurp(file_or_dir)
    File.open(TMP_FILE, 'w'){ |f| f << data0.to_yaml }
    edit_file(tmpfile)
    data1 = YAML.load_file(TMP_FILE)
    Console.splat(file_or_dir, :data=>data1)
  elsif File.file?(file_or_dir)
    edit_file(file_or_dir)
  else
    abort "does not exist"
  end
end

#edit_file(file) ⇒ Object



29
30
31
32
33
34
35
36
37
38
# File 'lib/yaml_command/edit.rb', line 29

def edit_file(file)
  success = system "#{EDITOR} #{file}"
  if success
    begin
      YAML.load_file(file)
    rescue
      edit_file(file)
    end
  end
end