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

2 thoughts on “Automatic Lua Properties

    1. DLed Post author

      Thanks! Very glad you found it useful! After I wrote it I’ve found some others, but it’s been a good little exercise

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.