Skip to content

Commit

Permalink
feat: rxi/classic object abstraction (#134)
Browse files Browse the repository at this point in the history
  • Loading branch information
oberblastmeister authored Jul 12, 2021
1 parent ca51b68 commit 8bae2c1
Showing 1 changed file with 81 additions and 0 deletions.
81 changes: 81 additions & 0 deletions lua/plenary/class.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
---@brief [[
---classic
---
---Copyright (c) 2014, rxi
---@brief ]]

---@class Object
local Object = {}
Object.__index = Object

---Does nothing.
---You have to implement this yourself for extra functionality when initializing
---@param self Object
function Object:new()
end

---Create a new class/object by extending the base Object class.
---The extended object will have a field called `super` that will access the super class.
---@param self Object
---@return Object
function Object:extend()
local cls = {}
for k, v in pairs(self) do
if k:find("__") == 1 then
cls[k] = v
end
end
cls.__index = cls
cls.super = self
setmetatable(cls, self)
return cls
end

---Implement a mixin onto this Object.
---@param self Object
---@param nil ...
function Object:implement(...)
for _, cls in pairs({...}) do
for k, v in pairs(cls) do
if self[k] == nil and type(v) == "function" then
self[k] = v
end
end
end
end

---Checks if the object is an instance
---This will start with the lowest class and loop over all the superclasses.
---@param self Object
---@param T Object
---@return boolean
function Object:is(T)
local mt = getmetatable(self)
while mt do
if mt == T then
return true
end
mt = getmetatable(mt)
end
return false
end

---The default tostring implementation for an object.
---You can override this to provide a different tostring.
---@param self Object
---@return string
function Object:__tostring()
return "Object"
end

---You can call the class the initialize it without using `Object:new`.
---@param self Object
---@param nil ...
---@return Object
function Object:__call(...)
local obj = setmetatable({}, self)
obj:new(...)
return obj
end

return Object

0 comments on commit 8bae2c1

Please sign in to comment.