Divide the MSB source codes into two repos
[msb/apigateway.git] / openresty-ext / src / assembly / resources / openresty / nginx / luaext / vendor / classic.lua
1 --
2 -- classic, object model.
3 --
4 -- Copyright (c) 2014, rxi
5 --
6 -- This module is free software; you can redistribute it and/or modify it under
7 -- the terms of the MIT license. See LICENSE for details.
8 --
9 -- Base object model used with Kong, see [classic github repo](https://github.com/rxi/classic) for usage information
10
11 local Object = {}
12 Object.__index = Object
13
14
15 function Object:new()
16 end
17
18
19 function Object:extend()
20   local cls = {}
21   for k, v in pairs(self) do
22     if k:find("__") == 1 then
23       cls[k] = v
24     end
25   end
26   cls.__index = cls
27   cls.super = self
28   setmetatable(cls, self)
29   return cls
30 end
31
32
33 function Object:implement(...)
34   for _, cls in pairs({...}) do
35     for k, v in pairs(cls) do
36       if self[k] == nil and type(v) == "function" then
37         self[k] = v
38       end
39     end
40   end
41 end
42
43
44 function Object:is(T)
45   local mt = getmetatable(self)
46   while mt do
47     if mt == T then
48       return true
49     end
50     mt = getmetatable(mt)
51   end
52   return false
53 end
54
55
56 function Object:__tostring()
57   return "Object"
58 end
59
60
61 function Object:__call(...)
62   local obj = setmetatable({}, self)
63   obj:new(...)
64   return obj
65 end
66
67
68 return Object