1234567891011121314151617181920212223242526272829303132 |
- local schema = {}
- function schema.new(k, default_value)
- assert(k)
- if default_value then
- local self = {
- assert_get = function(t) return assert(t.getspecific(k).get()) end,
- assert_runtime = function(t) return assert(t.getspecific(k).runtime) end,
- persist = function(t) t.persist(k, function() t.getspecific(k).save() end)
- end,
- load = function(t)
- if t.readonly then
- rawset(t, 'persist', function(...) end)
- end
- return t.register_specific(k, function()
- return default_value
- end)
- end
- }
- return self
- else
- local self = {
- assert_get = function(t) return assert(false, k) end,
- assert_runtime = function(t) return assert(t.getspecific(k).runtime) end,
- persist = function(t) return assert(false, k) end,
- load = function(t) return t.register_specific(k) end
- }
- return self
- end
- end
- return schema
|