Difference between revisions of "Module:Data/loader"

From Yugipedia
Jump to: navigation, search
(Simplify. Remove dependencies.)
(Fix module path construction. Add dynamic raw module data loading.)
Line 1: Line 1:
 
-- <pre>
 
-- <pre>
 
return function( namespace )
 
return function( namespace )
local thisModulePath = 'Module:Data/' .. ( namespace or '' )
+
-- TODO: only works for 1 level. Adjust if necessary.
 +
local thisModulePath = 'Module:Data'
 +
.. ( namespace
 +
and ( '/namespaces/' .. namespace )
 +
or ''
 +
)
 +
.. '/'
  
 
local endpoints = mw.loadData( thisModulePath .. 'endpoints' )
 
local endpoints = mw.loadData( thisModulePath .. 'endpoints' )
Line 21: Line 27:
  
 
__call = function( self, moduleName )
 
__call = function( self, moduleName )
return require( 'Module:Data/static/' .. moduleName .. '/data' ).main;
+
return require( thisModulePath .. '/static/' .. moduleName .. '/data' ).main;
 
end,
 
end,
 
} )
 
} )
 
end
 
end
 
-- </pre>
 
-- </pre>

Revision as of 02:27, 29 December 2019

-- <pre>
return function( namespace )
	-- TODO: only works for 1 level. Adjust if necessary.
	local thisModulePath = 'Module:Data'
		.. ( namespace
			and ( '/namespaces/' .. namespace )
			or ''
		)
		.. '/'

	local endpoints = mw.loadData( thisModulePath .. 'endpoints' )

	return setmetatable( {}, {
		__index = function( self, key ) -- only if it doesn't contain the key
			local subModuleName = endpoints[ key ]

			if not subModuleName then
				-- Let it explode on the calling code, just like it would
				-- if all of the functions were explicitly declared.
				return nil
			else
				rawset( self, key, require( thisModulePath .. subModuleName ) )

				return rawget( self, key )
			end
		end,

		__call = function( self, moduleName )
			return require( thisModulePath .. '/static/' .. moduleName .. '/data' ).main;
		end,
	} )
end
-- </pre>