Class: Til::TemporaryNote

Inherits:
Object
  • Object
show all
Defined in:
lib/til/note_writer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tempfile = Tempfile.new(["new_note_content", ".md"])) ⇒ TemporaryNote

Returns a new instance of TemporaryNote.



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

def initialize(tempfile = Tempfile.new(["new_note_content", ".md"]))
  @tempfile = tempfile
end

Instance Attribute Details

#tempfileObject (readonly)

Returns the value of attribute tempfile.



17
18
19
# File 'lib/til/note_writer.rb', line 17

def tempfile
  @tempfile
end

Instance Method Details

#edit(if_modified, if_unmodified) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/til/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

#textObject



27
28
29
# File 'lib/til/note_writer.rb', line 27

def text
  File.read(path)
end

#write(text) ⇒ Object



23
24
25
# File 'lib/til/note_writer.rb', line 23

def write(text)
  File.write(path, text)
end