Sha256: 58033b804a184c50118ae844079efe4114cd4e3f6a8aeaa9b104105689699410

Contents?: true

Size: 1.04 KB

Versions: 2

Compression:

Stored size: 1.04 KB

Contents

# frozen_string_literal: true

module CodebrekerManfly
  class CodebreakerStore
    STORAGE_DIRECTORY = 'db'
    STORAGE_FILE = 'db.yml'
    attr_accessor :data

    def initialize
      @data = db_initialized? ? load : initialize_db
    end

    def save
      store = YAML::Store.new(storage_path)
      store.transaction { @data.each { |key, value| store[key] = value } }
    end

    private

    def db_initialized?
      Dir.exist?(STORAGE_DIRECTORY) && File.file?(File.join(STORAGE_DIRECTORY, STORAGE_FILE))
    end

    def initialize_db
      Dir.mkdir(STORAGE_DIRECTORY)

      store = YAML::Store.new(File.join(STORAGE_DIRECTORY, STORAGE_FILE))
      store.transaction { default_data.each { |key, value| store[key] = value } }

      default_data
    end

    def default_data
      {
        user_statistics: []
      }
    end

    def storage_path
      File.join(STORAGE_DIRECTORY, STORAGE_FILE)
    end

    def load
      store = YAML::Store.new(storage_path)
      store.transaction { store.roots.to_h { |key| [key, store[key]] } }
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
codebreker_manfly-0.1.2 lib/codebreker_manfly/entities/codebreaker_store.rb
codebreker_manfly-0.1.1 lib/codebreker_manfly/entities/codebreaker_store.rb