Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
30 lines library for object orientation in Lua
Lua

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
cmake
rockspecs
specs
.travis.yml
30log.lua
30logclasscommons.lua
CMakeLists.txt
MIT-LICENSE.txt
README.md
dist.info
version_history.md

README.md

30log

Build Status

30log, in extenso 30 Lines Of Goodness is a minified framework for object-orientation in Lua. It features class creation, instantiation, single inheritance .
And, it makes 30 lines. No less, no more.

Download

Bash

git clone git://github.com/Yonaba/30log.git

Archive

LuaRocks

luarocks install 30log

MoonRocks

luarocks install --server=http://rocks.moonscript.org/manifests/Yonaba 30log

Installation

Copy the file 30log.lua inside your project folder, call it using require function.
When loaded, 30log returns its main function.

Quicktour

Creating a class

Making a new class is fairly simple. Just call the returned function, then add some properties to this class :

class = require '30log'
Window = class ()
Window.x, Window.y = 10, 10
Window.width, Window.height = 100,100

You can also shortcut it, passing the default properties as a table to class :

class = require '30log'
Window = class { width = 100, height = 100, x = 10, y = 10}

Instances

Once a class is set, you can easily create new instances from the class.

appFrame = Window:new()
print(appFrame.x,appFrame.y) --> 10, 10
print(appFrame.width,appFrame.height) --> 100, 100

You can also use a shortcut, calling the class as a function :

appFrame = Window()
print(appFrame.x,appFrame.y) --> 10, 10
print(appFrame.width,appFrame.height) --> 100, 100

From the two examples above, you might have noticed that once an instance is created from a class, its properties takes by default the class properties. But, you can init objects from a class with your own specific properties. To accomplish that, you must have implemented a method named __init inside the base class.
In a nutshell, __init is the default method to be used as a class constructor.

Window = class { width = 100, height = 100, x = 10, y = 10}
function Window:__init(x,y,width,height)
  self.x,self.y = x,y
  self.width,self.height = width,height
end

appFrame = Window:new(50,60,800,600)
   -- or appFrame = Window(50,60,800,600)
print(appFrame.x,appFrame.y) --> 50, 60
print(appFrame.width,appFrame.height) --> 800, 600

Methods

Methods are supported. Obviously.

Window = class { width = 100, height = 100, w = 10, y = 10}
function Window:__init(x,y,width,height)
  self.x,self.y = x,y
  self.width,self.height = width,height
end

function Window:set(x,y)
  self.x, self.y = x, y
end

function Window:resize(width, height)
  self.width, self.height = width, height
end

appFrame = Window()
appFrame:set(50,60)
print(appFrame.x,appFrame.y) --> 50, 60
appFrame:resize(800,600)
print(appFrame.width,appFrame.height) --> 800, 600

Inheritance

A class can derive from a base class using a default method named :extends. The new class will inherit his mother class default members and methods.

Window = class { width = 100, height = 100, x = 10, y = 10}
Frame = Window:extends { color = 'black' }
print(Frame.x, Frame.y) --> 10, 10

appFrame = Frame()
print(appFrame.x,appFrame.y) --> 10, 10

A derived class can overload any method defined in its base class (or mother class). Therefore, the derived class still has access to his mother class methods via a special key named super.
Let's use this feature to build a class constructor for our Frame class.

-- The base class "Window"
Window = class { width = 100, height = 100, x = 10, y = 10}
function Window:__init(x,y,width,height)
  self.x,self.y = x,y
  self.width,self.height = width,height
end

function Window:set(x,y)
  self.x, self.y = x, y
end

-- A derived class named "Frame"
Frame = Window:extends { color = 'black' }
function Frame:__init(x,y,width,height,color)
  -- Calling the superclass constructor
  self.super.__init(self,x,y,width,height)
  -- Setting the extra class member
  self.color = color
end

-- Overloading Window:set()
function Frame:set(x,y)
  self.x = x - self.width/2
  self.y = y - self.height/2
end

-- A appFrame from "Frame" class
appFrame = Frame(100,100,800,600,'red')
print(appFrame.x,appFrame.y) --> 100, 100

appFrame:set(400,400)
print(appFrame.x,appFrame.y) --> 0, 100

appFrame.super.set(appFrame,400,300)
print(appFrame.x,appFrame.y) --> 400, 300

Chained initialisation

In a single inheritance model, __init constructor can be chained from one class to another.

-- A mother class 'A'
local A = Class()
function A.__init(instance,a)
  instance.a = a
end

-- Class 'B', deriving from class 'A'
local B = A:extends()
function B.__init(instance, a, b)
  B.super.__init(instance, a)
  instance.b = b
end

-- Class 'C', deriving from class 'B'
local C = B:extends()
function C.__init(instance, a, b, c)
  C.super.__init(instance,a, b)
  instance.c = c
end

-- Class 'D', deriving from class 'C'
local D = C:extends()
function D.__init(instance, a, b, c, d)
  D.super.__init(instance,a, b, c)
  instance.d = d
end

-- Creating an instance of class 'D'
local objD = D(1,2,3,4)
for k,v in pairs(objD) do print(k,v) end

-- Output:
--> a   1
--> d   4
--> c   3
--> b   2

Class Commons support

Class-Commons is an interface that provides a common API for lua classes libraries.

require("30logclasscommons")

common.class(...)
common.instance(...)

Specification

30log Specs

Specs tests have been included.
Run them using Telescope with the following command from the root foolder:

tsc -f specs/*
------------------------------------------------------------------------
Class():                                                             
When Class is called with no args passed:                            
  it returns a new class (regular Lua table)                         [P]
Attributes:                                                          
  can be added to classes                                            [P]
  can be passed in a table to Class()                                [P]
Methods:                                                             
  can be added to classes                                            [P]
------------------------------------------------------------------------
Derivation (Inheritance):                                            
Class can be derived from a superclass:                              
  Via "extends()" method                                             [P]
  With extra-arguments passed to method "extends()" as a table       [P]
A derived class still points to its superclass:                      
  Via its "super" key                                                [P]
  Via "getmetatable()" function                                      [P]
A derived class:                                                     
  can instantiate objects                                            [P]
  shares its superclass attributes                                   [P]
  shares its superclass methods                                      [P]
  can reimplement its superclass methods                             [P]
  Yet, it still has access to the original superclass method         [P]
In a single inheritance model:                                       
  __init() class constructor can chain                               [P]
------------------------------------------------------------------------
Instances (Objects):                                                 
When a class is created:                                             
  new objects can be created via Class:new()                         [P]
  new objects can be created calling the class as a function         [P]
  new objects share their class attributes                           [P]
  new objects share their class methods                              [P]
Providing an :__init() method to classes:                            
  Overrides instantiation scheme with Class:new()                    [P]
  Overrides instantiation scheme with Class()                        [P]
------------------------------------------------------------------------
20 tests 20 passed 35 assertions 0 failed 0 errors 0 unassertive 0 pending

Class-Commons testing implementation

See Class-Commons-Tests

$ lua tests.lua 30logclasscommons
Testing implementation: 30logclasscommons
  Summary:
    Failed: 0
    Out of: 10
    Rate: 100%

Contributors

  • TsT2005, for Class-commons support.

License

This work is under MIT-LICENSE
Copyright (c) 2012 Roland Yonaba

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Something went wrong with that request. Please try again.