Class: Codebreakergem::FileWorker

Inherits:
Object
  • Object
show all
Defined in:
lib/classes/file_worker.rb

Constant Summary collapse

PERMITTED_CLASSES =
[Symbol].freeze

Class Method Summary collapse

Class Method Details

.add_to_file(filename, data) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/classes/file_worker.rb', line 14

def add_to_file(filename, data)
  if File.file?(filename)
    data_in_file = YAML.safe_load(File.read(filename), PERMITTED_CLASSES, [], true)
    data_in_file.is_a?(Array) ? data_in_file << data : data_in_file = [data_in_file, data]
    write_to_file(filename, data_in_file)
  else
    write_to_file(filename, data)
  end
end

.read_from_file(filename) ⇒ Object



8
9
10
11
12
# File 'lib/classes/file_worker.rb', line 8

def read_from_file(filename)
  return unless File.file?(Game::FILE)

  YAML.safe_load(File.read(filename), PERMITTED_CLASSES, [], true)
end

.write_to_file(filename, data) ⇒ Object



24
25
26
# File 'lib/classes/file_worker.rb', line 24

def write_to_file(filename, data)
  File.open(filename, 'w') { |file| file.write(data.to_yaml) }
end