Class: Eddy::Data::Persistence::File

Inherits:
Memory
  • Object
show all
Defined in:
lib/eddy/data/persistence/file.rb

Overview

Persist data to a local JSON file.

Instance Method Summary collapse

Methods inherited from Memory

#add_interchange_control_number, #add_transaction_set_control_number, #interchange_control_numbers, #transaction_set_control_numbers

Methods inherited from Base

#add_interchange_control_number, #add_transaction_set_control_number, #interchange_control_numbers, #transaction_set_control_numbers

Constructor Details

#initializevoid



12
13
14
15
16
17
18
# File 'lib/eddy/data/persistence/file.rb', line 12

def initialize()
  if path.file?
    self.read()
  else
    super()
  end
end

Instance Method Details

#pathPathname

Renturn a Pathname to the JSON file used for storage.

See:

Returns:

  • (Pathname)


27
28
29
30
31
# 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

#readvoid

This method returns an undefined value.

Read the JSON file into @data.



36
37
38
# File 'lib/eddy/data/persistence/file.rb', line 36

def read()
  @data = JSON.parse(File.read(self.path()), symbolize_names: symbolize)
end

#writevoid

This method returns an undefined value.

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



43
44
45
# File 'lib/eddy/data/persistence/file.rb', line 43

def write()
  File.open(self.path(), "w") { |f| f.write(@data.to_json) }
end