diff options
author | David Walter Seikel | 2014-03-26 16:59:47 +1000 |
---|---|---|
committer | David Walter Seikel | 2014-03-26 16:59:47 +1000 |
commit | dae9a662c23724749d78cda42f9f14bd4b450b66 (patch) | |
tree | 625852314a6c467dbe3ee4c5162da8e82b47a848 | |
parent | Change Thing to being fully a proxy table, coz __newindex() only works for ni... (diff) | |
download | SledjHamr-dae9a662c23724749d78cda42f9f14bd4b450b66.zip SledjHamr-dae9a662c23724749d78cda42f9f14bd4b450b66.tar.gz SledjHamr-dae9a662c23724749d78cda42f9f14bd4b450b66.tar.bz2 SledjHamr-dae9a662c23724749d78cda42f9f14bd4b450b66.tar.xz |
Write, use, and test isValid().
-rw-r--r-- | ClientHamr/GuiLua/skang.lua | 24 | ||||
-rw-r--r-- | ClientHamr/GuiLua/test.lua | 13 |
2 files changed, 30 insertions, 7 deletions
diff --git a/ClientHamr/GuiLua/skang.lua b/ClientHamr/GuiLua/skang.lua index f88ff1c..cb97a76 100644 --- a/ClientHamr/GuiLua/skang.lua +++ b/ClientHamr/GuiLua/skang.lua | |||
@@ -217,10 +217,17 @@ Thing.append = function (self,data) -- Append to the value of this Thing. | |||
217 | end | 217 | end |
218 | 218 | ||
219 | Thing.isValid = function (self) -- Check if this Thing is valid, return resulting error messages in errors. | 219 | Thing.isValid = function (self) -- Check if this Thing is valid, return resulting error messages in errors. |
220 | self.errors = {} | 220 | -- Anything that overrides this method, should call this super method first. |
221 | -- TODO - Should check for required, matching mask, matching type, etc. | 221 | local value = self.module[self.names[1] ] |
222 | return true | 222 | self.errors = {} |
223 | end | 223 | if 'nil' == type(value) then |
224 | if self.required then table.insert(self.errors, self.names[1] .. ' is required!') end | ||
225 | else | ||
226 | if self.types[1] ~= type(value) then table.insert(self.errors, self.names[1] .. ' should be a ' .. self.types[1] .. ', but it is a ' .. type(value) .. '!') end | ||
227 | end | ||
228 | -- TODO - Should check for matching mask, and anything else. | ||
229 | return #(self.errors) == 0 | ||
230 | end | ||
224 | 231 | ||
225 | Thing.remove = function (self) -- Delete this Thing. | 232 | Thing.remove = function (self) -- Delete this Thing. |
226 | end | 233 | end |
@@ -264,8 +271,13 @@ Thing.__newindex = function (table, key, value) | |||
264 | end | 271 | end |
265 | print(thing.module._NAME .. '.' .. name .. '(' .. types .. ') -> ' .. thing.help) | 272 | print(thing.module._NAME .. '.' .. name .. '(' .. types .. ') -> ' .. thing.help) |
266 | else | 273 | else |
267 | thing:isValid() | 274 | -- NOTE - invalid values are still stored, this is by design. |
268 | print(thing.types[1] .. ' ' .. thing.module._NAME .. '.' .. name .. ' = ' .. (value or 'nil') .. ' -> ' .. thing.help) | 275 | if not thing:isValid() then |
276 | for i, v in ipairs(thing.errors) do | ||
277 | print('ERROR - ' .. v) | ||
278 | end | ||
279 | end | ||
280 | -- print(thing.types[1] .. ' ' .. thing.module._NAME .. '.' .. name .. ' = ' .. (value or 'nil') .. ' -> ' .. thing.help) | ||
269 | -- TODO - Go through it's linked things and set them to. | 281 | -- TODO - Go through it's linked things and set them to. |
270 | end | 282 | end |
271 | else | 283 | else |
diff --git a/ClientHamr/GuiLua/test.lua b/ClientHamr/GuiLua/test.lua index 97adf9d..bc595fe 100644 --- a/ClientHamr/GuiLua/test.lua +++ b/ClientHamr/GuiLua/test.lua | |||
@@ -30,7 +30,7 @@ print('code') | |||
30 | -- A variable that is private to this module. | 30 | -- A variable that is private to this module. |
31 | local foo | 31 | local foo |
32 | 32 | ||
33 | skang.thing(_M, 'fooble,f', 'Help text goes here', 1, nil, '"edit", "The fooble:", 1, 1, 10, 50') | 33 | skang.thing(_M, 'fooble,f', 'Help text goes here', 1, 'number', '"edit", "The fooble:", 1, 1, 10, 50', true) |
34 | skang.thing(_M, 'bar', 'Help text', "Default") | 34 | skang.thing(_M, 'bar', 'Help text', "Default") |
35 | 35 | ||
36 | -- We can use inline functions if we don't need the function internally. | 36 | -- We can use inline functions if we don't need the function internally. |
@@ -58,3 +58,14 @@ test.fooble = 42 | |||
58 | print('f is now ' .. test.fooble .. ' ' .. test.f) | 58 | print('f is now ' .. test.fooble .. ' ' .. test.f) |
59 | test.fooble = nil | 59 | test.fooble = nil |
60 | print('f is now ' .. test.fooble .. ' ' .. test.f) | 60 | print('f is now ' .. test.fooble .. ' ' .. test.f) |
61 | -- First, disable the default value, so we see "is required" errors. | ||
62 | skang.things.f.default = nil | ||
63 | test.fooble = 42 | ||
64 | test.fooble = 'Should fail.' | ||
65 | test.fooble = 42 | ||
66 | test.fooble = nil | ||
67 | test.fooble = nil | ||
68 | test.fooble = 42 | ||
69 | test.fooble = true | ||
70 | test.f = 42 | ||
71 | test.f = nil | ||