Skip to content

Commit c23f71b

Browse files
authored
refactor(overrides): cleanup override.lua (#348)
- Move the store into the module's metatable at `__index`. - Setting a key to a falsy value resets the overrides under that key. - Don't allow `has_override` to be modified externally. - D.R.Y.
1 parent e682243 commit c23f71b

File tree

1 file changed

+22
-29
lines changed

1 file changed

+22
-29
lines changed

lua/github-theme/override.lua

+22-29
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,39 @@
11
local collect = require('github-theme.lib.collect')
2+
local M = {}
23

3-
local store = {
4-
palettes = {},
5-
specs = {},
6-
groups = {},
7-
has_override = false,
8-
}
9-
10-
local function reset()
11-
store.palettes = {}
12-
store.specs = {}
13-
store.groups = {}
14-
store.has_override = false
4+
function M.reset()
5+
getmetatable(M).__index =
6+
{ palettes = {}, specs = {}, groups = {}, has_override = false }
7+
return M
158
end
169

17-
local function hash()
18-
local hash = require('github-theme.lib.hash')(store)
19-
return hash and hash or 0
10+
function M.hash()
11+
return require('github-theme.lib.hash')(getmetatable(M).__index) or 0
2012
end
2113

2214
local function check_link(tbl)
23-
for _, style in pairs(tbl) do
24-
for _, opts in pairs(style) do
15+
for _, theme in pairs(tbl) do
16+
for _, opts in pairs(theme) do
2517
opts.link = opts.link or ''
2618
end
2719
end
2820
end
2921

30-
return setmetatable({ reset = reset, hash = hash }, {
31-
__index = function(_, value)
32-
if store[value] then
33-
return store[value]
34-
end
35-
end,
36-
37-
__newindex = function(_, key, value)
38-
if store[key] then
39-
if key == 'groups' then
40-
check_link(value or {})
22+
setmetatable(M, {
23+
__newindex = function(self, k, v)
24+
local store = getmetatable(self).__index
25+
if type(store[k]) == 'table' then
26+
if not v then
27+
store[k] = {}
28+
return
29+
end
30+
if k == 'groups' then
31+
check_link(v)
4132
end
42-
store[key] = collect.deep_extend(store[key], value or {})
33+
store[k] = collect.deep_extend(store[k], v)
4334
store.has_override = true
4435
end
4536
end,
4637
})
38+
39+
return M.reset()

0 commit comments

Comments
 (0)