Quick actions

cmd+k|ctrl+k

Navigation

Languages

relationTable

Snippet info

Language

Lua

Visibility

public

Author

evine

Created

2016-04-07T10:11:12Z

Updated

2016-05-12T03:52:41Z

function relationTableInit(relationTableAttach)
    assert(type(relationTableAttach) == "table",
        "relationTableAttach must be a table.")
    function relationTableAttach.relation(...)
        local args = {...}
        assert(#args > 1, "You can't get a relation from a single object.")
        table.sort(args)
        local inTable = relationTableAttach[args[1]].relationTable
        assert(relationTableAttach[args[1]].relationTable,
            "relationTable Does not exist for: \""..args[1].."\"")
        for i = 2, #args do
            assert(relationTableAttach[args[i]].relationTable,
                "relationTable Does not exist for: \""..args[i].."\"")
            if inTable[args[i]] == nil then
                inTable[args[i]] = {}
            end
            inTable = inTable[args[i]]
        end
        return inTable
    end
end

vera = {
    b = {relationTable = {}},
    a = {relationTable = {}},
    c = {relationTable = {}},
}

relationTableInit(vera)
-- Pass in the tables instead of the strings?
vera.relation("b","a","c").aKey = "Single Value that exist on the relation of a, b and c"

print(vera.relation("c","b","a").aKey) -- Access on out of order keys.
print(vera.a.relationTable.b.c.aKey) -- Raw access. (Never use this)
INFO