Module: Codebreaker::FileStore

Included in:
Game, Statistics
Defined in:
lib/codebreaker/file_store.rb

Constant Summary collapse

FILE_DIRECTORY =
"statistics"
FILE_NAME =
"statistics.yml"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.statisticsObject

def statistics

load_file.each.sort_by { |game| [game[:attempts], game[:used_hints], game[:used_attempts]] }

end



24
25
26
# File 'lib/codebreaker/file_store.rb', line 24

def self.statistics
  load_file.each.sort_by { |game| [game[:attempts], game[:used_hints], game[:used_attempts]] }
end

Instance Method Details

#load_fileObject



15
16
17
18
# File 'lib/codebreaker/file_store.rb', line 15

def load_file
  create_storage unless storage_exists?
  (YAML.load_file(storage_path) || {})[:codebrakers] || []
end

#save_file(game) ⇒ Object



8
9
10
11
12
13
# File 'lib/codebreaker/file_store.rb', line 8

def save_file(game)
  validate_state(game.state, :win)
  score = load_file
  score << fill_game_data(game)
  File.write(storage_path, YAML.dump({ codebrakers: score }))
end