ethstats-server/models/collection.js

71 lines
1.2 KiB
JavaScript
Raw Normal View History

2015-02-17 03:04:15 +01:00
var _ = require('lodash');
var Node = require('./node');
var Collection = function Collection()
{
this._list = [];
return this;
}
Collection.prototype.add = function(data)
{
2015-02-17 06:12:44 +01:00
var node = this.getNodeOrNew({ id : data.id }, data);
2015-02-17 10:30:41 +01:00
node.setInfo(data);
2015-02-17 03:04:15 +01:00
2015-02-17 06:12:44 +01:00
return node.getInfo();
2015-02-17 03:04:15 +01:00
}
2015-02-17 06:12:44 +01:00
Collection.prototype.update = function(id, stats)
2015-02-17 03:04:15 +01:00
{
2015-02-17 06:12:44 +01:00
var node = this.getNode({ id: id });
if(!node)
return false;
node.stats = stats;
return node.getStats();
2015-02-17 03:04:15 +01:00
}
Collection.prototype.getIndex = function(search)
{
return _.findIndex(this._list, search);
}
Collection.prototype.getNode = function(search)
{
var index = this.getIndex(search);
2015-02-17 06:12:44 +01:00
if(index >= 0)
2015-02-17 03:04:15 +01:00
return this._list[index];
return false;
}
2015-02-17 06:12:44 +01:00
Collection.prototype.getNodeByIndex = function(index)
{
if(this._list[index])
return this._list[index];
return false;
}
Collection.prototype.getIndexOrNew = function(search, data)
{
var index = this.getIndex(search);
return (index >= 0 ? index : this._list.push(new Node(data)) - 1);
}
Collection.prototype.getNodeOrNew = function(search, data)
2015-02-17 03:04:15 +01:00
{
2015-02-17 06:12:44 +01:00
return this.getNodeByIndex(this.getIndexOrNew(search, data));
2015-02-17 03:04:15 +01:00
}
2015-02-17 06:12:44 +01:00
Collection.prototype.all = function()
2015-02-17 03:04:15 +01:00
{
2015-02-17 06:12:44 +01:00
return this._list;
2015-02-17 03:04:15 +01:00
}
module.exports = Collection;