Class: Editus::FileLib

Inherits:
Object
  • Object
show all
Defined in:
lib/editus/file.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.instanceObject



4
5
6
# File 'lib/editus/file.rb', line 4

def instance
  @instance ||= new
end

.read(path) ⇒ Object



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

def read path
  instance.create_file_if_not_exists(path)
end

.write(path, content) ⇒ Object



12
13
14
# File 'lib/editus/file.rb', line 12

def write path, content
  instance.write_file_if_not_exists path, content
end

Instance Method Details

#create_file_if_not_exists(path) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/editus/file.rb', line 28

def create_file_if_not_exists path
  if File.exist?(path)
    File.read(path)
  else
    data = JSON.generate([])
    FileUtils.mkdir_p(File.dirname(path))
    File.open(path, "w") do |file|
      file.puts data
    end

    data
  end
end

#write_file_if_not_exists(path, content) ⇒ Object



17
18
19
20
21
22
23
24
25
26
# File 'lib/editus/file.rb', line 17

def write_file_if_not_exists path, content
  if File.exist?(path)
    File.write(path, content)
  else
    FileUtils.mkdir_p(File.dirname(path))
    File.open(path, "w") do |file|
      file.puts content
    end
  end
end