Joshua Wise |
|
I've also been doing some other random stuff lately. RiciLake suggested that I start documenting stuff, so as I find interesting things that I've done, I'll post them here. Like him, I can be found on the IRC channel, which is available at irc://irc.freenode.net/#lua -- my username is usually joshua_. If you're looking for me elsewhere on the web, try http://joshuawise.com/ .
require"lails" require"luasql.sqlite" env = luasql.sqlite() conn = env:connect("/home/joshua/mirror-objects.sqlite") db = lails.db:new(conn){ tables = { objects = { primkey = "hash" } } } test = db.objects dofile("objects.lua") conn:execute("BEGIN TRANSACTION;") for k,v in pairs(objects) do test[k] = { moddate = v.moddate, mimetype = v.mimetype, mirrored = v.mirrored, length = v.length, lastaccessed = v.lastaccessed, url = v.url } end conn:execute("COMMIT;")I whipped this up in about 25 minutes this afternoon. The neat bit about this is that when I convert the user of the datastore over, I won't really have to change much -- the only difference will be that I won't have to load and store the datastore, I can just act directly on the database. More on how Lails actually works later.
sqlite> INSERT INTO users (id, username) VALUES (1, "joshua"); sqlite> INSERT INTO users (id, username) VALUES (2, "someone"); sqlite> INSERT INTO acls (id, resource, user) VALUES (1, "/", 1); sqlite> INSERT INTO acls (id, resource, user) VALUES (2, "/home/someone", 2); sqlite> INSERT INTO acls (id, resource, user) VALUES (3, "/tmp", 2);Slick, huh? Did this 2007-02-04 (early in the morning; if you saw it mid day 2007-02-04, you're already out of date)
#define function main() { int i = 0; function --i; printf("Hello, world!\n"); } /* x() print("Hello, world!") end x() -- */
for k,v in pairs(files_in_directory) do dofile(v) client_command(...) client_command = nil endto someone on IRC. Get it from http://joshuawise.com/hooks.lua . A quick test for it is:
require"hooks" myhooks = {} myhooks.test = hooks:new() myhooks.ref1 = myhooks.test:insert(function (foo, bar) print("Test 1", foo, bar) end, 100) myhooks.ref2 = myhooks.test:insert(function (foo, bar) print("Test 2", foo, bar) end, 50) myhooks.ref3 = myhooks.test:insert(function (foo, bar) print("Test 3", foo, bar) end, 150) print"--" myhooks.test("Hello", "world") myhooks.test:remove(myhooks.ref1) print"--" myhooks.test("Hello", "world")Should prove useful. This is similar to how naim does its hooks, although all of the hook glue in naim happens in C. Remaining to do: provide support for writable arguments. Patches thoughtfully accepted. This is part of my "Joshua's Lua Hack of the Day" series, and was originally written on 2007-02-01.