-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson_manager.rb
More file actions
85 lines (78 loc) · 2.08 KB
/
Copy pathjson_manager.rb
File metadata and controls
85 lines (78 loc) · 2.08 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
# frozen_string_literal: false
# Handles reading and writing JSON data for each module
module JSONManager
def self.valid_filename?(name)
%r{\A[^\/\\\?%\*:|"<>]+\z}.match(name)
end
def self.init(folder_name)
Dir.mkdir(folder_name) unless File.directory?(folder_name)
@json_folder = folder_name
end
def self.write_json(subfolder, filename, json)
Dir.chdir(@json_folder) do
Dir.mkdir(subfolder) unless File.directory?(subfolder)
Dir.chdir(subfolder) do
File.open(filename, 'w') do |file|
file.syswrite(json)
end
end
end
end
def self.read_json(subfolder, filename)
return nil unless exist?(subfolder, filename)
json = nil
Dir.chdir(@json_folder) do
Dir.mkdir(subfolder) unless File.directory?(subfolder)
Dir.chdir(subfolder) do
File.open(filename, 'r') do |file|
file.each_line do |line|
json ||= ''
json = line
end
end
end
end
json
end
# Returns an array of all the first capture groups of the regex
def self.search(subfolder, regex)
matched = []
Dir.chdir(@json_folder) do
Dir.mkdir(subfolder) unless File.directory?(subfolder)
end
Dir.entries("./#{@json_folder}/#{subfolder}").each do |filename|
match_data = regex.match(filename)
matched.push(match_data.captures[0]) if match_data
end
matched
end
def self.exist?(subfolder, filename)
exists = nil
Dir.chdir(@json_folder) do
if File.directory?(subfolder)
Dir.chdir(subfolder) do
exists = File.exist?(filename)
end
end
end
exists
end
def self.delete_json(subfolder, filename)
json = nil
Dir.chdir(@json_folder) do
Dir.mkdir(subfolder) unless File.directory?(subfolder)
Dir.chdir(subfolder) do
if File.exist?(filename)
File.open(filename, 'r') do |file|
file.each_line do |line|
json ||= ''
json = line
end
end
File.delete(filename)
end
end
end
json
end
end