Skip to content

Commit

Permalink
modns: [+] add subloader helper for abstracting access to sub-components
Browse files Browse the repository at this point in the history
  • Loading branch information
thetaepsilon committed Sep 30, 2018
1 parent aeb16d2 commit 130cd05
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
1 change: 1 addition & 0 deletions modns/construct_interface.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ return function(loader)
local modns = {
get = mtrequire,
mk_parent_ns = nsauto.ns,
create_subloader = nsauto.create_subloader,
}

return modns
Expand Down
6 changes: 6 additions & 0 deletions modns/nsauto.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
local mk_helpers = function(loader)



local create_subloader = (dofile(_modpath.."subloader.lua"))(loader)



local dname = "mk_parent_ns_noauto() "
local mk_parent_ns_noauto_inner = function(list, base, sep)
local result = {}
Expand All @@ -23,6 +28,7 @@ end

return {
ns = mk_parent_ns,
create_subloader = create_subloader,
}


Expand Down
53 changes: 53 additions & 0 deletions modns/subloader.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
--[[
A subloader represents an abstraction of accessing sub-components of a given path.
You can pass it a list of sub-path segments
(e.g. {"some", "child"} as opposed to "some.child")
or a single segment string to load a component relative to a base path;
this base path is baked into the subloader at construction time.
]]




-- concat a path passed as a table of components,
-- or accept just a string to act as a list of length one.
-- data SubPath = Single Segment | Deep [Segment]
-- concat_path :: SubPath -> PathSep -> SubPathStr
local msg_concat = "path concatenation: expected a string or table, got "
local concat_path = function(path, sep)
local t = type(path)
if t == "string" then
return path
elseif t == "table" then
return table.concat(path, sep)
else
error(msg_concat .. t)
end
end





local checkstr = function(v) assert(type(v) == "string") end

-- it's closures all the way dooooooooown
local attach_loader = function(loader)
local create_subloader = function(base, sep)
checkstr(base)
checkstr(sep)
base = base .. sep

return function(subpath)
local subs = concat_path(subpath, sep)
local fullpath = base..subs
return loader:get(fullpath)
end
end
return create_subloader
end



return attach_loader

0 comments on commit 130cd05

Please sign in to comment.