summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRemko Tronçon <git@el-tramo.be>2013-09-20 20:19:47 (GMT)
committerRemko Tronçon <git@el-tramo.be>2013-09-20 20:33:32 (GMT)
commit577c0c8fd30ffc0df718a8958eb7b8784ec3b0ac (patch)
tree95a7fabe2796cc2c234f0ab6f299ecdb4889d236 /Sluift/ElementConvertors
parent4a8cf892743284265bcc8bf9c6fbc4747aa86089 (diff)
downloadswift-577c0c8fd30ffc0df718a8958eb7b8784ec3b0ac.zip
swift-577c0c8fd30ffc0df718a8958eb7b8784ec3b0ac.tar.bz2
Sluift: Add convenient way for filling in forms
Change-Id: Ia265e8659e32f9e7a221d986625c830953a93ddc
Diffstat (limited to 'Sluift/ElementConvertors')
-rw-r--r--Sluift/ElementConvertors/FormConvertor.cpp36
1 files changed, 33 insertions, 3 deletions
diff --git a/Sluift/ElementConvertors/FormConvertor.cpp b/Sluift/ElementConvertors/FormConvertor.cpp
index 1720037..e44ca3e 100644
--- a/Sluift/ElementConvertors/FormConvertor.cpp
+++ b/Sluift/ElementConvertors/FormConvertor.cpp
@@ -18,7 +18,6 @@
using namespace Swift;
namespace {
- // TODO: add __newindex to set a field value
int formIndex(lua_State* L) {
lua_getfield(L, 1, "fields");
if (lua_type(L, -1) != LUA_TTABLE) {
@@ -37,6 +36,32 @@ namespace {
return 0;
}
+ int formNewIndex(lua_State* L) {
+ lua_getfield(L, 1, "fields");
+ bool foundField = false;
+ if (lua_type(L, -1) == LUA_TTABLE) {
+ for (lua_pushnil(L); lua_next(L, -2) != 0; ) {
+ lua_getfield(L, -1, "name");
+ if (lua_equal(L, -1, 2)) {
+ lua_pushvalue(L, 3);
+ lua_setfield(L, -3, "value");
+ foundField = true;
+ lua_pop(L, 3);
+ break;
+ }
+ lua_pop(L, 2);
+ }
+ }
+ lua_pop(L, 1);
+
+ if (!foundField) {
+ lua_pushvalue(L, 2);
+ lua_pushvalue(L, 3);
+ lua_rawset(L, 1);
+ }
+ return 0;
+ }
+
Lua::Table convertFieldToLua(boost::shared_ptr<FormField> field) {
Lua::Table luaField = boost::assign::map_list_of("name", Lua::valueRef(field->getName()));
std::string type;
@@ -278,9 +303,12 @@ namespace {
}
Lua::pushValue(L, result);
+
lua_newtable(L);
lua_pushcfunction(L, formIndex);
lua_setfield(L, -2, "__index");
+ lua_pushcfunction(L, formNewIndex);
+ lua_setfield(L, -2, "__newindex");
lua_setmetatable(L, -2);
}
@@ -304,10 +332,12 @@ namespace {
field->setDescription("");
form->addField(field);
}
+ form->setType(Form::SubmitType);
// Convert back
convertFormToLua(L, form);
Lua::registerTableToString(L, -1);
+
return 1;
}
}
@@ -325,7 +355,7 @@ boost::shared_ptr<Form> FormConvertor::doConvertFromLua(lua_State* L) {
void FormConvertor::doConvertToLua(lua_State* L, boost::shared_ptr<Form> payload) {
convertFormToLua(L, payload);
+ lua_pushstring(L, "create_submission");
lua_pushcfunction(L, createSubmission);
- lua_setfield(L, -2, "create_submission");
-
+ lua_rawset(L, -3);
}