Class: Codebreaker::SaveRes

Inherits:
Object
  • Object
show all
Includes:
Validation
Defined in:
lib/codebreaker/save_res.rb,
lib/codebreaker/module/save_res.rb

Constant Summary collapse

FILE_PATH =
"#{ENV["DB_PATH"]}#{ENV["DB_FILE"]}"

Constants included from Validation

Validation::ERROR_MESSAGES

Instance Method Summary collapse

Methods included from Validation

#raise_error, #validate_difficulty, #validate_file_existens, #validate_guess, #validate_hints, #validate_user_name

Constructor Details

#initialize(db_file_path = FILE_PATH) ⇒ SaveRes



9
10
11
# File 'lib/codebreaker/save_res.rb', line 9

def initialize(db_file_path = FILE_PATH)
  @db_file_path = db_file_path
end

Instance Method Details

#create_directory(path) ⇒ Object



26
27
28
# File 'lib/codebreaker/save_res.rb', line 26

def create_directory(path)
  Dir.mkdir(ENV[path])
end

#loadObject



13
14
15
16
17
18
19
# File 'lib/codebreaker/save_res.rb', line 13

def load
  validate_file_existens(FILE_PATH)

  File.open(FILE_PATH, "r") do |file|
    YAML.load_stream(file)
  end
end

#save(object) ⇒ Object



21
22
23
24
# File 'lib/codebreaker/save_res.rb', line 21

def save(object)
  create_directory("DB_PATH") unless Dir.exist?(ENV["DB_PATH"])
  File.open(FILE_PATH, "a") { |file| file.write(object.to_yaml) }
end