added error handling & you can save nil now

This commit is contained in:
Paul Liverman III 2018-03-09 22:57:29 -08:00
parent 50d0770cfc
commit 7b7003b77a

View File

@ -13,66 +13,93 @@ totype = (str) ->
cache = {} cache = {}
get = (name, create=true) ->
setting = cache[name]
unless setting
setting = Settings\find :name
if (not setting) and create
setting = Settings\create :name
cache[name] = setting
if setting
return setting
else
return nil, "failed to load '#{name}' setting"
local settings local settings
settings = { settings = {
get: (name, skip_index=false) -> get: (name, skip_index) ->
unless name unless name
return settings.load! return settings.load!
unless skip_index -- for metamethods to not loop endlessly unless skip_index -- for metamethods to not loop endlessly
return settings[name] if settings[name] return settings[name] if settings[name]
setting = Settings\find :name
unless setting setting, err = get name
setting = Settings\create :name if setting
cache[name] = setting value = totype setting.value
val = totype setting.value settings[name] = value
settings[name] = val return value
return val else
return nil, err
set: (name, value) -> set: (name, value) ->
unless name unless name
return settings.save! return settings.save!
setting = cache[name] setting, err = get name
unless setting if setting
setting = Settings\find :name
unless setting
setting = Settings\create :name
cache[name] = setting
if value ~= nil
settings[name] = value settings[name] = value
return setting\update value: tostring value return setting\update value: tostring value
else else
return setting\update value: tostring settings[name] return nil, err
save: -> save: (name) ->
for name, value in pairs settings if name
unless "function" == type value setting, err = get name
unless cache[name] if setting
cache[name] = Settings\find :name return setting\update value: tostring settings[name]
unless cache[name] else
cache[name] = Settings\create :name return nil, err
for _, setting in pairs cache else
setting\update value: tostring settings[setting.name] for name, value in pairs settings
switch name
when "get", "set", "save", "load", "delete"
nil
else
t = type value
if t == "function" or t == "table"
return nil, "cannot save '#{name}' setting, type '#{t}' not supported"
else
unless cache[name]
cache[name] = Settings\find :name
unless cache[name]
cache[name] = Settings\create :name
for name, setting in pairs cache
_, err = setting\update value: tostring settings[name]
return nil, err if err
return true return true
load: -> load: (name) ->
return settings.get name if name
all_settings = Settings\select "WHERE true" all_settings = Settings\select "WHERE true"
for setting in *all_settings for setting in *all_settings
cache[setting.name] = setting name = setting.name
settings[setting.name] = totype setting.value cache[name] = setting
settings[name] = totype setting.value
return settings return settings
delete: (name) -> delete: (name) ->
setting = cache[name] if setting = get name, false
unless setting if setting\delete!
setting = Settings\find :name cache[name] = nil
if setting settings[name] = nil
setting\delete! else
cache[name] = nil return nil, "failed to delete '#{name}' setting"
settings[name] = nil return true
} }
return setmetatable settings, { return setmetatable settings, {