Class: MJ::Tools::TmpFileEditor

Inherits:
Object
  • Object
show all
Defined in:
lib/mj/tools/editor.rb

Overview

A class that puts the given content in a tmpfile, opens an editor for the user to play with it. After the user closed the editor it will give back the new content.

Instance Method Summary collapse

Constructor Details

#initialize(content) ⇒ TmpFileEditor

Returns a new instance of TmpFileEditor.



36
37
38
39
40
41
# File 'lib/mj/tools/editor.rb', line 36

def initialize( content )
    @file = Tempfile.new( [ 'configuration', '.yaml' ] )
    # First write the old content into the file
    @file.write( content )
    @file.close()
end

Instance Method Details

#editObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/mj/tools/editor.rb', line 43

def edit()
    # Check if there is a editor around here.
    editor = Tools::editor()
    if editor.nil?
        logger.error( 'Could not find a editor to use (xdg-open, $VISUAL, $GUIEDITOR, $EDITOR).' )
    end
    # Execute the editor
    cmd = "#{editor} #{@file.path}"
    logger.trace( "> #{cmd}" )
    if system( cmd ).nil?
        logger.error( "Failed to start editor #{editor}" )
    end
    return 0
end

#pathObject



58
59
60
# File 'lib/mj/tools/editor.rb', line 58

def path()
    @file.path()
end