Sha256: 0bd7fd45737c3762a1270accb3c943570bc446e9f8569d74e3c35789dd92e8e5

Contents?: true

Size: 757 Bytes

Versions: 2

Compression:

Stored size: 757 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)
      data = load_database << results
      write_data(data)
    end

    def load_database
      YAML.safe_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

2 entries across 2 versions & 1 rubygems

Version Path
codebreaker-Bulatkin-0.1.2 lib/modules/storage.rb
codebreaker-Bulatkin-0.1.1 lib/modules/storage.rb