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 <psychon@znc.in>
This commit is contained in:
Uli Schlachter 2016-06-04 15:47:09 +02:00
parent 0ac9a67d07
commit 6956b9f6fa
2 changed files with 8 additions and 4 deletions

View file

@ -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

View file

@ -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)