Class: Codebreaker::GameWriter

Inherits:
Object
  • Object
show all
Defined in:
lib/ep-codebreaker/game_writer.rb

Instance Method Summary collapse

Constructor Details

#initialize(file_name = nil) ⇒ GameWriter

Returns a new instance of GameWriter.



10
11
12
# File 'lib/ep-codebreaker/game_writer.rb', line 10

def initialize(file_name = nil)
  @file_name = file_name || FILE_NAME
end

Instance Method Details

#process_fileObject Also known as: load_scores



21
22
23
24
# File 'lib/ep-codebreaker/game_writer.rb', line 21

def process_file
  return [] if !File.exist?(@file_name) || File.zero?(@file_name)
  YAML.load_file(@file_name)
end

#write(player) ⇒ Object



14
15
16
17
18
19
# File 'lib/ep-codebreaker/game_writer.rb', line 14

def write(player)
  scores = process_file
  scores << player
  scores = scores.max_by(10, &:points)
  File.open(@file_name, 'w') { |f| f.write scores.to_yaml }
end