Object-orientation for Lua
Go to file
2022-01-20 23:31:22 +01:00
performance fixed perf tests 2013-09-18 23:13:41 +02:00
rockspecs new rockspec 2018-03-10 12:39:15 +01:00
spec add test case for checking type of instance's __index 2019-11-27 11:21:56 +08:00
.travis.yml fixes luacheck errors 2016-01-02 09:57:00 +01:00
CHANGELOG.md update changelog 2018-03-10 12:36:26 +01:00
middleclass.lua reduce a conditional branch in wrapped __index function 2019-10-18 18:50:22 +08:00
MIT-LICENSE.txt changed from BSD to MIT license 2011-09-19 00:24:00 +02:00
README.md restores :allocate and adds more documentation 2015-12-31 00:47:37 +01:00
UPDATING.md Update UPDATING.md 2016-03-20 18:49:56 +01:00

middleclass

Build Status Coverage Status

A simple OOP library for Lua. It has inheritance, metamethods (operators), class variables and weak mixin support.

Quick Look

local class = require 'middleclass'

local Fruit = class('Fruit') -- 'Fruit' is the class' name

function Fruit:initialize(sweetness)
  self.sweetness = sweetness
end

Fruit.static.sweetness_threshold = 5 -- class variable (also admits methods)

function Fruit:isSweet()
  return self.sweetness > Fruit.sweetness_threshold
end

local Lemon = class('Lemon', Fruit) -- subclassing

function Lemon:initialize()
  Fruit.initialize(self, 1) -- invoking the superclass' initializer
end

local lemon = Lemon:new()

print(lemon:isSweet()) -- false

Documentation

See the github wiki page for examples & documentation.

You can read the CHANGELOG.md file to see what has changed on each version of this library.

If you need help updating to a new middleclass version, read UPDATING.md.

Installation

Just copy the middleclass.lua file wherever you want it (for example on a lib/ folder). Then write this in any Lua file where you want to use it:

local class = require 'middleclass'

Specs

This project uses busted for its specs. If you want to run the specs, you will have to install it first. Then just execute the following:

cd /folder/where/the/spec/folder/is
busted

Performance tests

Middleclass also comes with a small performance test suite. Just run the following command:

lua performance/run.lua

License

Middleclass is distributed under the MIT license.