forked from andrianarivo/catalog_of_my_things
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_manager.rb
More file actions
132 lines (115 loc) · 3.62 KB
/
data_manager.rb
File metadata and controls
132 lines (115 loc) · 3.62 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# DataManager module handles
# saving and loading data to and from JSON files
# Upon loading ie authors from a file, the app:
# reads the file
# creates author objects
# updates list of authors
# updates the linkings of authors to their associated items
require 'fileutils'
BOOK_FILE = 'data/books.json'.freeze
LABEL_FILE = 'data/labels.json'.freeze
MUSIC_ALBUM_FILE = 'data/music_album.json'.freeze
GENRE_FILE = 'data/genres.json'.freeze
GAME_FILE = 'data/games.json'.freeze
AUTHOR_FILE = 'data/authors.json'.freeze
module DataManager
def save_files
directory_name = 'data'
FileUtils.mkdir_p(directory_name)
File.write(BOOK_FILE, @books.to_json)
File.write(GAME_FILE, @games.to_json)
File.write(MUSIC_ALBUM_FILE, @music_albums.to_json)
File.write(AUTHOR_FILE, @authors.to_json)
File.write(LABEL_FILE, @labels.to_json)
File.write(GENRE_FILE, @genres.to_json)
end
# rubocop:disable Style/GuardClause
# load_ methods are being fired when the App initializes
def load_books
@books = []
if File.exist?(BOOK_FILE)
# this line reads the contents of BOOK_FILE
# JSON.parse converts each JSON formatted data to array of hashes
# map method iterates over each element of the array
JSON.parse(File.read(BOOK_FILE)).map do |book|
# for each book element and book_object is created
book_object = create_book_object(book)
# each book_object is appended to the @books instance variable
# `<<` shovel operator appends elements to an array
@books << book_object
@items << book_object
end
end
end
def load_games
@games = []
if File.exist?(GAME_FILE)
JSON.parse(File.read(GAME_FILE)).map do |game|
game_object = create_game_object(game)
@games << game_object
@items << game_object
end
end
end
def load_music_albums
@music_albums = []
if File.exist?(MUSIC_ALBUM_FILE)
JSON.parse(File.read(MUSIC_ALBUM_FILE)).map do |music|
publish_date = music['publish_date']
archived = music['archived'] || nil
on_spotify = music['on_spotify']
new_music = MusicAlbum.new(publish_date, on_spotify)
new_music.move_to_archive unless archived.nil?
@music_albums << new_music
@items << new_music
end
end
end
def load_labels
@labels = []
if File.exist?(LABEL_FILE)
JSON.parse(File.read(LABEL_FILE)).map do |label|
label_object = create_label_object(label)
@labels << label_object
end
end
end
def load_genres
@genres = []
if File.exist?(GENRE_FILE)
JSON.parse(File.read(GENRE_FILE)).map do |genre|
genre_object = create_genre_object(genre)
@genres << genre_object
end
end
end
def load_authors
@authors = []
if File.exist?(AUTHOR_FILE)
JSON.parse(File.read(AUTHOR_FILE)).map do |author|
author_object = create_author_object(author)
@authors << author_object
end
end
end
private
# rubocop:enable Style/GuardClause
def create_game_object(game)
Game.new(game['publish_date'], game['multiplayer'], game['last_played_at'])
end
def create_book_object(book)
Book.new(book['publish_date'], book['publisher'], book['cover_state'])
end
def create_music_object(music_album)
MusicAlbum.new(music_album['publish_date'], music_album['on_spotify'])
end
def create_label_object(label)
Label.new(label['title'], label['color'])
end
def create_genre_object(genre)
Genre.new(genre['name'])
end
def create_author_object(author)
Author.new(author['first_name'], author['last_name'])
end
end