Sha256: cfc14bc44d9eb4d36a6e6af3e920c0b273cad5326387375d8a2db6ae75e5dbbe

Contents?: true

Size: 1.24 KB

Versions: 15

Compression:

Stored size: 1.24 KB

Contents

require "fileutils"
require "json"
require "pathname"

module Eddy
  module Data
    module Persistence
      # Persist data to a local JSON file.
      class File < Memory

        # @return [void]
        def initialize()
          if path.file?
            self.read()
          else
            super()
          end
        end

        # Renturn a [Pathname](https://ruby-doc.org/stdlib-2.5.0/libdoc/pathname/rdoc/Pathname.html) to the JSON file used for storage.
        #
        # See:
        #
        # - [FileTest](https://ruby-doc.org/core-2.5.0/FileTest.html#method-i-file-3F)
        #
        # @return [Pathname]
        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 the JSON file into `@data`.
        #
        # @return [void]
        def read()
          @data = JSON.parse(File.read(self.path()), symbolize_names: symbolize)
        end

        # Write `@data` out to the JSON file. This will overwrite the file's contents.
        #
        # @return [void]
        def write()
          File.open(self.path(), "w") { |f| f.write(@data.to_json) }
        end

      end
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
eddy-0.10.0 lib/eddy/data/persistence/file.rb
eddy-0.9.2 lib/eddy/data/persistence/file.rb
eddy-0.9.1 lib/eddy/data/persistence/file.rb
eddy-0.9.0 lib/eddy/data/persistence/file.rb
eddy-0.8.4 lib/eddy/data/persistence/file.rb
eddy-0.8.3 lib/eddy/data/persistence/file.rb
eddy-0.8.2 lib/eddy/data/persistence/file.rb
eddy-0.8.1 lib/eddy/data/persistence/file.rb
eddy-0.8.0 lib/eddy/data/persistence/file.rb
eddy-0.7.0 lib/eddy/data/persistence/file.rb
eddy-0.6.0 lib/eddy/data/persistence/file.rb
eddy-0.5.1 lib/eddy/data/persistence/file.rb
eddy-0.5.0 lib/eddy/data/persistence/file.rb
eddy-0.4.0 lib/eddy/data/persistence/file.rb
eddy-0.3.0 lib/eddy/data/persistence/file.rb