module Codebreaker::Storage

Attributes

storage_path[R]

Private Instance Methods

apply_external_path(external_path = false) click to toggle source
# File lib/codebreaker/storage.rb, line 9
def apply_external_path(external_path = false)
  raise ArgumentError, 'Invalid external path.' if external_path && !Dir.exist?(external_path)
  yml_file = 'scores.yml'

  @storage_path =
    if external_path
      "#{external_path}/#{yml_file}"
    else
      File.expand_path("./data/#{yml_file}", File.dirname(__FILE__))
    end
end
erase_data_file() click to toggle source
# File lib/codebreaker/storage.rb, line 37
def erase_data_file
  File.delete(storage_path)
end
load_game_data() click to toggle source
# File lib/codebreaker/storage.rb, line 21
def load_game_data
  YAML.load(File.open(storage_path, 'r')) rescue []
end
prepare_storage_dir() click to toggle source
# File lib/codebreaker/storage.rb, line 25
def prepare_storage_dir
  storage_dir = File.dirname(storage_path)
  Dir.mkdir(storage_dir) unless File.exist?(storage_dir)
end
save_to_yml() click to toggle source
# File lib/codebreaker/storage.rb, line 30
def save_to_yml
  scores_to_save = scores | load_game_data
  File.open(storage_path, 'w') do |file|
    file.write(YAML.dump(scores_to_save))
  end
end