Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/shira-374/lua-object-model
Simple object model implementation in Lua
https://github.com/shira-374/lua-object-model
class inheritance lua model object oop
Last synced: 9 days ago
JSON representation
Simple object model implementation in Lua
- Host: GitHub
- URL: https://github.com/shira-374/lua-object-model
- Owner: shira-374
- License: mit
- Created: 2015-01-18T14:14:00.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2023-04-22T15:07:21.000Z (almost 2 years ago)
- Last Synced: 2024-12-20T11:29:58.622Z (about 1 month ago)
- Topics: class, inheritance, lua, model, object, oop
- Language: Lua
- Homepage:
- Size: 8.79 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.markdown
- Changelog: CHANGELOG.rst
- License: LICENSE
Awesome Lists containing this project
README
Lua object model
================Simple object model implementation in Lua.
Inspired by [http://lua-users.org/wiki/SimpleLuaClasses](http://lua-users.org/wiki/SimpleLuaClasses)
## Features
- classes
- constructors
- destructors
- inheritance
- instanceof
- parent method calls## Requirements
- Lua 5.1 or newer
## Module method overview
- `class([parentClass]): table`
- create a new class, optionally inheriting methods and properties of another one
- `instanceof(object, class): boolean`
- check if the given object is an instance of the given class or has that class as
one of its parents
- `new(class, ...): object`
- create an instance of the given class and invoke its constructor
- any additional arguments will be passed to the constructor
- should not be used directly, invoke the class table instead (refer to the *Instantiating a class* section)
- `super(object, methodName, ...): mixed`
- invokes a parent method
- this is the default implementation of `object:super(method, ...)`## Feature documentation
Let's assume you have imported the `ObjectModel` module like this:
local o = require 'ObjectModel'
### Creating a new class
The `class` function returns a new table that you can populate with your methods
and properties.MyClass = o.class()
### Inheritance
If another class is passed to the `class()` function, all of its methods
and properties are inherited.MyClass = o.class(OtherClass)
Internally, a shallow copy of the parent class is created for performance reasons.
Modifications of the parent class at a later point will NOT be propagated to the child
classes.### Prototype properties
Prototype properties can be defined in the class table. They are available in all
instances of the class and can be accessed statically. Instance properties will shadow
prototype properties with the same name.MyClass.foo = 'bar'
**Warning!** Do not put tables into prototype properties unless you want the table
to be shared across all the instances (see instance properties).### Methods
Lua provides syntactic sugar for this purpose.
function MyClass:doSomething()
-- do something, self points to the instance
print(self.someProperty) -- getting properties
print(self:someMethod()) -- calling other methods
endSame as prototype properties, methods defined this way are available in all instances
of the class and can be called statically.### Constructors
Constructor is a special method that is invoked when a class is being instantiated.
function MyClass:constructor(lorem, ipsum)
-- self points to the instance
end### Destructors
Destructor is a special method that is invoked when an instance is being garbage-collected
by Lua.function MyClass:destructor()
-- self points to the instance
-- no arguments are passed
end### Instantiating a class
To create an instance of a class, simply call the class table as a function.
local obj = MyClass('something')
All arguments passed to this function are forwarded to the constructor.
### Instance properties
Properties defined on an object are privately owned by that object. They will shadow prototype
properties with the same name.MyClass.test = 'hello from prototype'
local obj = MyClass()
print(obj.test) -- prints: hello from prototype
obj.test = 'hello from instance'
print(obj.test) -- prints: hello from instance
obj.test = nil
print(obj.test) -- prints: hello from prototype### Instanceof
To check whether an object is instance of a specific class or has that class as one of its parents,
use the `instanceof()` function.local obj = MyClass()
if o.instanceof(obj, MyClass) then
print('obj is instance of MyClass')
end### Calling parent methods
If a class has inherited from another and overridden some of its methods, these methods can be
invoked using the `:super(methodName, ...)` method.BaseClass = o.class()
function BaseClass:constructor(foo)
self.foo = foo
endDerivedClass = o.class(BaseClass)
function DerivedClass:constructor(foo, bar)
self:super('constructor', foo) -- invoke the parent constructor
self.bar = bar
endlocal obj = DerivedClass('hello', 'world')
print(obj.foo, obj.bar) -- prints: hello world
### Metamethods
Metamethods can be defined the same way as any other methods and will work as expected.
function MyClass:__tostring()
return 'stringified!'
endlocal obj = MyClass()
print(obj) -- prints: stringified!