class Til::TemporaryNote

Attributes

tempfile[R]

Public Class Methods

new(tempfile = Tempfile.new(["new_note_content", ".md"])) click to toggle source
# File lib/til/services/note_writer.rb, line 19
def initialize(tempfile = Tempfile.new(["new_note_content", ".md"]))
  @tempfile = tempfile
end

Public Instance Methods

edit(if_modified, if_unmodified) click to toggle source
# File lib/til/services/note_writer.rb, line 31
def edit(if_modified, if_unmodified)
  original_text = text
  system(ENV["EDITOR"], path)
  modified_text = text
  
  if original_text == modified_text
    if_unmodified.call
  else
    if_modified.call(modified_text)
  end

  finish_editing
end
text() click to toggle source
# File lib/til/services/note_writer.rb, line 27
def text
  File.read(path)
end
write(text) click to toggle source
# File lib/til/services/note_writer.rb, line 23
def write(text)
  File.write(path, text)
end

Private Instance Methods

finish_editing() click to toggle source
# File lib/til/services/note_writer.rb, line 51
def finish_editing
  tempfile.close
  tempfile.unlink
end
path() click to toggle source
# File lib/til/services/note_writer.rb, line 47
def path
  tempfile.path
end