Class: GemCodebreaker::SerializerDeserializer

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

Instance Method Summary collapse

Constructor Details

#initializeSerializerDeserializer

Returns a new instance of SerializerDeserializer.



5
6
7
8
9
# File 'lib/gem_codebreaker/classes/serializer_deserializer.rb', line 5

def initialize
  @file_name = 'game_statistic.yml'
  @statistic_dir = 'data'
  @storage_path = File.expand_path("./#{@statistic_dir}/#{@file_name}")
end

Instance Method Details

#data_dir_getObject



11
12
13
14
# File 'lib/gem_codebreaker/classes/serializer_deserializer.rb', line 11

def data_dir_get
  data_directory = File.dirname(@storage_path)
  Dir.mkdir(data_directory) unless File.directory?(data_directory)
end

#load_game_statisticObject



16
17
18
# File 'lib/gem_codebreaker/classes/serializer_deserializer.rb', line 16

def load_game_statistic
  File.exist?(@storage_path) ? YAML.load_file(@storage_path) : []
end

#save_game_statistic(game_statistic) ⇒ Object



20
21
22
23
24
25
# File 'lib/gem_codebreaker/classes/serializer_deserializer.rb', line 20

def save_game_statistic(game_statistic)
  save_game_stat = game_statistic | load_game_statistic
  File.open(@storage_path, 'w') do |file|
    YAML.dump(save_game_stat, file)
  end
end