summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Silverstone <dsilvers@digital-scurf.org>2012-08-23 11:37:19 +0100
committerDaniel Silverstone <dsilvers@digital-scurf.org>2012-08-23 11:37:19 +0100
commit493d34935bea7313e8d0b753ffacc3a49fc6ea22 (patch)
tree42bfde0fbb25e8bec821f1a33e972458ea21350d
parent0365e1bb27e6bedcba4b9b62baaa08d60115ca9a (diff)
downloadclod-493d34935bea7313e8d0b753ffacc3a49fc6ea22.tar.gz
Basic value setting now works
-rw-r--r--examples/change-setting.lua9
-rw-r--r--lib/clod.lua51
2 files changed, 56 insertions, 4 deletions
diff --git a/examples/change-setting.lua b/examples/change-setting.lua
index bff5889..3dd646e 100644
--- a/examples/change-setting.lua
+++ b/examples/change-setting.lua
@@ -14,11 +14,14 @@ project.head "refs/heads/jeff"
other.thing "here"
]]
-conf = clod.parse(input_config)
+conf, err = clod.parse(input_config)
+if not conf then
+ error(err)
+end
conf.settings.project.name = "Clod"
-conf.settings.project.head = "refs/heads/jeff"
+conf.settings.project.head = "refs/heads/master"
conf.settings.project.description = "Demonstration of settings"
-print(conf:serialise())
+io.stdout:write(conf:serialise())
diff --git a/lib/clod.lua b/lib/clod.lua
index 7058ab3..4cabbf0 100644
--- a/lib/clod.lua
+++ b/lib/clod.lua
@@ -40,6 +40,55 @@ function settings_mt:__index(subkey)
end
end
+function settings_mt:__newindex(subkey, value)
+ local meta = metadata[self]
+ local confmeta = metadata[meta.conf]
+ local key = subkey
+ if meta.prefix then
+ key = meta.prefix .. "." .. subkey
+ end
+ if value == nil then
+ -- removing an entry...
+ if confmeta.settings[key] then
+ -- Need to remove *this* entry
+ local entry = confmeta.settings[key]
+ if entry == confmeta.entries then
+ -- First entry, move the head along
+ confmeta.entries = entry.next
+ end
+ if entry.prev then
+ entry.prev.next = entry.next
+ end
+ if entry.next then
+ entry.next.prev = entry.prev
+ end
+ -- Shuffle all lines from here on down away
+ entry = entry.next
+ while entry do
+ entry.lineno = entry.lineno - 1
+ entry = entry.next
+ end
+ end
+ elseif confmeta.settings[key] then
+ -- Replacing extant entry
+ confmeta.settings[key].value = value
+ else
+ -- Inventing a new entry, we will put it at the end
+ local last = confmeta.entries
+ while last.next do
+ last = last.next
+ end
+ local new_entry = {
+ key = key,
+ value = value,
+ lineno = last.lineno + 1,
+ prev = last
+ }
+ last.next = new_entry
+ confmeta.settings[key] = new_entry
+ end
+end
+
-- Helper routines
function gen_settings(tab, prefix)
local meta = metadata[tab]
@@ -113,7 +162,7 @@ local function parse_config(conf, confname)
-- A 'local' indexing, so combine with the key
return gen_hook(("%s.%s"):format(prefix, key))
end
- function parse_mt:__setindex(key, value)
+ function parse_mt:__newindex(key, value)
-- This is the equivalent of 'foo = "bar"' instead of 'foo "bar"'
return self[key](value)
end