From 56a57f2e8997812a6d53aa766ed3db67ca55998c Mon Sep 17 00:00:00 2001 From: Adrian Perez de Castro Date: Thu, 23 Jun 2016 04:07:01 +0300 Subject: [PATCH] Add __tostring metamethods to prototypes This is nice to have, and aids with debugging and interactive usage. --- matrix/api.lua | 5 +++++ matrix/client.lua | 15 +++++++++++++++ matrix/factory/chttp.lua | 5 +++++ 3 files changed, 25 insertions(+) diff --git a/matrix/api.lua b/matrix/api.lua index e275a25..63021cd 100644 --- a/matrix/api.lua +++ b/matrix/api.lua @@ -27,6 +27,7 @@ end local API = {} +API.__name = "matrix.api" API.__index = API setmetatable(API, { __call = function (self, base_url, token, http_factory) @@ -40,6 +41,10 @@ setmetatable(API, { __call = function (self, base_url, token, http_factory) }, API) end }) +function API:__tostring() + return self.__name .. "{" .. self.base_url .. "}" +end + function API:_quote(string) return self._http:quote(string) end diff --git a/matrix/client.lua b/matrix/client.lua index 2ab0a6c..c707eee 100644 --- a/matrix/client.lua +++ b/matrix/client.lua @@ -7,12 +7,17 @@ -- local User = {} +User.__name = "matrix.user" User.__index = User setmetatable(User, { __call = function (self, client, user_id) return setmetatable({ user_id = user_id, _client = client }, User) end }) +function User:__tostring() + return self.__name .. "{" .. self.user_id .. "}" +end + function User:get_display_name() return self._client._api:get_display_name(self.user_id) end @@ -32,6 +37,7 @@ end local Room = {} +Room.__name = "matrix.room" Room.__index = Room setmetatable(Room, { __call = function (self, client, room_id) @@ -43,6 +49,10 @@ setmetatable(Room, { __call = function (self, client, room_id) }, Room) end }) +function Room:__tostring() + return self.__name .. "{" .. self.room_id .. "}" +end + function Room:send_text(text) return self._client._api:send_message(self.room_id, text) end @@ -81,6 +91,7 @@ end local Client = {} +Client.__name = "matrix.client" Client.__index = Client setmetatable(Client, { __call = function (self, base_url, token, http_factory) @@ -95,6 +106,10 @@ setmetatable(Client, { __call = function (self, base_url, token, http_factory) return c end }) +function Client:__tostring() + return self.__name .. "{" .. self._api.base_url .. "}" +end + function Client:register_with_password(username, password, limit) return self:_logged_in(self._api:register("m.login.password", { user = username, password = password }), limit) diff --git a/matrix/factory/chttp.lua b/matrix/factory/chttp.lua index fd1c462..92c36fc 100644 --- a/matrix/factory/chttp.lua +++ b/matrix/factory/chttp.lua @@ -18,8 +18,13 @@ local CqHttpClient = { quote = function (self, text) return encodeURI(text) end, unquote = function (self, text) return decodeURI(text) end, } +CqHttpClient.__name = "matrix.factory.chttp" CqHttpClient.__index = CqHttpClient +function CqHttpClient:__tostring() + return self.__name +end + local function headers_to_dict(h) local headers = {}