Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/joshtynjala/getset.lua
A simple getter and setter library for Lua.
https://github.com/joshtynjala/getset.lua
corona-sdk lua
Last synced: 4 months ago
JSON representation
A simple getter and setter library for Lua.
- Host: GitHub
- URL: https://github.com/joshtynjala/getset.lua
- Owner: joshtynjala
- Archived: true
- Created: 2011-09-29T01:39:12.000Z (over 13 years ago)
- Default Branch: master
- Last Pushed: 2011-10-04T06:23:46.000Z (over 13 years ago)
- Last Synced: 2024-05-01T19:33:46.760Z (9 months ago)
- Topics: corona-sdk, lua
- Language: Lua
- Homepage: https://github.com/joshtynjala/getset.lua
- Size: 93.8 KB
- Stars: 11
- Watchers: 5
- Forks: 8
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# getset.lua
A library for defining getters and setters on Lua tables by [Josh Tynjala](http://twitter.com/joshtynjala).
## Usage
local getset = require("getset")
function createSimple2DPoint()
local _x = 0
local _y = 0
local point = {}
getset.defineProperty( point, "x",
{
get = function()
return _x
end,
set = function( value )
_x = value
end
})
getset.defineProperty( point, "y",
{
get = function()
return _y
end,
set = function( value )
_y = value
end
})
getset.defineProperty(point, "length",
{
get = function()
return math.sqrt(_x * _x + _y * _y)
end
-- no setter required
})
-- prevents new fields from being added later, and existing fields cannot
-- be configured (we can't redefine the getters and setters later).
-- see also: getset.preventExtensions()
return getset.seal(point)
end
local myPoint = createSimple2DPoint()
myPoint.x = 4
myPoint.y = 4
-- returns 5.6568542494924:
local myPointLength = myPoint.length
-- runtime error because the table is not extensible:
myPoint.z = 7
-- runtime error because length has no setter:
myPoint.length = 9
-- runtime error because table is sealed:
getset.defineProperty( myPoint, "x",
{
get = function()
return 1
end
})
## APIThe getset library offers the following functions. For more detailed descriptions, see the documentation above each function in getset.lua.
### getset.defineProperty( table, key, descriptor )
Defines a getter and a setter on a table for a specific key. Both are optional.
### getset.preventExtensions( table )
Prevents new properties from being added to a table. Existing properties may be modified and configured.
### getset.isExtensible( table )
Determines if a table is may be extended.
### getset.seal( table )
Prevents new properties from being added to a table, and existing properties may be modified, but not configured.
### getset.isSealed( table )
Determines if a table is sealed.