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 = {}
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
settings = {
get: (name, skip_index=false) ->
get: (name, skip_index) ->
unless name
return settings.load!
unless skip_index -- for metamethods to not loop endlessly
return settings[name] if settings[name]
setting = Settings\find :name
unless setting
setting = Settings\create :name
cache[name] = setting
val = totype setting.value
settings[name] = val
return val
setting, err = get name
if setting
value = totype setting.value
settings[name] = value
return value
else
return nil, err
set: (name, value) ->
unless name
return settings.save!
setting = cache[name]
unless setting
setting = Settings\find :name
unless setting
setting = Settings\create :name
cache[name] = setting
if value ~= nil
setting, err = get name
if setting
settings[name] = value
return setting\update value: tostring value
else
return setting\update value: tostring settings[name]
return nil, err
save: ->
save: (name) ->
if name
setting, err = get name
if setting
return setting\update value: tostring settings[name]
else
return nil, err
else
for name, value in pairs settings
unless "function" == type value
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 _, setting in pairs cache
setting\update value: tostring settings[setting.name]
for name, setting in pairs cache
_, err = setting\update value: tostring settings[name]
return nil, err if err
return true
load: ->
load: (name) ->
return settings.get name if name
all_settings = Settings\select "WHERE true"
for setting in *all_settings
cache[setting.name] = setting
settings[setting.name] = totype setting.value
name = setting.name
cache[name] = setting
settings[name] = totype setting.value
return settings
delete: (name) ->
setting = cache[name]
unless setting
setting = Settings\find :name
if setting
setting\delete!
if setting = get name, false
if setting\delete!
cache[name] = nil
settings[name] = nil
else
return nil, "failed to delete '#{name}' setting"
return true
}
return setmetatable settings, {