Sha256: dcb07b1c1fa00e1c8e9aa3a3aed13320668c513ad64d914d781c2166a5a2aea6

Contents?: true

Size: 803 Bytes

Versions: 1

Compression:

Stored size: 803 Bytes

Contents

# frozen_string_literal: true

module Codebreaker
  module Storage
    PATH = 'db'
    FILE_NAME = 'data.yml'

    def save(results)
      FileUtils.mkdir_p(PATH) unless Dir.exist?(PATH)
      create_storage unless File.exist?(file_path)
      data = load_database << results
      write_data(data)
    end

    def load_database
      YAML.load(File.read(file_path))
    end

    def sort_player
      create_storage unless File.exist?(file_path)
      load_database.sort_by { |game| [game[:difficulty], game[:attempts_used], game[:hints_used]] }
    end

    private

    def file_path
      File.join(Dir.pwd, PATH, FILE_NAME)
    end

    def create_storage
      write_data([])
    end

    def write_data(data)
      File.open(file_path, 'w') { |file| file.write(data.to_yaml) }
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
codebreaker-Bulatkin-0.2.1 lib/modules/storage.rb