mirror of
https://github.com/lazuscripts/utility.git
synced 2024-11-16 03:14:21 +00:00
added error handling & you can save nil now
This commit is contained in:
parent
50d0770cfc
commit
7b7003b77a
@ -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) ->
|
||||||
|
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
|
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]
|
unless cache[name]
|
||||||
cache[name] = Settings\find :name
|
cache[name] = Settings\find :name
|
||||||
unless cache[name]
|
unless cache[name]
|
||||||
cache[name] = Settings\create :name
|
cache[name] = Settings\create :name
|
||||||
|
|
||||||
for _, setting in pairs cache
|
for name, setting in pairs cache
|
||||||
setting\update value: tostring settings[setting.name]
|
_, 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
|
|
||||||
if setting
|
|
||||||
setting\delete!
|
|
||||||
cache[name] = nil
|
cache[name] = nil
|
||||||
settings[name] = nil
|
settings[name] = nil
|
||||||
|
else
|
||||||
|
return nil, "failed to delete '#{name}' setting"
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
return setmetatable settings, {
|
return setmetatable settings, {
|
||||||
|
Loading…
Reference in New Issue
Block a user