class JsonDB

Public Class Methods

load(file_path) click to toggle source
# File lib/json_db.rb, line 13
def self.load(file_path)
    File.open(file_path) do |the_file|
        JSON.parse(the_file.read())["data"]
    end
end
new(file_path) click to toggle source
# File lib/json_db.rb, line 4
def initialize(file_path)
    @data_file_path = file_path
    if not File.exists?(file_path)
        @data_hash_arr = []
        save()
        puts("create empty list file #{file_path}")
    end
    @data_hash_arr = JsonDB.load(@data_file_path)
end

Public Instance Methods

get_data() click to toggle source
# File lib/json_db.rb, line 21
def get_data()
    @data_hash_arr
end
reload() click to toggle source
# File lib/json_db.rb, line 18
def reload()
    @data_hash_arr = JsonDB.load(@data_file_path)
end
save() click to toggle source
# File lib/json_db.rb, line 27
def save()
    the_data = {"data"=>@data_hash_arr}
    File.open(@data_file_path,"w") do |the_file|
        the_file.write(JSON.pretty_generate(the_data))
    end
end
set_data(data_hash_arr) click to toggle source
# File lib/json_db.rb, line 24
def set_data(data_hash_arr)
    @data_hash_arr = data_hash_arr
end