Class: Codebreaker::FileLoader

Inherits:
Object
  • Object
show all
Includes:
Validation
Defined in:
lib/codebreaker/file_loader.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) ⇒ FileLoader

Returns a new instance of FileLoader.



9
10
11
# File 'lib/codebreaker/file_loader.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/file_loader.rb', line 26

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

#loadObject



13
14
15
16
17
18
19
# File 'lib/codebreaker/file_loader.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/file_loader.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