Tag Archives: table

Automatic Lua Properties

Automatic Lua Properties?

Starting with an example using the Lua specification and testing framework Busted:

describe("automatic lua properties",function()
    it ("should be clear from an example",function()
        local autoprop = require 'autoprop'

        -- an empty table upon creation --
        local config = autoprop()
        assert.True( #config == 0 )

        -- properties are added dynamically on demand --
        config.some.url = 'http://olivinelabs.com/busted'
        config.some.number = 42
        config.some.other.url = 'https://github.com/nrother/dynamiclua'

        -- this should hold --
        assert.are_equal(42, config.some.number)
        assert.True( #config.some.url > 0 )
        assert.True( #config.some.other.url > 0 )
    end)
end)

Here is a little exercise in Lua metaprogramming.

Code

https://github.com/d-led/automatic-lua-property-tables

Spec: autoprop_spec.lua
Implementation: autoprop.lua

P.S. Other implementations: lua-users wiki: Automagic Tables