Module:Card gallery/File/Other

From Yugipedia
Jump to: navigation, search
Module documentation[create]
-- <pre>
-- @name Card gallery/File/Other
-- @description Card Gallery file class for other entries.
-- @author [[User:GMTails]]
-- @contact [[User talk:GMTails]]

-- «
-- <filename>
-- »

-- «
-- <filename> // <option1>::<value1>; <optionN>::<valueN>
-- »

-------------
-- Constants:
-------------
local DEFAULT_CARD_BACK = 'Back-EN.png';

----------------
-- Load modules:
----------------
local DATA = require( 'Module:Data' );
local UTIL = require( 'Module:Util' );

local StringBuffer = require( 'Module:StringBuffer' );

---------------
-- Helper vars:
---------------
-- These variables are only used by the init functions.
-- Therefore, even though they are re-assigned every time
-- a new instance is created (through `new()`), there's no
-- problem, because their useful lifetime is only inside
-- that very function.
-- This way, having them here kinda as static variables,
-- supports encapsulation, since each instance of `File`
-- doesn't need to have them.
local _standard, _release, _options;

--------------------
-- Helper functions:
--------------------
-- @name initFilename
-- @description Set the `filename` attribute.
local function initFilename( t )
	t.filename = UTIL.trim( _standard[ 1 ] );
end

-- @name initOptions
-- @description Sets any possible options (`caption`, `twitter` and `youtube`).
local function initOptions( t )
	-- Caption
	t.caption = _options[ 'caption' ];

	-- Twitter ID:
	t.twitter_id = _options[ 'twitter' ];
	
	-- YouTube ID
	t.youtube_id = _options[ 'youtube' ];
end

-- @name init
-- @description Initializes the attributes of the File instance.
local function init( t )
	initFilename( t );
	initOptions( t );
	return t;
end

--------------
-- File class:
--------------
-- @name File
-- @attr counter -> [static] Counts the number of File instances.
local File   = {};
File.__index = File;
File.counter = 0;

-- @name new         -> File constructor.
-- @attr id          -> The file id.
-- @attr flags       -> Control flags:
-- -- hasErrors -> Denotes if a file has errors. Used when parsing the gathered content.
-- @attr filename    -> The file name.
-- @attr description -> A short file description.
-- @attr caption     -> The overriding caption.
-- @attr twitter     -> The Twitter ID.
-- @attr youtube     -> The YouTube video ID.
function File.new( cardGallery, std, rel, opt )
	_standard = std or {};
	_release  = rel or {};
	_options  = opt or {};

	File.counter   = File.counter + 1;
	local fileData = {
		id     = File.counter;
		parent = cardGallery;
		flags  = {
			hasErrors = false,
		},
	};

	return init( setmetatable( fileData, File ) );
end

-- @name error
-- @description Generate consistent error messages.
function File:error( parameter )
	self.flags.hasErrors = true;
	self.parent:error(
		('No %s found for file input number %d!'):format( parameter, self.id )
	)

	return self;
end

-- @name render
-- @description Renders the File by parsing the info gathered.
function File:render()
	if self.flags.hasErrors then
		return ('%s | File #%d'):format( DEFAULT_CARD_BACK, self.id );
	end

	-- Build file:
	local file = self.filename or DEFAULT_CARD_BACK;

	-- Build caption:
	local caption;
	if self.caption then
		caption = StringBuffer()
			:add(
				self.caption
			)
			:flush( '<br />' )
		;
	elseif self.twitter_id then
		caption = StringBuffer()
			:add(
				'[https://twitter.com/YuGiOh_OCG_INFO/status/' .. 
				self.twitter_id ..
				' Yu-Gi-Oh! OCG Twitter]'
			)
			:flush( '<br />' )
		;
	elseif self.youtube_id then
		caption = StringBuffer()
			:add(
				'[https://www.youtube.com/watch?v=' .. 
				self.youtube_id ..
				' Yu-Gi-Oh! OCG YouTube Channel]'
			)
			:flush( '<br />' )
		;
	else
		caption = StringBuffer();
	end

	return ('%s | %s'):format( file, caption:toString() );
end

----------
-- Return:
----------
-- @exports The `File` class.
return File;
-- </pre>