
var Calc = require ('./calc');

/**
 * Computes a sum of data in the model.
 */
module.exports = new Class
({
	Extends: Calc
	,Tag: 'db-calc-sum'

	,getRowValue: function (row)
	{
		var value;
	
		if (this._func)
		{
			this._set.row = row;
			value = this._func (this._set);
		}
		else
			value = this._model.getByIndex (row, this.columnIndex);

		return value;
	}

	,before: function (row)
	{
		var value = this.getRowValue (row)
	
		if (value !== null)
			this._sum -= value;
	}

	,after: function (row)
	{
		var value = this.getRowValue (row);

		if (value !== null)
			this._sum += value;
	}

	,init: function ()
	{
		this._sum = 0;
	}

	,done: function ()
	{
		this.value = this._sum;
	}
});
