Created
May 16, 2011 16:25
-
-
Save ignacio/974770 to your computer and use it in GitHub Desktop.
Redis table with __index metamethod
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local fail_safe = { | |
__index = function(t, command) | |
error( ("No command defined for %q"):format(tostring(command)) ) | |
end | |
} | |
setmetatable(fail_safe, fail_safe) | |
-- You add commands here (internally) | |
local commands = { | |
get = function(t, key) | |
return 42 | |
end, | |
set = function(t, key, value) | |
end | |
} | |
setmetatable(commands, fail_safe) | |
local redis = setmetatable({}, { | |
__index = commands | |
}) | |
print( redis.get("x") ) | |
-- a non existing command | |
redis.foo() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment