diff options
author | Kaarle Ritvanen <kaarle.ritvanen@datakunkku.fi> | 2013-10-08 18:50:56 +0300 |
---|---|---|
committer | Kaarle Ritvanen <kaarle.ritvanen@datakunkku.fi> | 2013-10-08 18:50:56 +0300 |
commit | 33728ad3382d74281412d4556561d479bb88832b (patch) | |
tree | e8a9b2798dec96d820715f729989e91f9e7d5e12 /acf2/model/combination.lua | |
parent | 3e48dd63e8bdf0c2641cfb73e6b20bea8c466ff8 (diff) | |
download | aconf-33728ad3382d74281412d4556561d479bb88832b.tar.bz2 aconf-33728ad3382d74281412d4556561d479bb88832b.tar.xz |
changed module paths from acf to acf2v0.1.0
Diffstat (limited to 'acf2/model/combination.lua')
-rw-r--r-- | acf2/model/combination.lua | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/acf2/model/combination.lua b/acf2/model/combination.lua new file mode 100644 index 0000000..28c1570 --- /dev/null +++ b/acf2/model/combination.lua @@ -0,0 +1,50 @@ +--[[ +Copyright (c) 2012-2013 Kaarle Ritvanen +See LICENSE file for license details +--]] + +local M = {} + +local err = require('acf2.error') +local raise = err.raise + +local fld = require('acf2.model.field') +local String = fld.String + +local to_field = require('acf2.model.model').to_field + +local object = require('acf2.object') +local class = object.class +local super = object.super + + +local stringy = require('stringy') + + +M.Range = class(String) + +function M.Range:init(params) + super(self, M.Range):init(params) + if not self.type then self.type = fld.Integer end +end + +function M.Range:validate(context, value) + local comps = stringy.split(value, '-') + if #comps > 2 then raise(context.path, 'Invalid range') end + for _, v in ipairs(comps) do to_field(self.type):_validate(context, v) end +end + + +M.Union = class(String) + +function M.Union:validate(context, value) + super(self, M.Union):validate(context, value) + for _, tpe in ipairs(self.types) do + local field = to_field(tpe) + if err.call(field.validate, field, context, value) then return end + end + raise(context.path, self.error or 'Invalid value') +end + + +return M |