Difference between revisions of "Module:InfoWrapper"

From Yugipedia
Jump to: navigation, search
(Remove unnecessary methods. Add some useful ones. Cleanup.)
(Add __class attribute. Add setTitle method. Add --Doc documentation.)
Line 1: Line 1:
 
-- <pre>
 
-- <pre>
-- @description Creates objects to store module info.
+
--[=[Doc
-- @author [[User:Becasita]]
+
@module Info class
-- @contact [[User talk:Becasita]]
+
@description Creates an object to store module info.
 +
@author [[User:Becasita]]
 +
@contact [[User talk:Becasita]]
 +
]=]
  
-- Define class:
+
--[[Doc
 +
@class InfoClass
 +
@description Stores and provides an interface to manage module info.
 +
@exportable
 +
]]
 
local InfoClass = {};
 
local InfoClass = {};
 
InfoClass.__index = InfoClass;
 
InfoClass.__index = InfoClass;
 +
InfoClass.__class = InfoClass;
  
-- @description Initialize the instance of the InfoClass.
+
--[[Doc
 +
@function InfoClass new
 +
@description Initializes an instance of `InfoClass`.
 +
@return {InfoClass} New instance of `InfoClass`.
 +
]]
 
function InfoClass.new( title )
 
function InfoClass.new( title )
 
local data = {
 
local data = {
Line 19: Line 31:
 
end
 
end
  
-- @description Returns its gathered categories.
+
--[[Doc
 +
@method InfoClass setTitle
 +
@description Changes the instance title.
 +
@parameter {string|nil} title The new title. If `nil`, the empty string is used.
 +
@return {StringBuffer} `self`
 +
]]
 +
function InfoClass:setTitle( title )
 +
self._title = title or '';
 +
 
 +
return self;
 +
end
 +
 
 +
--[[Doc
 +
@method InfoClass getCategories
 +
@description Get the stored categories.
 +
@return {table} Table, as an array, of all of the stored categories.
 +
]]
 
function InfoClass:getCategories()
 
function InfoClass:getCategories()
 
return self._categories;
 
return self._categories;
 
end
 
end
  
-- @description Returns its gathered errors.
+
--[[Doc
 +
@method InfoClass getErrors
 +
@description Get the stored errors.
 +
@return {table} Table, as an array, of all of the stored errors.
 +
]]
 
function InfoClass:getErrors()
 
function InfoClass:getErrors()
 
return self._errors;
 
return self._errors;
 
end
 
end
  
-- @description Store associated category.
+
--[[Doc
 +
@method InfoClass category
 +
@description Stores a new category.
 +
@parameter {string} category The category name
 +
(no need to prefix the `Category` namespace).
 +
@return {InfoClass} `self`
 +
]]
 
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.
+
--[[Doc
 +
@method InfoClass error
 +
@description Stores a new error.
 +
@parameter {string} message The error message.
 +
@parameter {*} default A default return a value.
 +
@return {*|InfoClass} `default` or `self`.
 +
]]
 
function InfoClass:error( message, default )
 
function InfoClass:error( message, default )
 
self._errors.exists = true;
 
self._errors.exists = true;
Line 43: Line 88:
 
end
 
end
  
 +
--[[Doc
 +
@method InfoClass dumpCategories
 +
]]
 
function InfoClass:dumpCategories( callback )
 
function InfoClass:dumpCategories( callback )
 
callback = callback or function( cat, index )
 
callback = callback or function( cat, index )
 
return cat;
 
return cat;
 
end;
 
end;
 +
 
local categories = {
 
local categories = {
 
self._errors.exists and ('[[Category:((%s)) transclusions to be checked]]'):format( self._title );
 
self._errors.exists and ('[[Category:((%s)) transclusions to be checked]]'):format( self._title );
 
};
 
};
 +
 
for index, cat in ipairs( self._categories ) do
 
for index, cat in ipairs( self._categories ) do
 
table.insert(
 
table.insert(
Line 59: Line 109:
 
);
 
);
 
end
 
end
 +
 
return table.concat( categories );
 
return table.concat( categories );
 
end
 
end
  
 +
--[[Doc
 +
@method InfoClass dumpErrors
 +
]]
 
function InfoClass:dumpErrors( callback )
 
function InfoClass:dumpErrors( callback )
 
callback = callback or function( err, index )
 
callback = callback or function( err, index )
 
return ('%d - %s'):format( index, err );
 
return ('%d - %s'):format( index, err );
 
end;
 
end;
 +
 
local errors = {};
 
local errors = {};
 +
 
for index, err in ipairs( self._errors ) do
 
for index, err in ipairs( self._errors ) do
 
table.insert( errors, callback( err, index ) );
 
table.insert( errors, callback( err, index ) );
 
end
 
end
return table.concat( errors, '\n' );
+
 
 +
return table.concat( errors, '\n' ); -- TODO: more flexible delimiter
 
end
 
end
  
-- @export Instatiation method.
+
--[=[Doc
 +
@exports The `InfoClass` class constructor (`new`).
 +
@see [[#InfoClass.new]]
 +
]=]
 
return {
 
return {
 
['new'] = InfoClass.new,
 
['new'] = InfoClass.new,
 
}
 
}
 
-- </pre>
 
-- </pre>

Revision as of 20:14, 12 October 2018

-- <pre>
--[=[Doc
@module Info class
@description Creates an object to store module info.
@author [[User:Becasita]]
@contact [[User talk:Becasita]]
]=]

--[[Doc
@class InfoClass
@description Stores and provides an interface to manage module info.
@exportable
]]
local InfoClass = {};
InfoClass.__index = InfoClass;
InfoClass.__class = InfoClass;

--[[Doc
@function InfoClass new
@description Initializes an instance of `InfoClass`.
@return {InfoClass} New instance of `InfoClass`.
]]
function InfoClass.new( title )
	local data = {
		_title      = title or '',
		_categories = {},
		_errors     = {},
	};

	return setmetatable( data, InfoClass );
end

--[[Doc
@method InfoClass setTitle
@description Changes the instance title.
@parameter {string|nil} title The new title. If `nil`, the empty string is used.
@return {StringBuffer} `self`
]]
function InfoClass:setTitle( title )
	self._title = title or '';

	return self;
end

--[[Doc
@method InfoClass getCategories
@description Get the stored categories.
@return {table} Table, as an array, of all of the stored categories.
]]
function InfoClass:getCategories()
	return self._categories;
end

--[[Doc
@method InfoClass getErrors
@description Get the stored errors.
@return {table} Table, as an array, of all of the stored errors.
]]
function InfoClass:getErrors()
	return self._errors;
end

--[[Doc
@method InfoClass category
@description Stores a new category.
@parameter {string} category The category name
(no need to prefix the `Category` namespace).
@return {InfoClass} `self`
]]
function InfoClass:category( category )
	table.insert( self._categories, category );

	return self;
end

--[[Doc
@method InfoClass error
@description Stores a new error.
@parameter {string} message The error message.
@parameter {*} default A default return a value.
@return {*|InfoClass} `default` or `self`.
]]
function InfoClass:error( message, default )
	self._errors.exists = true;
	table.insert( self._errors, message );

	return default or self;
end

--[[Doc
@method InfoClass dumpCategories
]]
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

--[[Doc
@method InfoClass dumpErrors
]]
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' ); -- TODO: more flexible delimiter
end

--[=[Doc
@exports The `InfoClass` class constructor (`new`).
@see [[#InfoClass.new]]
]=]
return {
	['new'] = InfoClass.new,
}
-- </pre>