Class: Annoyme::NotesFile

Inherits:
Object
  • Object
show all
Defined in:
lib/annoyme/notes_file.rb

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ NotesFile

Returns a new instance of NotesFile.



5
6
7
# File 'lib/annoyme/notes_file.rb', line 5

def initialize(path)
  @path = path
end

Instance Method Details

#createObject



9
10
11
12
13
# File 'lib/annoyme/notes_file.rb', line 9

def create
  File.open(@path, 'w') do |f|
    f.write("[]")
  end
end

#exists?Boolean

Returns:

  • (Boolean)


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

def exists?
  File.exist?(@path)
end

#parseObject



19
20
21
22
23
24
# File 'lib/annoyme/notes_file.rb', line 19

def parse
  content = File.readlines(@path).first
  JSON.parse(content)
rescue JSON::ParserError
  raise 'notes file seems to be corrupted'
end

#write(content) ⇒ Object



26
27
28
29
30
# File 'lib/annoyme/notes_file.rb', line 26

def write(content)
  File.open(@path, 'w') do |f|
    f.write(content)
  end
end