transformed inspect into a callable table with _VERSION attribute

This commit is contained in:
kikito 2012-11-03 15:17:14 +01:00
parent 9df9eaf1c4
commit 14a63ac985

View File

@ -5,6 +5,9 @@
-- inspired by http://lua-users.org/wiki/TableSerialization -- inspired by http://lua-users.org/wiki/TableSerialization
----------------------------------------------------------------------------------------------------------------------- -----------------------------------------------------------------------------------------------------------------------
local inspect ={}
inspect.__VERSION = '1.2.0'
-- Apostrophizes the string if it has quotes, but not aphostrophes -- Apostrophizes the string if it has quotes, but not aphostrophes
-- Otherwise, it returns a regular quoted string -- Otherwise, it returns a regular quoted string
local function smartQuote(str) local function smartQuote(str)
@ -92,10 +95,7 @@ function Inspector:new(t, depth)
tableAppearances = setmetatable({}, {__mode = "k"}) tableAppearances = setmetatable({}, {__mode = "k"})
} }
setmetatable( inspector, { setmetatable(inspector, {__index = Inspector})
__index = Inspector,
__tostring = function(instance) return table.concat(instance.buffer) end
} )
inspector:countTableAppearances(t) inspector:countTableAppearances(t)
@ -231,9 +231,13 @@ function Inspector:putKey(k)
return self:puts( "[" ):putValue(k):puts("]") return self:puts( "[" ):putValue(k):puts("]")
end end
local function inspect(t, depth) function Inspector:tostring()
return tostring(Inspector:new(t, depth)) return table.concat(self.buffer)
end end
setmetatable(inspect, { __call = function(_,t,depth)
return Inspector:new(t, depth):tostring()
end })
return inspect return inspect