Class: TempEditor

Inherits:
Object
  • Object
show all
Defined in:
lib/temp_editor.rb,
lib/temp_editor/version.rb

Defined Under Namespace

Classes: EditorConfigureError

Constant Summary collapse

VERSION =
'0.1.0'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(basename, *rest, &after) ⇒ TempEditor

Returns a new instance of TempEditor.



10
11
12
13
# File 'lib/temp_editor.rb', line 10

def initialize(basename, *rest, &after)
  @tempfile = Tempfile.new(basename, *rest)
  after(&after) if block_given?
end

Instance Attribute Details

#tempfileObject (readonly)

Returns the value of attribute tempfile.



8
9
10
# File 'lib/temp_editor.rb', line 8

def tempfile
  @tempfile
end

Instance Method Details

#after(&block) ⇒ Object



19
20
21
# File 'lib/temp_editor.rb', line 19

def after(&block)
  @after_callback = build_callback(&block)
end

#before(&block) ⇒ Object



15
16
17
# File 'lib/temp_editor.rb', line 15

def before(&block)
  @before_callback = build_callback(&block)
end

#editObject



23
24
25
26
27
# File 'lib/temp_editor.rb', line 23

def edit
  @before_callback.call if @before_callback
  system "#{editor} #{@tempfile.path}"
  @after_callback.call if @after_callback
end

#editorObject



29
30
31
32
# File 'lib/temp_editor.rb', line 29

def editor
  @editor ||= ENV['EDITOR']
  @editor or raise EditorConfigureError, "set your ENV['EDITOR']"
end

#editor=(editor_cmd) ⇒ Object



34
35
36
# File 'lib/temp_editor.rb', line 34

def editor=(editor_cmd)
  @editor = editor_cmd
end