summaryrefslogtreecommitdiffstats
path: root/acf/model/field.lua
diff options
context:
space:
mode:
authorKaarle Ritvanen <kaarle.ritvanen@datakunkku.fi>2013-06-28 23:31:21 +0300
committerKaarle Ritvanen <kaarle.ritvanen@datakunkku.fi>2013-09-04 15:24:08 +0300
commit25ffc62a5b2c9a65e5c1689d5351adcf8cbef7e2 (patch)
tree7703761a8c13778b4aa6ef07d64628e760d6ba6f /acf/model/field.lua
parent63942c3e11107bb1f3f5d874a2f3b694bb510f39 (diff)
downloadaconf-25ffc62a5b2c9a65e5c1689d5351adcf8cbef7e2.tar.bz2
aconf-25ffc62a5b2c9a65e5c1689d5351adcf8cbef7e2.tar.xz
eliminate deprecated module style
Diffstat (limited to 'acf/model/field.lua')
-rw-r--r--acf/model/field.lua91
1 files changed, 47 insertions, 44 deletions
diff --git a/acf/model/field.lua b/acf/model/field.lua
index c69b4d7..d4e934c 100644
--- a/acf/model/field.lua
+++ b/acf/model/field.lua
@@ -3,7 +3,7 @@ Copyright (c) 2012-2013 Kaarle Ritvanen
See LICENSE file for license details
--]]
-module(..., package.seeall)
+local M = {}
local err = require('acf.error')
local raise = err.raise
@@ -22,22 +22,22 @@ local function contains(list, value)
return false
end
-Member = class()
+M.Member = class()
-function Member:init(params)
+function M.Member:init(params)
for k, v in pairs(params or {}) do
if self[k] == nil then self[k] = v end
end
end
-function Member:auto_ui_name(name)
+function M.Member:auto_ui_name(name)
if not name then return end
return string.gsub(
string.upper(string.sub(name, 1, 1))..string.sub(name, 2), '-', ' '
)
end
-function Member:meta(context)
+function M.Member:meta(context)
return {
name=self.name,
description=self.description,
@@ -46,10 +46,10 @@ function Member:meta(context)
end
-Field = class(Member)
+M.Field = class(M.Member)
-function Field:init(params)
- super(self, Field):init(params)
+function M.Field:init(params)
+ super(self, M.Field):init(params)
if self.choice and not self['ui-choice'] then
self['ui-choice'] = util.map(
@@ -63,9 +63,9 @@ function Field:init(params)
end
end
-function Field:meta(context)
+function M.Field:meta(context)
assert(self.dtype)
- local res = super(self, Field):meta(context)
+ local res = super(self, M.Field):meta(context)
res.type = self.dtype
res.required = self.required
@@ -77,20 +77,20 @@ function Field:meta(context)
return res
end
-function Field:topology(context)
+function M.Field:topology(context)
return {
{path=context.path, addr=context.addr, type=self.dtype}
}
end
-function Field:load(context)
+function M.Field:load(context)
if not context.txn then return setmetatable({}, context) end
local value = context.txn:get(context.addr)
if value == nil then return self.default end
return value
end
-function Field:_validate(context, value)
+function M.Field:_validate(context, value)
if self.required and value == nil then
raise(context.path, 'Required value not set')
end
@@ -101,18 +101,18 @@ function Field:_validate(context, value)
return value
end
-function Field:validate(context, value) end
+function M.Field:validate(context, value) end
-function Field:save(context, value)
+function M.Field:save(context, value)
context.txn:set(context.addr, self:_validate(context, value))
end
-function Field:validate_saved(context)
+function M.Field:validate_saved(context)
self:save(context, self:load(context))
end
-local Primitive = class(Field)
+local Primitive = class(M.Field)
function Primitive:validate(context, value)
local t = self.dtype
@@ -120,76 +120,76 @@ function Primitive:validate(context, value)
end
-String = class(Primitive)
+M.String = class(Primitive)
-function String:init(params)
- super(self, String):init(params)
+function M.String:init(params)
+ super(self, M.String):init(params)
self.dtype = 'string'
end
-function String:validate(context, value)
- super(self, String):validate(context, value)
+function M.String:validate(context, value)
+ super(self, M.String):validate(context, value)
if self['max-length'] and string.len(value) > self['max-length'] then
raise(context.path, 'Maximum length exceeded')
end
end
-function String:meta(context)
- local res = super(self, String):meta(context)
+function M.String:meta(context)
+ local res = super(self, M.String):meta(context)
res['max-length'] = self['max-length']
return res
end
-Number = class(Primitive)
+M.Number = class(Primitive)
-function Number:init(params)
- super(self, Number):init(params)
+function M.Number:init(params)
+ super(self, M.Number):init(params)
self.dtype = 'number'
end
-function Number:_validate(context, value)
- return super(self, Number):_validate(
+function M.Number:_validate(context, value)
+ return super(self, M.Number):_validate(
context,
value and tonumber(value) or value
)
end
-Integer = class(Number)
+M.Integer = class(M.Number)
-function Integer:validate(context, value)
- super(self, Integer):validate(context, value)
+function M.Integer:validate(context, value)
+ super(self, M.Integer):validate(context, value)
if math.floor(value) ~= value then raise(context.path, 'Not an integer') end
end
-Boolean = class(Primitive)
+M.Boolean = class(Primitive)
-function Boolean:init(params)
- super(self, Boolean):init(params)
+function M.Boolean:init(params)
+ super(self, M.Boolean):init(params)
self.dtype = 'boolean'
self.widget = self.dtype
end
-TreeNode = class(Field)
+M.TreeNode = class(M.Field)
-function TreeNode:topology(context)
- local res = super(self, TreeNode):topology(context)
+function M.TreeNode:topology(context)
+ local res = super(self, M.TreeNode):topology(context)
res[1].type = 'table'
util.extend(res, node.topology(self:load(context, true)))
return res
end
-function TreeNode:load(context, create)
+function M.TreeNode:load(context, create)
if context.txn and not (
create or self.create or context.txn:get(context.addr)
) then return end
return self.itype(context, self.iparams)
end
-function TreeNode:save(context, value)
+function M.TreeNode:save(context, value)
local path = context.path
if value == path then return end
@@ -216,13 +216,13 @@ function TreeNode:save(context, value)
end
end
-function TreeNode.save_member(node, k, v) node[k] = v end
+function M.TreeNode.save_member(node, k, v) node[k] = v end
-Model = class(TreeNode)
+M.Model = class(M.TreeNode)
-function Model:init(params)
- super(self, Model):init(params)
+function M.Model:init(params)
+ super(self, M.Model):init(params)
assert(self.model)
self.itype = self.model
@@ -230,3 +230,6 @@ function Model:init(params)
self.dtype = 'model'
self.widget = self.dtype
end
+
+
+return M