summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid McMackins II <contact@mcmackins.org>2015-07-05 21:54:12 -0500
committerDavid McMackins II <contact@mcmackins.org>2015-07-05 21:54:12 -0500
commit62e714e6eff527491111a1f5a1f95d530f595dfe (patch)
treea0c0de8d296af54635473d9a077c5314ed570915
Initial commit
-rw-r--r--README4
l---------README.md1
-rw-r--r--class.lua56
3 files changed, 61 insertions, 0 deletions
diff --git a/README b/README
new file mode 100644
index 0000000..18304b3
--- /dev/null
+++ b/README
@@ -0,0 +1,4 @@
+lua-classes
+===========
+
+Simple Lua 5.1 class implementation \ No newline at end of file
diff --git a/README.md b/README.md
new file mode 120000
index 0000000..100b938
--- /dev/null
+++ b/README.md
@@ -0,0 +1 @@
+README \ No newline at end of file
diff --git a/class.lua b/class.lua
new file mode 100644
index 0000000..67259b6
--- /dev/null
+++ b/class.lua
@@ -0,0 +1,56 @@
+--[[
+
+Copyright (C) 2015 Delwink, LLC
+
+Permission to use, copy, modify, and/or distribute this software for any
+purpose with or without fee is hereby granted, provided that the above
+copyright notice and this permission notice appear in all copies.
+
+THE SOFTWARE IS PROVIDED “AS IS” AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
+TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
+FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
+CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
+DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
+ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
+SOFTWARE.
+
+--]]
+
+function class(base, init)
+ local c = {}
+ if not init and type(base) == 'function' then
+ init = base
+ base = nil
+ elseif type(base) == 'table' then
+ for i,v in pairs(base) do
+ c[i] = v
+ end
+ c._base = base
+ end
+ c.__index = c
+
+ local mt = {}
+ mt.__call = function(class_tbl, ...)
+ local obj = {}
+ setmetatable(obj,c)
+ if init then
+ init(obj,...)
+ else
+ if base and base.init then
+ base.init(obj, ...)
+ end
+ end
+ return obj
+ end
+ c.init = init
+ c.is_a = function(self, klass)
+ local m = getmetatable(self)
+ while m do
+ if m == klass then return true end
+ m = m._base
+ end
+ return false
+ end
+ setmetatable(c, mt)
+ return c
+end