-
-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
1 changed file
with
22 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,39 @@ | ||
local collect = require('github-theme.lib.collect') | ||
local M = {} | ||
|
||
local store = { | ||
palettes = {}, | ||
specs = {}, | ||
groups = {}, | ||
has_override = false, | ||
} | ||
|
||
local function reset() | ||
store.palettes = {} | ||
store.specs = {} | ||
store.groups = {} | ||
store.has_override = false | ||
function M.reset() | ||
getmetatable(M).__index = | ||
{ palettes = {}, specs = {}, groups = {}, has_override = false } | ||
return M | ||
end | ||
|
||
local function hash() | ||
local hash = require('github-theme.lib.hash')(store) | ||
return hash and hash or 0 | ||
function M.hash() | ||
return require('github-theme.lib.hash')(getmetatable(M).__index) or 0 | ||
end | ||
|
||
local function check_link(tbl) | ||
for _, style in pairs(tbl) do | ||
for _, opts in pairs(style) do | ||
for _, theme in pairs(tbl) do | ||
for _, opts in pairs(theme) do | ||
opts.link = opts.link or '' | ||
end | ||
end | ||
end | ||
|
||
return setmetatable({ reset = reset, hash = hash }, { | ||
__index = function(_, value) | ||
if store[value] then | ||
return store[value] | ||
end | ||
end, | ||
|
||
__newindex = function(_, key, value) | ||
if store[key] then | ||
if key == 'groups' then | ||
check_link(value or {}) | ||
setmetatable(M, { | ||
__newindex = function(self, k, v) | ||
local store = getmetatable(self).__index | ||
if type(store[k]) == 'table' then | ||
if not v then | ||
store[k] = {} | ||
return | ||
end | ||
if k == 'groups' then | ||
check_link(v) | ||
end | ||
store[key] = collect.deep_extend(store[key], value or {}) | ||
store[k] = collect.deep_extend(store[k], v) | ||
store.has_override = true | ||
end | ||
end, | ||
}) | ||
|
||
return M.reset() |