Class: Codebreaker::Statistics

Inherits:
Object
  • Object
show all
Includes:
Constants::Shared
Defined in:
lib/codebreaker/statistics.rb

Constant Summary collapse

FILENAME =
'default.yml'

Constants included from Constants::Shared

Constants::Shared::CORRECT_RANGE, Constants::Shared::LENGTH_GOOD, Constants::Shared::REGULAR_FOR_CODE, Constants::Shared::TYPE_OF_DIFFICULTY, Constants::Shared::WINNING_INDEX

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename = FILENAME) ⇒ Statistics

Returns a new instance of Statistics.



11
12
13
14
# File 'lib/codebreaker/statistics.rb', line 11

def initialize(filename = FILENAME)
  @filename = filename
  @data_store = []
end

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename.



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

def filename
  @filename
end

Instance Method Details

#add_to_data_store(game) ⇒ Object



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

def add_to_data_store(game)
  @data_store.push(data_to_save(game))
end

#save_unitObject



20
21
22
# File 'lib/codebreaker/statistics.rb', line 20

def save_unit
  @data_store.empty? ? raise(StandardError, I18n.t(:empty_data)) : File.write(@filename, @data_store.to_yaml)
end

#showObject



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

def show
  file_valid? ? sort_store(YAML.load_file(@filename)) : raise(StandardError, I18n.t(:bad_file))
end