Improve table hierarchy
Signed-off-by: Rahix <rahix@rahix.de>
This commit is contained in:
parent
f9a14ecaeb
commit
e328ae0ab9
2 changed files with 26 additions and 28 deletions
|
|
@ -9,7 +9,8 @@ module("bc-tests", package.seeall, lunit.testcase)
|
|||
-- Local {{{
|
||||
function test_construct()
|
||||
local function is_a_bc(bc)
|
||||
assert_true(bc.locals ~= nil)
|
||||
assert_true(bc.local_nouns ~= nil)
|
||||
assert_true(bc.local_verbs ~= nil)
|
||||
end
|
||||
|
||||
-- Short init
|
||||
|
|
@ -58,8 +59,8 @@ function test_available()
|
|||
bc:register("available2v", function() end)
|
||||
bc:finalize()
|
||||
|
||||
assert_equal(1234, bc.locals.nouns["available1"])
|
||||
assert_true(bc.locals.verbs["available2v"] ~= nil)
|
||||
assert_equal(1234, bc.local_nouns["available1"])
|
||||
assert_true(bc.local_verbs["available2v"] ~= nil)
|
||||
end
|
||||
|
||||
function test_get()
|
||||
|
|
|
|||
47
bc.lua
47
bc.lua
|
|
@ -83,14 +83,11 @@ local BaseControl = {
|
|||
|
||||
function BaseControl:new(self, network)
|
||||
local self = {
|
||||
locals = {
|
||||
nouns = {},
|
||||
verbs = {},
|
||||
},
|
||||
remotes = {
|
||||
nouns = {},
|
||||
verbs = {},
|
||||
},
|
||||
local_nouns = {},
|
||||
local_verbs = {},
|
||||
remote_nouns = {},
|
||||
remote_verbs = {},
|
||||
|
||||
live = false,
|
||||
network = network or Network:new(),
|
||||
}
|
||||
|
|
@ -107,16 +104,16 @@ function BaseControl:register(name, value)
|
|||
end
|
||||
|
||||
if type(value) == "function" then
|
||||
if self.locals.verbs[name] ~= nil then
|
||||
if self.local_verbs[name] ~= nil then
|
||||
error("\""..name.."\" already registered")
|
||||
else
|
||||
self.locals.verbs[name] = value
|
||||
self.local_verbs[name] = value
|
||||
end
|
||||
else
|
||||
if self.locals.nouns[name] ~= nil then
|
||||
if self.local_nouns[name] ~= nil then
|
||||
error("\""..name.."\" already registered")
|
||||
else
|
||||
self.locals.nouns[name] = value
|
||||
self.local_nouns[name] = value
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -134,12 +131,12 @@ end
|
|||
|
||||
-- Nouns {{{
|
||||
function BaseControl:has_noun(noun)
|
||||
return self.locals.nouns[noun] ~= nil or self.remotes.nouns[noun] ~= nil
|
||||
return self.local_nouns[noun] ~= nil or self.remote_nouns[noun] ~= nil
|
||||
end
|
||||
|
||||
function BaseControl:nouns(local_only)
|
||||
local nouns = {}
|
||||
for noun in pairs(self.locals.nouns) do
|
||||
for noun in pairs(self.local_nouns) do
|
||||
table.insert(nouns, noun)
|
||||
end
|
||||
-- TODO: Remote
|
||||
|
|
@ -147,9 +144,9 @@ function BaseControl:nouns(local_only)
|
|||
end
|
||||
|
||||
function BaseControl:get(noun, timeout)
|
||||
if self.locals.nouns[noun] ~= nil then
|
||||
return self.locals.nouns[noun]
|
||||
elseif self.locals.verbs[noun] ~= nil then
|
||||
if self.local_nouns[noun] ~= nil then
|
||||
return self.local_nouns[noun]
|
||||
elseif self.local_verbs[noun] ~= nil then
|
||||
error("\""..noun.."\" is not a noun")
|
||||
else
|
||||
return nil
|
||||
|
|
@ -157,17 +154,17 @@ function BaseControl:get(noun, timeout)
|
|||
end
|
||||
|
||||
function BaseControl:set(name, value)
|
||||
if self.locals.nouns[name] ~= nil then
|
||||
if self.local_nouns[name] ~= nil then
|
||||
if type(value) == "function" then
|
||||
error("\""..name.."\" can't be cast into a verb")
|
||||
else
|
||||
self.locals.nouns[name] = value
|
||||
self.local_nouns[name] = value
|
||||
end
|
||||
elseif self.locals.verbs[name] ~= nil then
|
||||
elseif self.local_verbs[name] ~= nil then
|
||||
if type(value) ~= "function" then
|
||||
error("\""..name.."\" can't be cast into a noun")
|
||||
else
|
||||
self.locals.verbs[name] = value
|
||||
self.local_verbs[name] = value
|
||||
end
|
||||
else
|
||||
error("\""..name.."\" is not a local")
|
||||
|
|
@ -177,12 +174,12 @@ end
|
|||
|
||||
-- Verbs {{{
|
||||
function BaseControl:has_verb(verb)
|
||||
return self.locals.verbs[verb] ~= nil or self.remotes.verbs[verb] ~= nil
|
||||
return self.local_verbs[verb] ~= nil or self.remote_verbs[verb] ~= nil
|
||||
end
|
||||
|
||||
function BaseControl:verbs(local_only)
|
||||
local verbs = {}
|
||||
for verb in pairs(self.locals.verbs) do
|
||||
for verb in pairs(self.local_verbs) do
|
||||
table.insert(verbs, verb)
|
||||
end
|
||||
-- TODO: Remote
|
||||
|
|
@ -190,8 +187,8 @@ function BaseControl:verbs(local_only)
|
|||
end
|
||||
|
||||
function BaseControl:call(verb, ...)
|
||||
if self.locals.verbs[verb] ~= nil then
|
||||
self.locals.verbs[verb](...)
|
||||
if self.local_verbs[verb] ~= nil then
|
||||
self.local_verbs[verb](...)
|
||||
else
|
||||
error("unknown verb")
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue