-- object.lua - (Beckett Dunning 2014) - Object oriented lua programming
---------- ---------- ---------- ---------- ----------
local Lib_Version = 1.4
-- This script adds an Object Oriented aproach to Lua Programming Calls. Traditionally in lua, there is little notion of inheritance or classes. This script allows for Javascript like progamming calls in chaned sequence as opposed to the traditional structure of raw Lua
object,Libs = {},{} -- Stores Object Libraries
local meta = {__index = self,__type = "object class", __version = Lib_Version}
setmetatable(meta,{__type = "object meta", __index = object }) setmetatable(object,meta)
-- Local variable declaration for speed improvement
local type,pairs,table,unpack,setmetatable,getmetatable =
type,pairs,table,unpack,setmetatable,getmetatable
---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
local meta_tables = { -- Stores Possible Meta-Table Key Values
__index = false, __newindex = true, __call = true, __add = true, __sub = true, __mul = true,
__div = true, __unm = true, __concat = true, __len = true, __eq = true, __lt = true, __le = true}
local meta_ext = { __type = true, __version = true, __namespace = true}
------------------------------------------------------------------
-- Primative Object Constructor
-- The new metamethods for an object subclass are passed at the time of initialization. If metatable elements are detected, they are removed from the objects methods and added to its metatable. The imput object as well as the output object retured can be used to add new methods and values to a class, but new metamethods will not be detected after initial initialization.
object.new = function(self,entries) -- Generic object constructor
local object,meta = entries and type(entries) == "table" and entries or {},
getmetatable(self):copy() meta.__index, meta. __type = self, "object"
for key,value in pairs(object) do -- Updates object metatable by parsing passed table
if meta_tables[key] or meta_ext[key] then meta[key],object[key] = value,nil end end
setmetatable(object,meta) return object end -- Returns new object
------------------------------------------------------------------
-- Object Native Data Management Methods
-- An object is created with pointers to methods which can evaluate and modify data which is contained within an object. While these functions do exist in deeper object classes, they can be overriden by methods inherited from those classes.
object.keys = function(self) -- Returs array object of keys in an object
local keys = object:new() for Key,_ in pairs(self) do keys[#keys + 1] = Key end
return keys end -- Return: "array object"
object.length = function(self) return #self:keys() end -- Returns number of elements
object.insert = function(self,pos,value) -- Adds indexies / keys to objects
if not self then error("Invalid argument no.1 'self' to object.insert().") return end
if pos and type(pos) == "number" and value then table.insert(self,pos,value) return value
elseif pos and value then self[pos] = value return value
elseif pos and not value then table.insert(self,pos) return pos
else error("Invalid argument no.2 to object.insert().") return end end
object.remove = function(self,...) -- Removes indexies / keys from object entries
if not self then error("Invalid argument no.1 'self' to object.remove().") end
local arguments = {...} local length = #arguments
if length == 0 then return table.remove(self)
else local format for i = 1,length do local arg = arguments[i] format = type(arg)
if format == "number" then arguments[i] = table.remove(self,arg)
elseif format == "string" then arguments[i] = self[arg] self[arg] = nil print("key removed")
else arguments[i] = nil end end end
return unpack(arguments) end
object.indexOf = function(self,...) -- Parses table for indexes
local search,entries = {...}, #self local num = #search
for i = 1,num do local found for index = 1,entries do
if self[index] == search[i] then search[i] = index found = true break end end
if not found then search[i] = nil end end
return unpack(search,1,num) end -- Returns list of first occurances
object.keyOf = function(self,...) -- Parses table for key values
local search,entries = {...}, #self local num = #search
for i = 1,num do local found for key,value in pairs(self) do
if value == search[i] then search[i] = key found = true break end end
if not found then search[i] = nil end end
return unpack(search,1,num) end -- Returns list of first occurances
object.contains = function(self,...) -- Determines if object contains entries
local arguments = {...} for i = 1, #arguments do local arg = arguments[i]
if not self:indexOf(arg) and not self:keyOf(arg) then return false end end
return true end -- Returns true or false
object.inverse = function(self) -- Inverses numerical indexies of array
local pos = 0 for i = #self,1,-1 do i = i + pos
self:insert(pos + 1,self:remove(i)) pos = pos + 1 end
return self end
------------------------------------------------------------------
-- Object Status / Configuration Methods
object.prototype = function(self) -- Returns prototype of object
if self == object then return self else return getmetatable(self).__index end end
object.type = function(self) -- Gets objects' __type values or type() results
if not self then error("Invalid argument no.1 to object.type().",2) end
local Meta = getmetatable(self) if Meta and Meta.__type then return Meta.__type
else return type(self) end end
object.copy = function(self) -- Creates a deep copy of object table and metatable
local meta,metaFm,copy = {},getmetatable(self),{} for k,v in pairs(metaFm) do meta[k] = v end
for key,value in pairs(self) do if type(value) ~= "table" then copy[key] = value
else copy[key] = object.copy(value) end end setmetatable(copy,meta)
return copy end -- Returns: object - copy of object
local meta_orig,meta = getmetatable(self) or {},{} for key,value in pairs(meta_orig) do
meta[key] = type(value) ~= "table" and value or object.copy(value) end
------------------------------------------------------------------
-- Extra Utility Methods
object.concat = function(self,sep) -- Returns concantenated string of array entries
return table.concat(self,sep) end -- Return: "string"