From 6956b9f6fa14b9f6182ec42cab27254fc58bc48d Mon Sep 17 00:00:00 2001 From: Uli Schlachter Date: Sat, 4 Jun 2016 15:47:09 +0200 Subject: [PATCH] gears.object: Implement read-only properties If a getter exists, but there is no setter, then this means that the property is read-only. Signed-off-by: Uli Schlachter --- lib/gears/object.lua | 6 +++++- spec/gears/object_spec.lua | 6 +++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/gears/object.lua b/lib/gears/object.lua index 25e4ae63..18d0df4b 100644 --- a/lib/gears/object.lua +++ b/lib/gears/object.lua @@ -148,8 +148,12 @@ local function set_miss(self, key, value) if changed then self:emit_signal("property::"..key, value) end - else + elseif (not rawget(self, "get_"..key)) + and not (class and class["get_"..key]) then return rawset(self, key, value) + else + error("Cannot set '" .. tostring(key) .. "' on " .. tostring(self) + .. " because it is read-only") end end diff --git a/spec/gears/object_spec.lua b/spec/gears/object_spec.lua index 812cc0bc..7c6eb57f 100644 --- a/spec/gears/object_spec.lua +++ b/spec/gears/object_spec.lua @@ -140,7 +140,6 @@ describe("gears.object", function() it("dynamic property disabled", function() local class = {} function class:get_foo() return "bar" end - function class:set_foo() end local obj2 = object{class=class} @@ -152,11 +151,12 @@ describe("gears.object", function() it("dynamic property disabled", function() local class = {} function class:get_foo() return "bar" end - function class:set_foo() end local obj2 = object{class=class, enable_properties = true} - obj2.foo = 42 + assert.has_error(function() + obj2.foo = 42 + end) assert.is.equal(obj2.foo, "bar") end)