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 {{{
|
-- Local {{{
|
||||||
function test_construct()
|
function test_construct()
|
||||||
local function is_a_bc(bc)
|
local function is_a_bc(bc)
|
||||||
assert_true(bc.locals ~= nil)
|
assert_true(bc.local_nouns ~= nil)
|
||||||
|
assert_true(bc.local_verbs ~= nil)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Short init
|
-- Short init
|
||||||
|
|
@ -58,8 +59,8 @@ function test_available()
|
||||||
bc:register("available2v", function() end)
|
bc:register("available2v", function() end)
|
||||||
bc:finalize()
|
bc:finalize()
|
||||||
|
|
||||||
assert_equal(1234, bc.locals.nouns["available1"])
|
assert_equal(1234, bc.local_nouns["available1"])
|
||||||
assert_true(bc.locals.verbs["available2v"] ~= nil)
|
assert_true(bc.local_verbs["available2v"] ~= nil)
|
||||||
end
|
end
|
||||||
|
|
||||||
function test_get()
|
function test_get()
|
||||||
|
|
|
||||||
47
bc.lua
47
bc.lua
|
|
@ -83,14 +83,11 @@ local BaseControl = {
|
||||||
|
|
||||||
function BaseControl:new(self, network)
|
function BaseControl:new(self, network)
|
||||||
local self = {
|
local self = {
|
||||||
locals = {
|
local_nouns = {},
|
||||||
nouns = {},
|
local_verbs = {},
|
||||||
verbs = {},
|
remote_nouns = {},
|
||||||
},
|
remote_verbs = {},
|
||||||
remotes = {
|
|
||||||
nouns = {},
|
|
||||||
verbs = {},
|
|
||||||
},
|
|
||||||
live = false,
|
live = false,
|
||||||
network = network or Network:new(),
|
network = network or Network:new(),
|
||||||
}
|
}
|
||||||
|
|
@ -107,16 +104,16 @@ function BaseControl:register(name, value)
|
||||||
end
|
end
|
||||||
|
|
||||||
if type(value) == "function" then
|
if type(value) == "function" then
|
||||||
if self.locals.verbs[name] ~= nil then
|
if self.local_verbs[name] ~= nil then
|
||||||
error("\""..name.."\" already registered")
|
error("\""..name.."\" already registered")
|
||||||
else
|
else
|
||||||
self.locals.verbs[name] = value
|
self.local_verbs[name] = value
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
if self.locals.nouns[name] ~= nil then
|
if self.local_nouns[name] ~= nil then
|
||||||
error("\""..name.."\" already registered")
|
error("\""..name.."\" already registered")
|
||||||
else
|
else
|
||||||
self.locals.nouns[name] = value
|
self.local_nouns[name] = value
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
@ -134,12 +131,12 @@ end
|
||||||
|
|
||||||
-- Nouns {{{
|
-- Nouns {{{
|
||||||
function BaseControl:has_noun(noun)
|
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
|
end
|
||||||
|
|
||||||
function BaseControl:nouns(local_only)
|
function BaseControl:nouns(local_only)
|
||||||
local nouns = {}
|
local nouns = {}
|
||||||
for noun in pairs(self.locals.nouns) do
|
for noun in pairs(self.local_nouns) do
|
||||||
table.insert(nouns, noun)
|
table.insert(nouns, noun)
|
||||||
end
|
end
|
||||||
-- TODO: Remote
|
-- TODO: Remote
|
||||||
|
|
@ -147,9 +144,9 @@ function BaseControl:nouns(local_only)
|
||||||
end
|
end
|
||||||
|
|
||||||
function BaseControl:get(noun, timeout)
|
function BaseControl:get(noun, timeout)
|
||||||
if self.locals.nouns[noun] ~= nil then
|
if self.local_nouns[noun] ~= nil then
|
||||||
return self.locals.nouns[noun]
|
return self.local_nouns[noun]
|
||||||
elseif self.locals.verbs[noun] ~= nil then
|
elseif self.local_verbs[noun] ~= nil then
|
||||||
error("\""..noun.."\" is not a noun")
|
error("\""..noun.."\" is not a noun")
|
||||||
else
|
else
|
||||||
return nil
|
return nil
|
||||||
|
|
@ -157,17 +154,17 @@ function BaseControl:get(noun, timeout)
|
||||||
end
|
end
|
||||||
|
|
||||||
function BaseControl:set(name, value)
|
function BaseControl:set(name, value)
|
||||||
if self.locals.nouns[name] ~= nil then
|
if self.local_nouns[name] ~= nil then
|
||||||
if type(value) == "function" then
|
if type(value) == "function" then
|
||||||
error("\""..name.."\" can't be cast into a verb")
|
error("\""..name.."\" can't be cast into a verb")
|
||||||
else
|
else
|
||||||
self.locals.nouns[name] = value
|
self.local_nouns[name] = value
|
||||||
end
|
end
|
||||||
elseif self.locals.verbs[name] ~= nil then
|
elseif self.local_verbs[name] ~= nil then
|
||||||
if type(value) ~= "function" then
|
if type(value) ~= "function" then
|
||||||
error("\""..name.."\" can't be cast into a noun")
|
error("\""..name.."\" can't be cast into a noun")
|
||||||
else
|
else
|
||||||
self.locals.verbs[name] = value
|
self.local_verbs[name] = value
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
error("\""..name.."\" is not a local")
|
error("\""..name.."\" is not a local")
|
||||||
|
|
@ -177,12 +174,12 @@ end
|
||||||
|
|
||||||
-- Verbs {{{
|
-- Verbs {{{
|
||||||
function BaseControl:has_verb(verb)
|
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
|
end
|
||||||
|
|
||||||
function BaseControl:verbs(local_only)
|
function BaseControl:verbs(local_only)
|
||||||
local verbs = {}
|
local verbs = {}
|
||||||
for verb in pairs(self.locals.verbs) do
|
for verb in pairs(self.local_verbs) do
|
||||||
table.insert(verbs, verb)
|
table.insert(verbs, verb)
|
||||||
end
|
end
|
||||||
-- TODO: Remote
|
-- TODO: Remote
|
||||||
|
|
@ -190,8 +187,8 @@ function BaseControl:verbs(local_only)
|
||||||
end
|
end
|
||||||
|
|
||||||
function BaseControl:call(verb, ...)
|
function BaseControl:call(verb, ...)
|
||||||
if self.locals.verbs[verb] ~= nil then
|
if self.local_verbs[verb] ~= nil then
|
||||||
self.locals.verbs[verb](...)
|
self.local_verbs[verb](...)
|
||||||
else
|
else
|
||||||
error("unknown verb")
|
error("unknown verb")
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue