-
Notifications
You must be signed in to change notification settings - Fork 2
/
storage.rb
82 lines (67 loc) · 1.85 KB
/
storage.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
require 'json'
class Storage
def self.create_files
files = %w[book.json music_album.json game.json]
files.each do |file|
File.open(file, 'w+') unless File.exist?(file)
end
end
def self.save_all(app)
save_music_album(app)
save_books(app)
save_games(app)
end
def self.load_all(app)
load_music_albums(app)
load_books(app)
load_games(app)
end
def self.save_books(app)
file = './book.json'
return unless File.exist?(file)
books = File.open(file, 'w')
books.write(JSON.generate(app.books))
books.close
end
def self.save_music_album(app)
file = './music_album.json'
return unless File.exist?(file)
albums = File.open(file, 'w')
albums.write(JSON.generate(app.music_albums))
albums.close
end
def self.save_games(app)
file = './game.json'
return unless File.exist?(file)
games = File.open(file, 'w')
games.write(JSON.generate(app.games))
games.close
end
def self.load_books(app)
file = './book.json'
return unless File.exist?(file)
return if File.zero?(file)
books_parse = JSON.parse(File.read(file))
books_parse.each do |book|
app.create_book(book['publish_date'], book['publisher'], book['cover_state'])
end
end
def self.load_music_albums(app)
file = './music_album.json'
return unless File.exist?(file)
return if File.zero?(file)
albums_parse = JSON.parse(File.read(file))
albums_parse.each do |album|
app.create_music_album(album['publish_date'], on_spotify: album['on_spotify'])
end
end
def self.load_games(app)
file = './game.json'
return unless File.exist?(file)
return if File.zero?(file)
games_parse = JSON.parse(File.read(file))
games_parse.each do |game|
app.create_game(game['multiplayer'], game['last_played_at'], game['publish_date'])
end
end
end