|
| 1 | +---@diagnostic disable: undefined-global |
1 | 2 | --[[ |
2 | 3 | Copyright (c) 2010-2013 Matthias Richter |
3 | 4 |
|
@@ -26,80 +27,80 @@ THE SOFTWARE. |
26 | 27 | -- |
27 | 28 |
|
28 | 29 | local function include_helper(to, from, seen) |
29 | | - if from == nil then |
30 | | - return to |
31 | | - elseif type(from) ~= "table" then |
32 | | - return from |
33 | | - elseif seen[from] then |
34 | | - return seen[from] |
35 | | - end |
| 30 | + if from == nil then |
| 31 | + return to |
| 32 | + elseif type(from) ~= "table" then |
| 33 | + return from |
| 34 | + elseif seen[from] then |
| 35 | + return seen[from] |
| 36 | + end |
36 | 37 |
|
37 | | - seen[from] = to |
38 | | - for k, v in pairs(from) do |
39 | | - k = include_helper({}, k, seen) -- keys might also be tables |
40 | | - if to[k] == nil then |
41 | | - to[k] = include_helper({}, v, seen) |
42 | | - end |
43 | | - end |
44 | | - return to |
| 38 | + seen[from] = to |
| 39 | + for k, v in pairs(from) do |
| 40 | + k = include_helper({}, k, seen) -- keys might also be tables |
| 41 | + if to[k] == nil then |
| 42 | + to[k] = include_helper({}, v, seen) |
| 43 | + end |
| 44 | + end |
| 45 | + return to |
45 | 46 | end |
46 | 47 |
|
47 | 48 | -- deeply copies `other' into `class'. keys in `other' that are already |
48 | 49 | -- defined in `class' are omitted |
49 | 50 | local function include(class, other) |
50 | | - return include_helper(class, other, {}) |
| 51 | + return include_helper(class, other, {}) |
51 | 52 | end |
52 | 53 |
|
53 | 54 | -- returns a deep copy of `other' |
54 | 55 | local function clone(other) |
55 | | - return setmetatable(include({}, other), getmetatable(other)) |
| 56 | + return setmetatable(include({}, other), getmetatable(other)) |
56 | 57 | end |
57 | 58 |
|
58 | 59 | local function new(class) |
59 | | - -- mixins |
60 | | - class = class or {} -- class can be nil |
61 | | - local inc = class.__includes or {} |
62 | | - if getmetatable(inc) then |
63 | | - inc = { inc } |
64 | | - end |
| 60 | + -- mixins |
| 61 | + class = class or {} -- class can be nil |
| 62 | + local inc = class.__includes or {} |
| 63 | + if getmetatable(inc) then |
| 64 | + inc = { inc } |
| 65 | + end |
65 | 66 |
|
66 | | - for _, other in ipairs(inc) do |
67 | | - if type(other) == "string" then |
68 | | - other = _G[other] |
69 | | - end |
70 | | - include(class, other) |
71 | | - end |
| 67 | + for _, other in ipairs(inc) do |
| 68 | + if type(other) == "string" then |
| 69 | + other = _G[other] |
| 70 | + end |
| 71 | + include(class, other) |
| 72 | + end |
72 | 73 |
|
73 | | - -- class implementation |
74 | | - class.__index = class |
75 | | - class.init = class.init or class[1] or function() end |
76 | | - class.include = class.include or include |
77 | | - class.clone = class.clone or clone |
| 74 | + -- class implementation |
| 75 | + class.__index = class |
| 76 | + class.init = class.init or class[1] or function() end |
| 77 | + class.include = class.include or include |
| 78 | + class.clone = class.clone or clone |
78 | 79 |
|
79 | | - -- constructor call |
80 | | - return setmetatable(class, { |
81 | | - __call = function(c, ...) |
82 | | - local o = setmetatable({}, c) |
83 | | - o:init(...) |
84 | | - return o |
85 | | - end, |
86 | | - }) |
| 80 | + -- constructor call |
| 81 | + return setmetatable(class, { |
| 82 | + __call = function(c, ...) |
| 83 | + local o = setmetatable({}, c) |
| 84 | + o:init(...) |
| 85 | + return o |
| 86 | + end, |
| 87 | + }) |
87 | 88 | end |
88 | 89 |
|
89 | 90 | -- interface for cross class-system compatibility (see https://github.com/bartbes/Class-Commons). |
90 | 91 | if class_commons ~= false and not common then |
91 | | - common = {} |
92 | | - function common.class(name, prototype, parent) |
93 | | - return new({ __includes = { prototype, parent } }) |
94 | | - end |
95 | | - function common.instance(class, ...) |
96 | | - return class(...) |
97 | | - end |
| 92 | + common = {} |
| 93 | + function common.class(name, prototype, parent) |
| 94 | + return new({ __includes = { prototype, parent } }) |
| 95 | + end |
| 96 | + function common.instance(class, ...) |
| 97 | + return class(...) |
| 98 | + end |
98 | 99 | end |
99 | 100 |
|
100 | 101 | -- the module |
101 | 102 | return setmetatable({ new = new, include = include, clone = clone }, { |
102 | | - __call = function(_, ...) |
103 | | - return new(...) |
104 | | - end, |
| 103 | + __call = function(_, ...) |
| 104 | + return new(...) |
| 105 | + end, |
105 | 106 | }) |
0 commit comments