Class: Eddy::Data::Persistence::File
- Defined in:
- lib/eddy/data/persistence/file.rb
Overview
Persist data to a local JSON file.
Instance Method Summary collapse
- #initialize ⇒ void constructor
-
#path ⇒ Pathname
Renturn a Pathname to the JSON file used for storage.
-
#read ⇒ void
Read the JSON file into
@data
. -
#write ⇒ void
Write
@data
out to the JSON file.
Methods inherited from Memory
#add_interchange_control_number, #add_transaction_set_control_number, #interchange_control_numbers, #reset, #transaction_set_control_numbers
Methods inherited from Base
#add_interchange_control_number, #add_transaction_set_control_number, #interchange_control_numbers, #reset, #transaction_set_control_numbers
Constructor Details
#initialize ⇒ void
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
#path ⇒ 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 |
#read ⇒ void
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 |
#write ⇒ void
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 |