Difference between revisions of "Module:InfoWrapper"
(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 | + | -- @description Initialize the instance of the InfoClass. |
function InfoClass.new( title ) | function InfoClass.new( title ) | ||
local data = { | local data = { | ||
− | + | _title = title or '', | |
− | + | _categories = {}, | |
− | + | _errors = {}, | |
− | |||
}; | }; | ||
Line 20: | Line 19: | ||
end | end | ||
− | -- @description | + | -- @description Returns its gathered categories. |
− | function InfoClass: | + | function InfoClass:getCategories() |
− | + | return self._categories; | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | return self. | ||
end | end | ||
− | -- @description | + | -- @description Returns its gathered errors. |
− | function InfoClass: | + | function InfoClass:getErrors() |
− | + | return self._errors; | |
− | |||
− | |||
end | end | ||
-- @description Store associated category. | -- @description Store associated category. | ||
function InfoClass:category( category ) | function InfoClass:category( category ) | ||
− | table.insert( self. | + | table.insert( self._categories, category ); |
return self; | return self; | ||
end | end | ||
-- @description Store error message. | -- @description Store error message. | ||
− | function InfoClass:error( message, default | + | function InfoClass:error( message, default ) |
− | self. | + | self._errors.exists = true; |
− | table.insert( self. | + | 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 | ||
− | return | + | 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 | ||
− | -- | + | -- @export Instatiation method. |
return { | return { | ||
['new'] = InfoClass.new, | ['new'] = InfoClass.new, | ||
} | } | ||
− | + | -- </pre> | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | -- <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>