Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local json = require ("json")
- CURRENT_PATH = shell.dir()
- Config = {}
- function Config:new(name)
- local private = {}
- private.name = name
- local public = {}
- public.properties = {}
- function public:size()
- local count = 0
- for _, _ in pairs(public.properties) do
- count = count + 1
- end
- return count
- end
- function public:fetchFromFile()
- local file = io.open(CURRENT_PATH .. "/" .. private.name .. ".json", "r")
- if file == nil then
- return
- end
- io.input(file)
- local json_data_string = io.read("a");
- if (#json_data_string == 0) then
- io.close(file)
- return
- end
- public.properties = json.decode(json_data_string)
- io.close(file)
- end
- function public:updateFile()
- local file = io.open(CURRENT_PATH .. "/" .. private.name .. ".json", "w")
- if file == nil then
- return
- end
- io.output(file)
- local json_data = json.encode(public.properties)
- io.write(json_data)
- io.flush()
- io.close(file)
- end
- public:fetchFromFile()
- setmetatable(public, self)
- self.__index = self; return public
- end
- return {
- Config = Config
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement