-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsection.lua
More file actions
67 lines (58 loc) · 1.61 KB
/
section.lua
File metadata and controls
67 lines (58 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
local function with_helper(till, its_from, seen_in)
if its_from == nil then
return till
elseif type(its_from) ~= 'table' then
return its_from
elseif seen_in[its_from] then
return seen_in[its_from]
end
seen_in[its_from] = till
for k,v in pairs(its_from) do
--add tables
k = with_helper({}, k, seen_in)
if till[k] == nil then
till[k] = with_helper({}, v, seen_in)
end
end
return till
end
local function with(section, other_one)
return with_helper(section, other_one, {})
end
local function cloner(other_one)
return setmetatable(with({}, other_one), getmetatable(other_one))
end
local function brand_new(section)
-- checks fot new class
section = section or {}
local inc = section.__includes or {}
if getmetatable(inc) then inc = {inc} end
for _, other_one in ipairs(inc) do
if type(other_one) == "string" then
other_one = _G[other_one]
end
with(section, other_one)
end
-- implements class
section.__index = section
section.init = section.init or section[1] or function() end
section.with = section.with or with
section.cloner = section.cloner or cloner
-- constructs brick
return setmetatable(section, {__call = function(c, ...)
local o = setmetatable({}, c)
o:init(...)
return o
end})
end
if section_commons ~= false and not common then
common = {}
function common.section(naming, c_prototype, c_parent)
return brand_new{__includes = {c_prototype, c_parent}}
end
function common.instance(section, ...)
return section(...)
end
end
return setmetatable({brand_new = brand_new, with = with, cloner = cloner},
{__call = function(_,...) return brand_new(...) end})