Difference between revisions of "Module:Data/loader"

From Yugipedia
Jump to: navigation, search
(Trying something here. Data namespace loader.)
 
(Simplify. Remove dependencies.)
Line 1: Line 1:
 
-- <pre>
 
-- <pre>
local StringBuffer = require( 'Module:StringBuffer' )
+
return function( namespace )
 +
local thisModulePath = 'Module:Data/' .. ( namespace or '' )
  
return function( namespace )
+
local endpoints = mw.loadData( thisModulePath .. 'endpoints' )
local methods = mw.loadData( StringBuffer()
 
:add( 'Module:Data' )
 
:add( namespace )
 
:add( 'methods' )
 
:flush( '/' )
 
:toString()
 
)
 
  
 
return setmetatable( {}, {
 
return setmetatable( {}, {
 
__index = function( self, key ) -- only if it doesn't contain the key
 
__index = function( self, key ) -- only if it doesn't contain the key
local subModule = methods[ key ]
+
local subModuleName = endpoints[ key ]
  
if not subModule then
+
if not subModuleName then
 
-- Let it explode on the calling code, just like it would
 
-- Let it explode on the calling code, just like it would
 
-- if all of the functions were explicitly declared.
 
-- if all of the functions were explicitly declared.
 
return nil
 
return nil
 
else
 
else
rawset( self, key, require( 'Module:Data/' .. subModule ) )
+
rawset( self, key, require( thisModulePath .. subModuleName ) )
  
 
return rawget( self, key )
 
return rawget( self, key )

Revision as of 01:41, 29 December 2019

-- <pre>
return function( namespace )
	local thisModulePath = 'Module:Data/' .. ( 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( 'Module:Data/static/' .. moduleName .. '/data' ).main;
		end,
	} )
end
-- </pre>