Commits

Bart van Strien committed 29e53bf

Add class attributes and class attributes only, everything else can be simulated, these are well-defined and non-inherited

Comments (0)

Files changed (3)

 
 If you just want to use the library, slither.lua is the file you'll want, examples are present in examples.lua.
 
+Documentation can be found at [my documentation wiki](http://docs.bartbes.com/slither)
+
 Enjoy!
 
 (P.S. Do feel free to report any bugs you might encounter)
 
 local class =
 {
-	_VERSION = "Slither 20140425",
+	_VERSION = "Slither 20140713",
 	-- I have no better versioning scheme, deal with it
 	_DESCRIPTION = "Slither is a pythonic class library for lua",
 	_URL = "http://bitbucket.org/bartbes/slither",
 local function stringtotable(path)
 	local t = _G
 	local name
+
 	for part in path:gmatch("[^%.]+") do
 		t = name and t[name] or t
 		name = part
 	end
+
 	return t, name
 end
 
 	for i, v in pairs(parents) do
 		table.insert(temp.__parents__, i)
 	end
-	return setmetatable(temp, {
+
+	local class = setmetatable(temp, {
 		__index = function(self, key)
 			if key == "__class__" then return temp end
 			if key == "__name__" then return name end
 			return instance
 		end
 		})
+
+	for i, v in ipairs(t.__attributes__ or {}) do
+		class = v(class) or class
+	end
+
+	return class
 end
 
 local function inheritance_handler(set, name, ...)
 
 	assert(A.test == false)
 end)
+
+Test("Attributes get called", function()
+	local called = false
+
+	local function attr(class)
+		called = true
+	end
+
+	class "A"
+	{
+		__attributes__ = {attr}
+	}
+
+	assert(called)
+end)
+
+Test("Attribute can modify class", function()
+	local function attr(class)
+		class.cake = true
+	end
+
+	class "A"
+	{
+		__attributes__ = {attr}
+	}
+
+	assert(A.cake)
+end)
+
+Test("Attribute can modify constructor", function()
+	local called = false
+
+	local function attr(class)
+		local oldinit = class.__init__ or function() end
+		class.__init__ = function(...)
+			called = true
+		end
+	end
+
+	class "A"
+	{
+		__attributes__ = {attr}
+	}
+
+	local a = A()
+	assert(called)
+end)
+
+Test("Attributes are not inherited", function()
+	local called = false
+	local function attr()
+		called = true
+	end
+
+	class "A"
+	{
+		__attributes__ = {attr}
+	}
+
+	called = false
+
+	class "B" (A)
+	{
+	}
+
+	assert(not called)
+end)
+
+Test("Attribute can replace return value", function()
+	local function attr()
+		return "a"
+	end
+
+	assert((class "A" { __attributes__ = {attr} }) == "a")
+end)
+
+Test("Attributes are called in order", function()
+	local function attr1(class)
+		class.stage = 1
+	end
+
+	local function attr2(class)
+		if class.stage == 1 then
+			class.stage = 2
+		end
+	end
+
+	class "A"
+	{
+		__attributes__ = {attr1, attr2}
+	}
+
+	assert(A.stage == 2)
+end)
+
+Test("Attributes can be anything callable", function()
+	class "Attribute"
+	{
+		called = false,
+
+		__call__ = function(self)
+			self.called = true
+		end
+	}
+
+	local attr = Attribute()
+
+	class "A"
+	{
+		__attributes__ = {attr}
+	}
+
+	assert(attr.called)
+end)