Class: Codebreaker::Statistic

Inherits:
Object
  • Object
show all
Defined in:
lib/codebreaker/statistic.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_path) ⇒ Statistic

Returns a new instance of Statistic.



7
8
9
# File 'lib/codebreaker/statistic.rb', line 7

def initialize(file_path)
  @file_path = file_path
end

Instance Attribute Details

#file_pathObject (readonly)

Returns the value of attribute file_path.



5
6
7
# File 'lib/codebreaker/statistic.rb', line 5

def file_path
  @file_path
end

Instance Method Details

#readObject



19
20
21
# File 'lib/codebreaker/statistic.rb', line 19

def read
  YAML.load_file(file_path)
end

#save(game) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/codebreaker/statistic.rb', line 11

def save(game)
  data = File.file?(file_path) && !File.zero?(file_path) ? read : []
  data << new_record(game)
  file = File.open(file_path, 'w')
  file.write(data.to_yaml)
  file.close
end