-------------------------------------------------------------------------------- -- Title: LWTable.lua -- Description: Like a square peg in a round hole -- Author: Raphaël Szwarc http://alt.textdrive.com/lua/ -- Creation Date: February 1, 2005 -- Legal: Copyright (C) 2005 Raphaël Szwarc -------------------------------------------------------------------------------- -- import dependencies local LWComponent = require( "LWComponent" ) local LWLink = require( "LWLink" ) local LUList = require( "LUList" ) local LUMap = require( "LUMap" ) local LUString = require( "LUString" ) local math = require( "math" ) -- define the class local super = LWComponent local self = super() -- object methods -- method to access this table objects function self:objects() if self._objects == nil then self._objects = self:bindings():get( "objects" ) if self._objects == nil then self._objects = LUList() end end return self._objects end -- method to access this table object count function self:objectCount() if self._objectCount == nil then local aBathIndex = self:currentBatchIndex() - 1 local aBatchSize = self:batchSize() local anIndex = aBathIndex * aBatchSize local aCount = self:objects():size() if ( anIndex + aBatchSize ) > aCount then aBatchSize = aCount - anIndex end self._objectCount = aBatchSize end return self._objectCount end -- method to access this table object index function self:objectIndex() return self._objectIndex end -- method to access this table object function self:object() local aBathIndex = self:currentBatchIndex() - 1 local aBatchSize = self:batchSize() local anObjectIndex = self:objectIndex() local anIndex = ( aBathIndex * aBatchSize ) + anObjectIndex return self:objects():get( anIndex ) end -- method to access this table value function self:value() local anObject = self:object() local aKey = self:key() if anObject:respondsTo( aKey ) == true then return anObject:invoke( aKey ) end return nil end -- key methods -- method to access this table keys function self:keys() if self._keys == nil then self._keys = self:bindings():get( "keys" ) if self._keys == nil then local aDescription = self:bindings():get( "keysDescription" ) self._keys = LUString:components( aDescription, "|" ) end if self._keys == nil then self._keys = LUList() end end return self._keys end -- method to access this table key function self:key() return self:keys():get( self:keyIndex() ) end -- method to access this table key index function self:keyIndex() return self._keyIndex end -- method to access this table current key index function self:currentKeyIndex() if self._currentKeyIndex == nil then self._currentKeyIndex = 1 end return self._currentKeyIndex end -- batch methods -- method to access this table batch count function self:batchCount() if self._batchCount == nil then local anObjectCount = self:objects():size() local aBatchSize = self:batchSize() self._batchCount = math.ceil( anObjectCount / aBatchSize ) end return self._batchCount end -- method to access this table batch index function self:batchIndex() return self._batchIndex end -- method to access this table current batch index function self:currentBatchIndex() if self._currentBatchIndex == nil then self._currentBatchIndex = 1 end return self._currentBatchIndex end -- method to access this table batch size function self:batchSize() return 20 end -- method to access this table batch path function self:batchPath() return "/" end -- method to access this table batch component function self:batchComponent() local someBindings = LUMap() someBindings:put( "object", self:batchIndex() ) someBindings:put( "value", self:batchPath() ) local aComponent = LWLink( self:context(), self, someBindings ) return aComponent end -- method to access this table header function self:header() return self:key() end -- method to access this component header path function self:headerPath() return "/" end -- method to access this table header component function self:headerComponent() local someBindings = LUMap() someBindings:put( "object", self:header() ) someBindings:put( "value", self:headerPath() ) local aComponent = LWLink( self:context(), self, someBindings ) return aComponent end -- method to access this table value component function self:valueComponent() local someBindings = LUMap() someBindings:put( "object", self:value() ) local aComponent = LWLink( self:context(), self, someBindings ) return aComponent end -- method for string representation function self:toString() local aTemplate = self:template() local count = self:batchCount() for index = 1, count do local aBatchTemplate = aTemplate:get( "batches" ) self._batchIndex = index aBatchTemplate:put( "batch", self:batchComponent() ) aTemplate:put( "batches", aBatchTemplate ) end count = self:keys():size() for index = 1, count do local anHeaderTemplate = aTemplate:get( "headers" ) self._keyIndex = index anHeaderTemplate:put( "header", self:headerComponent() ) aTemplate:put( "headers", anHeaderTemplate ) end count = self:objectCount() for index = 1, count do local anObjectTemplate = aTemplate:get( "objects" ) self._objectIndex = index for keyIndex = 1, self:keys():size() do local aKeyTemplate = anObjectTemplate:get( "keys" ) self._keyIndex = keyIndex aKeyTemplate:put( "value", self:valueComponent() ) anObjectTemplate:put( "keys", aKeyTemplate ) end aTemplate:put( "objects", anObjectTemplate ) end return aTemplate:toString() end return self