Sha256: 2aa0ab1980d43551505d00bdf5a066018c977257fbc21ec0776305e5ba8cb1e7

Contents?: true

Size: 981 Bytes

Versions: 6

Compression:

Stored size: 981 Bytes

Contents

# frozen_string_literal: true

module Codebreaker
  module Entities
    class DataStorage
      FILE_DIRECTORY = 'database'
      FILE_NAME = 'data.yml'

      def create
        ensuring_directory_availability
        File.new(File.join(FILE_DIRECTORY, FILE_NAME), 'w')
        File.write(File.join(FILE_DIRECTORY, FILE_NAME), [].to_yaml)
      end

      def load
        YAML.safe_load(File.open(File.join(FILE_DIRECTORY, FILE_NAME)), [Symbol, Time]) if storage_exist?
      end

      def save(object)
        File.open(File.join(FILE_DIRECTORY, FILE_NAME), 'w') { |file| file.write(YAML.dump(object)) }
      end

      def storage_exist?
        File.exist?(File.join(FILE_DIRECTORY, FILE_NAME))
      end

      def save_game_result(object)
        create unless storage_exist?
        save(load.push(object))
      end

      private

      def ensuring_directory_availability
        Dir.mkdir(FILE_DIRECTORY) unless Dir.exist?(FILE_DIRECTORY)
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
codebreaker_marian-0.3.6 lib/codebreaker_marian/entities/data_storage.rb
codebreaker_marian-0.3.5 lib/codebreaker_marian/entities/data_storage.rb
codebreaker_marian-0.3.4 lib/codebreaker_marian/entities/data_storage.rb
codebreaker_marian-0.3.3 lib/codebreaker_marian/entities/data_storage.rb
codebreaker_marian-0.3.2 lib/codebreaker_marian/entities/data_storage.rb
codebreaker_marian-0.3.1 lib/codebreaker_marian/entities/data_storage.rb