Skip to content

Commit

Permalink
settings.get_table can now convert values.
Browse files Browse the repository at this point in the history
  • Loading branch information
RobertZenz committed Jan 9, 2016
1 parent cda2d8b commit 7cee378
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
3 changes: 2 additions & 1 deletion doc/modules/settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,8 @@ <h3>Parameters:</h3>
nil.
</li>
<li><span class="parameter">...</span>
The name of the keys.
The name of the keys, or a table with the name and the conversion
function.
</li>
</ul>

Expand Down
9 changes: 7 additions & 2 deletions mods/utils/settings.lua
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,8 @@ end
-- @param name The name of the value to get.
-- @param default_value The default value to return if the value is nil, can be
-- nil.
-- @param ... The name of the keys.
-- @param ... The name of the keys, or a table with the name and the conversion
-- function.
-- @return The table with the given name and the given keys, or the default
-- value it is nil.
function settings.get_table(name, default_value, ...)
Expand All @@ -175,7 +176,11 @@ function settings.get_table(name, default_value, ...)
if ... ~= nil then
for index, key in ipairs({...}) do
table[key] = value:get(index)
if type(key) == "table" then
table[key.name] = key.convert(value:get(index))
else
table[key] = value:get(index)
end
end
end
Expand Down
6 changes: 6 additions & 0 deletions test/settings.lua
Original file line number Diff line number Diff line change
Expand Up @@ -110,5 +110,11 @@ test.run("get_table", function()
test.equals({ a = "a", b = "b", c = "c", d = "d", e = "e" }, settings.get_table("list", nil, "a", "b", "c", "d", "e"))
test.equals({ a = "a", b = "b", [3] = "c", [4] = "d", [5] = "e" }, settings.get_table("list", nil, "a", "b"))
test.equals({ a = "a", b = "b", c = "c", d = "d", e = "e" }, settings.get_table("list", nil, "a", "b", "c", "d", "e", "f", "g", "h"))

local convert_function = function(value)
return tonumber(value) - 10
end

test.equals({ min = 33.23, max = 19.89 }, settings.get_table("pos2d", nil, { name = "min", convert = convert_function }, { name = "max", convert = convert_function }))
end)

0 comments on commit 7cee378

Please sign in to comment.