generated from ger619/ruby-template
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.rb
More file actions
114 lines (98 loc) · 2.3 KB
/
app.rb
File metadata and controls
114 lines (98 loc) · 2.3 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
require_relative './book_methods'
require_relative './music_methods'
require_relative './game_methods'
require_relative './file_helper'
require 'json'
class App
attr_accessor :book_list, :author_list, :music_list, :label_list, :game_list, :genre_list
def initialize()
@book_list = []
@author_list = []
@music_list = []
@label_list = []
@game_list = []
@genre_list = []
end
def book_display
list_books
end
def author_display
list_authors
end
def music_display
list_music
end
def label_display
list_labels
end
def game_display
list_games
end
def genre_display
list_genres
end
def book_create
create_book
end
def music_create
create_music
end
def game_create
create_game
end
def save_files
instance_variables.each do |var|
file_name = var.to_s.chomp('_list').delete('@')
ary = []
instance_variable_get(var).each do |obj|
hash = { ref: obj, value: to_hash(obj) }
ary << hash
end
File.write("./data/#{file_name}.json", JSON.generate(ary))
end
end
def read_files
instance_variables.each do |var|
file_name = var.to_s.chomp('_list').delete('@')
if File.exist?("./data/#{file_name}.json") && File.read("./data/#{file_name}.json") != ''
ary = JSON.parse(File.read("./data/#{file_name}.json"))
case file_name
when 'book'
read_book(ary)
when 'music'
read_music(ary)
when 'game'
read_game(ary)
end
else
File.write("./data/#{file_name}.json", '[]')
end
end
end
def read_files2
instance_variables.each do |var|
file_name = var.to_s.chomp('_list').delete('@')
if File.exist?("./data/#{file_name}.json") && File.read("./data/#{file_name}.json") != ''
ary = JSON.parse(File.read("./data/#{file_name}.json"))
case file_name
when 'author'
read_author(ary)
when 'label'
read_label(ary)
when 'genre'
read_genre(ary)
end
else
File.write("./data/#{file_name}.json", '[]')
end
end
end
private
def to_hash(obj)
hash = {}
obj.instance_variables.each do |var|
hash[var.to_s.delete('@')] = obj.instance_variable_get(var)
end
hash
end
end