class Eddy::Data::Persistence::File

Persist data to a local JSON file.

Public Class Methods

new() click to toggle source

@return [void]

Calls superclass method
# File lib/eddy/data/persistence/file.rb, line 12
def initialize()
  if path.file?
    self.read()
  else
    super()
  end
end

Public Instance Methods

path() click to toggle source

Renturn a [Pathname](ruby-doc.org/stdlib-2.5.0/libdoc/pathname/rdoc/Pathname.html) to the JSON file used for storage.

See:

@return [Pathname]

# File lib/eddy/data/persistence/file.rb, line 27
def path()
  file = File.join(Eddy.config.tmp_dir, "eddy_persistent_data.json")
  # FileUtils.mkdir_p(File.dirname(file))
  return Pathname.new(file)
end
read() click to toggle source

Read the JSON file into `@data`.

@return [void]

# File lib/eddy/data/persistence/file.rb, line 36
def read()
  @data = JSON.parse(File.read(self.path()), symbolize_names: symbolize)
end
write() click to toggle source

Write `@data` out to the JSON file. This will overwrite the file's contents.

@return [void]

# File lib/eddy/data/persistence/file.rb, line 43
def write()
  File.open(self.path(), "w") { |f| f.write(@data.to_json) }
end