diffusion
Version:
Diffusion JavaScript client
77 lines (57 loc) • 1.79 kB
JavaScript
var MutableRecordModelImpl = require('data/record/model/mutable-record-model-impl');
var RecordImpl = require('data/record/schema/record-impl');
module.exports = function SchemaImpl(constructor, _records) {
var records = {};
var self = this;
_records.forEach(function(record) {
records[record.name] = record;
});
this.getRecord = function(name) {
var record = records[name];
if (record) {
return record;
}
throw new Error("Record '" + name + "' is not known");
};
this.getRecords = function() {
return [].concat(_records);
};
this.records = function() {
return _records;
};
this.firstRecord = function() {
return _records[0];
};
this.lastRecord = function() {
return _records[_records.length - 1];
};
this.createMutableModel = function() {
return new MutableRecordModelImpl(constructor, this);
};
this.createModel = function() {
var model = [];
_records.forEach(function(record) {
var recordValue = record.createModel();
if (record.min > 0) {
for (var i = 0; i < record.min; ++i) {
model.push([].concat(recordValue));
}
}
});
return model;
};
this.asJSON = function() {
return JSON.stringify({ records : _records.map(RecordImpl.toJSON) });
};
this.equals = function(other) {
if (other && other instanceof SchemaImpl) {
var s1 = self.asJSON();
var s2 = other.asJSON();
return s1 === s2;
}
return false;
};
this.toString = function() {
return "Schema [Records=" + _records + "]";
};
};