class CodebreakerGem::Statistic

Constants

DB_DIR
STATISTIC_FILE_NAME
STATISTIC_YML
YML_FORMAT

Public Class Methods

new() click to toggle source
# File lib/app/entities/statistic.rb, line 11
def initialize
  @table = I18n.t(:table)
end

Public Instance Methods

rating_table() click to toggle source
# File lib/app/entities/statistic.rb, line 20
def rating_table
  table = Terminal::Table.new
  table.title = @table[:title]
  table.headings = @table[:headings]
  table.rows = table_rows
  table
end
save(player, score) click to toggle source
# File lib/app/entities/statistic.rb, line 15
def save(player, score)
  new_record = record(player, score)
  save_to_file(new_record)
end

Private Instance Methods

load() click to toggle source
# File lib/app/entities/statistic.rb, line 49
def load
  YAML.load_stream(File.read(STATISTIC_YML)).sort_by { |statistic| statistic[:difficulty] }
end
record(player, score) click to toggle source
# File lib/app/entities/statistic.rb, line 30
def record(player, score)
  {
    name: player.name,
    difficulty: score.difficulty,
    total_attempts: score.total_attempts,
    used_attempts: score.used_attempts,
    total_hints: score.total_hints,
    used_hints: score.used_hints
  }
end
rows(record, index) click to toggle source
# File lib/app/entities/statistic.rb, line 53
def rows(record, index)
  [
    index + 1,
    record[:name],
    record[:difficulty],
    record[:total_attempts],
    record[:used_attempts],
    record[:total_hints],
    record[:used_hints]
  ]
end
save_to_file(record) click to toggle source
# File lib/app/entities/statistic.rb, line 41
def save_to_file(record)
  File.open(STATISTIC_YML, 'a') { |file| file.write record.to_yaml }
end
table_rows() click to toggle source
# File lib/app/entities/statistic.rb, line 45
def table_rows
  File.exist?(STATISTIC_YML) ? load.each_with_index.map { |record, index| rows(record, index) } : []
end