--[[ Copyright (c) 2012-2013 Kaarle Ritvanen See LICENSE file for license details --]] module(..., package.seeall) local err = require('acf.error') local raise = err.raise local node = require('acf.model.node') local object = require('acf.object') local class = object.class local super = object.super local map = require('acf.util').map local function contains(list, value) for k, v in ipairs(list) do if v == value then return true end end return false end local function 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 Field = class() function Field:init(params) for k, v in pairs(params or {}) do if self[k] == nil then self[k] = v end end if self.choice and not self['ui-choice'] then self['ui-choice'] = map(auto_ui_name, self.choice) end if not self.widget then self.widget = self.choice and 'combobox' or 'field' end end function Field:meta(context) assert(self.dtype) return { name=self.name, type=self.dtype, required=self.required, default=self.default, choice=self.choice, description=self.description, ['ui-name']=self['ui-name'] or auto_ui_name(self.name), widget=self.widget, ['ui-choice']=self['ui-choice'] } end function Field:load(context) local value = context.txn:get(context.addr) if value == nil then return self.default end return value end function Field:_validate(context, value) if self.required and value == nil then raise(context.path, 'Required value not set') end if self.choice and value ~= nil and not contains(self.choice, value) then raise(context.path, 'Invalid value') end if value ~= nil then self:validate(context, value) end return value end function Field:validate(context, value) end function Field:save(context, value) -- 2nd argument currenly not much used by backends context.txn:set(context.addr, self.dtype, self:_validate(context, value)) end function Field:validate_saved(context) self:save(context, self:load(context)) end TreeNode = class(Field) function TreeNode:save(context, value) local path = context.path -- TODO hack, allow preserving old instance on parent update if value == path then return end if object.isinstance(value, node.TreeNode) then -- TODO clone if TreeNode has wrong path if node.path(value) ~= path then raise(path, 'Attempted to assign foreign object as value') end return end context.txn:set(context.addr) if value then if type(value) ~= 'table' then raise(path, 'Cannot assign primitive value') end context.txn:set(context.addr, 'table') local new = self:load(context, true) local errors = err.ErrorDict() for k, v in pairs(value) do errors:collect(self.save_member, new, k, v) end errors:raise() end end function TreeNode.save_member(node, k, v) node[k] = v end Model = class(TreeNode) function Model:init(params) super(self, Model):init(params) assert(self.model) self.dtype = 'model' self.widget = self.dtype end function Model:load(context, create) if not create and not context.txn:get(context.addr) then return end return self.model(context) end