Difference between revisions of "Module:InfoWrapper"

From Yugipedia
Jump to: navigation, search
(Helper module to create info objects.)
 
(Remove unnecessary methods. Add some useful ones. Cleanup.)
Line 8: Line 8:
 
InfoClass.__index = InfoClass;
 
InfoClass.__index = InfoClass;
  
-- @description: Initialize the instance of the InfoClass.
+
-- @description Initialize the instance of the InfoClass.
 
function InfoClass.new( title )
 
function InfoClass.new( title )
 
local data = {
 
local data = {
title     = title or '',
+
_title     = title or '',
info      = {},
+
_categories = {},
categories = {},
+
_errors     = {},
errors     = {},
 
 
};
 
};
  
Line 20: Line 19:
 
end
 
end
  
-- @description Add <code>key</code> info with <code>value</value>.
+
-- @description Returns its gathered categories.
function InfoClass:add( key, value )
+
function InfoClass:getCategories()
self.info[ key ] = value;
+
return self._categories;
return self;
 
end
 
 
 
-- @description Get <code>key</code> info.
 
function InfoClass:get( key )
 
return self.info[ key ];
 
 
end
 
end
  
-- @description Remove <code>key</code> info.
+
-- @description Returns its gathered errors.
function InfoClass:remove( key )
+
function InfoClass:getErrors()
local value = self.info[ key ];
+
return self._errors;
self.info[ key ] = nil;
 
return value;
 
 
end
 
end
  
 
-- @description Store associated category.
 
-- @description Store associated category.
 
function InfoClass:category( category )
 
function InfoClass:category( category )
table.insert( self.categories, category );
+
table.insert( self._categories, category );
 
return self;
 
return self;
 
end
 
end
  
 
-- @description Store error message.
 
-- @description Store error message.
function InfoClass:error( message, default, category )
+
function InfoClass:error( message, default )
self.errors.exists = true;
+
self._errors.exists = true;
table.insert( self.errors, message );
+
table.insert( self._errors, message );
 +
 
 +
return default or self;
 +
end
  
if category then self:category( category ) end
+
function InfoClass:dumpCategories( callback )
 +
callback = callback or function( cat, index )
 +
return cat;
 +
end;
 +
local categories = {
 +
self._errors.exists and ('[[Category:((%s)) transclusions to be checked]]'):format( self._title );
 +
};
 +
for index, cat in ipairs( self._categories ) do
 +
table.insert(
 +
categories,
 +
callback(
 +
('[[Category:%s]]'):format( cat ), -- TODO: sortkey
 +
index
 +
)
 +
);
 +
end
 +
return table.concat( categories );
 +
end
  
return default or self;
+
function InfoClass:dumpErrors( callback )
 +
callback = callback or function( err, index )
 +
return ('%d - %s'):format( index, err );
 +
end;
 +
local errors = {};
 +
for index, err in ipairs( self._errors ) do
 +
table.insert( errors, callback( err, index ) );
 +
end
 +
return table.concat( errors, '\n' );
 
end
 
end
  
-- Return instatiation method:
+
-- @export Instatiation method.
 
return {
 
return {
 
['new'] = InfoClass.new,
 
['new'] = InfoClass.new,
 
}
 
}
--[[setmetatable( {}, {
+
-- </pre>
--['new'] = InfoClass.new,
 
__call = function( ... )
 
return InfoClass.new( ... );
 
end
 
}]]
 
-- <pre>
 

Revision as of 14:26, 10 August 2018

-- <pre>
-- @description Creates objects to store module info.
-- @author [[User:Becasita]]
-- @contact [[User talk:Becasita]]

-- Define class:
local InfoClass = {};
InfoClass.__index = InfoClass;

-- @description Initialize the instance of the InfoClass.
function InfoClass.new( title )
	local data = {
		_title      = title or '',
		_categories = {},
		_errors     = {},
	};

	return setmetatable( data, InfoClass );
end

-- @description Returns its gathered categories.
function InfoClass:getCategories()
	return self._categories;
end

-- @description Returns its gathered errors.
function InfoClass:getErrors()
	return self._errors;
end

-- @description Store associated category.
function InfoClass:category( category )
	table.insert( self._categories, category );
	return self;
end

-- @description Store error message.
function InfoClass:error( message, default )
	self._errors.exists = true;
	table.insert( self._errors, message );

	return default or self;
end

function InfoClass:dumpCategories( callback )
	callback = callback or function( cat, index )
		return cat;
	end;
	local categories = {
		self._errors.exists and ('[[Category:((%s)) transclusions to be checked]]'):format( self._title );
	};
	for index, cat in ipairs( self._categories ) do
		table.insert(
			categories,
			callback(
				('[[Category:%s]]'):format( cat ), -- TODO: sortkey
				index
			)
		);
	end
	return table.concat( categories );
end

function InfoClass:dumpErrors( callback )
	callback = callback or function( err, index )
		return ('%d - %s'):format( index, err );
	end;
	local errors = {};
	for index, err in ipairs( self._errors ) do
		table.insert( errors, callback( err, index ) );
	end
	return table.concat( errors, '\n' );
end

-- @export Instatiation method.
return {
	['new'] = InfoClass.new,
}
-- </pre>