Modul:Foundationclass/doc

This is [[MediaWiki:Tagline]]. Set to <code>display:none</code> by chameleon skin.
Wechseln zu:Navigation, Suche

This is a Template for Class Modules. It implements a class in Lua using Module:Middleclass and provides serveral static and public methods, commonly used in the typical form>page>store process.

Usage

This is in a way an abstract class. It provides some static and public methods for resuse, and also maintains a store of private properties. If can be used to fast and easily create a typical class module that can handle:

  • creating an appropriate form
  • supplying your respective template with functionalities for
    • object initialization via template parameters, arguments, and datastore (self initialization)
    • general data assesment (basic plausibility tests)
    • data storage (supported are cargo and semantic media wiki)
    • error and warnings management
    • output and rendering
  • creating a template documentation

You can and should adjust the module in this methods:

  • individual attribute adjustment for the semantic forms fields
  • individual plausibility testing
  • individual data initialization
  • individual data store adjustments before stashing

You can access the objects core data store with getCoreData() and its uid with getUid().

How-To

This is, what you have to do (see Useful templates for content of the corresponding pages):

  1. create your base module Module:Name
  2. create your class module Module:Name/class. For this, copy lua sample code below this list and apply the following changes:
    1. change all occurences of className into your class name
    2. change the highlighted lines and replace 'Name' with the Name of your module
    3. implement the abstract methods:
      1. myAgumentProcessing(coreData)
      2. myDataAdjustments(data)
      3. myPlausibilityTest(args)
      4. myStashAdjustments(stash)
      5. static:mySfDynamicFieldAttribute(fieldname, attribute, value)
    4. for the usual output generation, implement (shells given in sample code)
      1. addInfobox()
      2. addPageBody()
  3. create and fill your configuration Module:Name/config
  4. create your template Template:Name
  5. create your form Form:Name
  6. if applicable, create your category page Category:Name
  7. fill in documentation:
    1. of your base module (place one invoke below the first comment and before the includeonly)
    2. of your class module (new docu for your own public, static and private methods (which you can probably copy from other classes) and one invoke to show the inheritance. Sample code provided in Useful templates
    3. of your class module's configuration (just copy content)
    4. of your template (one invoke below the first comment and before the includeonly)

Useful templates

private, public, class or instance

Zugriffsmatrix
- public private
instance
function MyClass:publicMethod()
MyClass.publicProperty
local _privateMethod = function (self, var)
_private[self]._privateProperty
class
function MyClass.static:staticMethod()
MyClass.static.staticProperty
-- accessing
MyClass.staticProperty
-- being "outside":
MyClass:staticMethod()
-- being inside a static method
self:staticMethod()
-- being inside a public method
self.class:staticMethod()
local _privateMethod = function (self, var)
local _privateProperty

private

property

you can declare a property private in one of two ways:

  1. anywhere, preferably before your constructor, declare a local var to have a private propery, this is not recommended, but it allows you to declare a private property, that can be used by your static methods. thus being somewhat private static
  2. for your instances, you can use the table _private[self] as a private store. it is available after your construrctor has been called (since this is an instance attribute)
method

a private method is declared thus: local _privateMethod = function(self, var). If you want to use the method in an public/static/other private method before it is declared (aka: the calling method is on a lower line number than your private method's declaration), decare it ahead and define it later:

local _privateImportantMethod
..
privateImportantMethod = function(self, var)

private class and instance methods are basically the same.

public

property

can be dclared via class.property. DONT DO THIS. It is bad style!

method

Declare them class:publicMethod(var). They are, after all, public

NOTE: public methods can be called by an instance, but also as static method:

function Class:public()
	return 'public: ' .. tostring(self)
end

local me = Class:new()
me.public()	-- returns "public: Instance of class Class"
Class:public()	-- returns "public: class Class"


static

property

Declare a static property like this: class.static.myProperty = 'Hello World'. This produces a public property, that can be accessed by the class, childclasses or instances via *.myProperty (replace * with class, object, childclass, ...)

method

Much the same as static properties, but you should use the colon operator: function Class.static:staticMethod() when definig the method. On accessig this method, you should use the dot-notation: function Class.staticMethod(self)

NOTE: Other than public methods, they can only be called by the class, but not by an instance:

function Class.static:static()
	return 'static: ' .. tostring(self)
end

local me = Class:new()
me.static()	-- returns "error:  attempt to call method 'static' (a nil value)"
Class:static()	-- returns "static: class Class"
Class.static(self)	-- this is not called via colon (:) operator as to preserve the childClass's scope when inheriting from this class


Methods

Constructor

new(uid, superhandler)

Creates a new Object for the class.

uid
variable, suggested
used to identify the object. defaults to mw.title.getCurrentTitle().prefixedText
superhandler
object of childclass of Foundationclass, optional
supply the superhandler, if you instanciate an object within another object. the instanciated object will pass errors and warnings to the superhandler and forgo adding an object category
return
object, of the class

private methods

These private Methods can not be used by child classes!

_amIPlausible(self)

Returs true or false, whether the object is plausible or not (i.e. has no errors or 1+)

self
object, me
return
boolean, whether the object is plausible or not

_debug(self, level, text)

Adds output to the internal debug log.

self
object, me
level
int; mandatory
debug level for the message
text
string; mandatory
debug message text
return
void

_plausibilityTest(self, args)

Checks, if input provided via template is plausible enough, to represent the object (to be stored and displayed). Also fills the list of errors and does some argument preparation.

self
object, me
args
table, mandatory
arguments passed to the template to be checked
return
boolean, whether the object is plausible or not

Properties

static

FoundationClass.globalConfig
table, holds some global configuration (data, that applies to all classes the same way). See Module:Foundationclass/globalconfig for details.
FoundationClass.myCargoUtil
table, instance of Module:CargoUtil
FoundationClass.mySwmUtil
table, instance of Module:SmwUtil
FoundationClass.myTableTools
table, instance of Module:TableTools
FoundationClass.myYesno
table, instance of Module:Yesno

private

Note: all private properties are stored in table _private[self]. _private is a static array that is indexed by self, so the table _private[self] holds all properties for instance self.

categories
table, holds the objects categories
coreData
table, holds the objects data ofter initializing
dbg
object, my instance of Module:Debug/class for debugging purposes
errors
table, a list of errors that occurred
fullpagename
string, name of currentpage including namespace
initialized
boolean, is this object properly initialized
loadedDataFromDataStore
boolean, did this object get its data from the data store
output
object of type mw.html, holds all output to be rendered()
pagename
string, name of current page (or current page's super page) w/o namespace
uid
string, uinique identifier ('NIL' if unset)
warnings
table, a list of warnings that came up

Configuration Data

This class holds gets its control data from the child class. Also, there is some global configuration data in Module:Foundationclass/globalconfig, that applies to all classes.

Dependencies

If you want to use Module:Foundationclass, you also need the following items (and their doc pages!):

also

and of course Extension:Semantic Forms

Also, have a look at the extended functionality of the Class engine.

Interface messages

To generate the Useful_templates, Foundationclass makes use of the following interface messages:

Next in dev queue

  1. have foundation class try to detect integer inputs (by td_type or cargo_type) and run a regexp in _plausibilityTest
  2. have foundationclass use mw message system for printouts instead of hardcoded text
  3. a template or page, that #autogenerates all categories, defined in Module:Foundationclass/globalconfig