janus
Version:
the two-faced application library-framework
468 lines (342 loc) • 12.4 kB
JavaScript
// Generated by CoffeeScript 1.12.2
(function() {
var Attribute, BooleanAttribute, DateAttribute, EnumAttribute, List, ListAttribute, Map, Model, ModelAttribute, NumberAttribute, ReferenceAttribute, TextAttribute, Varying, from, isArray, isFunction, ref, types,
extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
hasProp = {}.hasOwnProperty;
from = require('../core/from');
types = require('../core/types');
Model = require('./model').Model;
Map = require('../collection/map').Map;
Varying = require('../core/varying').Varying;
List = require('../collection/list').List;
ref = require('../util/util'), isFunction = ref.isFunction, isArray = ref.isArray;
Attribute = (function(superClass) {
extend(Attribute, superClass);
function Attribute(model, key) {
this.model = model;
this.key = key;
Attribute.__super__.constructor.call(this);
}
Attribute.prototype.setValue = function(value) {
return this.model.set(this.key, value);
};
Attribute.prototype.unsetValue = function() {
return this.model.unset(this.key);
};
Attribute.prototype.getValue_ = function() {
var value;
value = this.model.get_(this.key);
if ((value == null) && (this.initial != null)) {
value = this.initial();
if (this.writeInitial === true) {
this.setValue(value);
}
}
return value;
};
Attribute.prototype.getValue = function() {
return this.model.get(this.key);
};
Attribute.prototype.initial = function() {};
Attribute.prototype.writeInitial = false;
Attribute.prototype.transient = false;
Attribute.deserialize = function(data) {
return data;
};
Attribute.prototype.serialize = function() {
if (this.transient !== true) {
return this.getValue_();
}
};
return Attribute;
})(Model);
TextAttribute = (function(superClass) {
extend(TextAttribute, superClass);
function TextAttribute() {
return TextAttribute.__super__.constructor.apply(this, arguments);
}
return TextAttribute;
})(Attribute);
EnumAttribute = (function(superClass) {
extend(EnumAttribute, superClass);
function EnumAttribute() {
return EnumAttribute.__super__.constructor.apply(this, arguments);
}
EnumAttribute.prototype.values = function() {
return this.values$ != null ? this.values$ : this.values$ = (function(_this) {
return function() {
var vs;
vs = _this._values();
if ((vs != null ? vs.all : void 0) != null) {
vs = vs.all.point(_this.model.pointer());
}
return Varying.of(vs).map(function(xs) {
if (xs == null) {
return new List();
} else if (isArray(xs)) {
return new List(xs);
} else if (xs.isMappable) {
return xs;
} else {
return new List();
}
});
};
})(this)();
};
EnumAttribute.prototype._values = function() {
return new List([]);
};
EnumAttribute.prototype.nullable = false;
return EnumAttribute;
})(Attribute);
NumberAttribute = (function(superClass) {
extend(NumberAttribute, superClass);
function NumberAttribute() {
return NumberAttribute.__super__.constructor.apply(this, arguments);
}
return NumberAttribute;
})(Attribute);
BooleanAttribute = (function(superClass) {
extend(BooleanAttribute, superClass);
function BooleanAttribute() {
return BooleanAttribute.__super__.constructor.apply(this, arguments);
}
return BooleanAttribute;
})(Attribute);
DateAttribute = (function(superClass) {
extend(DateAttribute, superClass);
function DateAttribute() {
return DateAttribute.__super__.constructor.apply(this, arguments);
}
DateAttribute.deserialize = function(data) {
return new Date(data);
};
DateAttribute.prototype.serialize = function() {
var ref1;
if (this.transient !== true) {
return (ref1 = this.getValue_()) != null ? ref1.getTime() : void 0;
}
};
return DateAttribute;
})(Attribute);
ModelAttribute = (function(superClass) {
var RecursiveModelAttribute;
extend(ModelAttribute, superClass);
function ModelAttribute() {
return ModelAttribute.__super__.constructor.apply(this, arguments);
}
ModelAttribute.prototype.modelClass = Model;
ModelAttribute.prototype.writeInitial = true;
ModelAttribute.deserialize = function(data) {
return this.prototype.modelClass.deserialize(data);
};
ModelAttribute.prototype.serialize = function() {
if (this.transient !== true) {
return this.modelClass.prototype.serialize.call(this.getValue_());
}
};
ModelAttribute.of = function(modelClass) {
return (function(superClass1) {
extend(_Class, superClass1);
function _Class() {
return _Class.__super__.constructor.apply(this, arguments);
}
_Class.prototype.modelClass = modelClass;
return _Class;
})(this);
};
ModelAttribute.withInitial = function() {
return (function(superClass1) {
extend(_Class, superClass1);
function _Class() {
return _Class.__super__.constructor.apply(this, arguments);
}
_Class.prototype.initial = function() {
return new this.modelClass();
};
return _Class;
})(this);
};
ModelAttribute.Recursive = RecursiveModelAttribute = (function(superClass1) {
extend(RecursiveModelAttribute, superClass1);
function RecursiveModelAttribute() {
return RecursiveModelAttribute.__super__.constructor.apply(this, arguments);
}
RecursiveModelAttribute.prototype.writeInitial = true;
RecursiveModelAttribute.deserialize = function(data, klass) {
return klass.deserialize(data);
};
RecursiveModelAttribute.prototype.serialize = function() {
var ref1;
if (this.transient !== true) {
return (ref1 = this.getValue_()) != null ? ref1.serialize() : void 0;
}
};
RecursiveModelAttribute.withInitial = function() {
return (function(superClass2) {
extend(_Class, superClass2);
function _Class() {
return _Class.__super__.constructor.apply(this, arguments);
}
_Class.prototype.initial = function() {
return new this.model.constructor();
};
return _Class;
})(this);
};
return RecursiveModelAttribute;
})(Attribute);
return ModelAttribute;
})(Attribute);
ListAttribute = (function(superClass) {
var RecursiveListAttribute;
extend(ListAttribute, superClass);
function ListAttribute() {
return ListAttribute.__super__.constructor.apply(this, arguments);
}
ListAttribute.prototype.listClass = List;
ListAttribute.prototype.writeInitial = true;
ListAttribute.deserialize = function(data) {
return this.prototype.listClass.deserialize(data);
};
ListAttribute.prototype.serialize = function() {
if (this.transient !== true) {
return this.listClass.prototype.serialize.call(this.getValue_());
}
};
ListAttribute.of = function(listClass) {
return (function(superClass1) {
extend(_Class, superClass1);
function _Class() {
return _Class.__super__.constructor.apply(this, arguments);
}
_Class.prototype.listClass = listClass;
return _Class;
})(this);
};
ListAttribute.withInitial = function() {
return (function(superClass1) {
extend(_Class, superClass1);
function _Class() {
return _Class.__super__.constructor.apply(this, arguments);
}
_Class.prototype.initial = function() {
return new this.listClass();
};
return _Class;
})(this);
};
ListAttribute.Recursive = RecursiveListAttribute = (function(superClass1) {
extend(RecursiveListAttribute, superClass1);
function RecursiveListAttribute() {
return RecursiveListAttribute.__super__.constructor.apply(this, arguments);
}
RecursiveListAttribute.prototype.writeInitial = true;
RecursiveListAttribute.deserialize = function(data, klass) {
var datum;
return new (List.of(klass))((function() {
var i, len, results;
results = [];
for (i = 0, len = data.length; i < len; i++) {
datum = data[i];
results.push(klass.deserialize(datum));
}
return results;
})());
};
RecursiveListAttribute.prototype.serialize = function() {
var ref1;
if (this.transient !== true) {
return (ref1 = this.getValue_()) != null ? ref1.serialize() : void 0;
}
};
RecursiveListAttribute.withInitial = function() {
return (function(superClass2) {
extend(_Class, superClass2);
function _Class() {
return _Class.__super__.constructor.apply(this, arguments);
}
_Class.prototype.initial = function() {
return new (List.of(this.model.constructor))();
};
return _Class;
})(this);
};
return RecursiveListAttribute;
})(Attribute);
return ListAttribute;
})(Attribute);
ReferenceAttribute = (function(superClass) {
extend(ReferenceAttribute, superClass);
function ReferenceAttribute() {
return ReferenceAttribute.__super__.constructor.apply(this, arguments);
}
ReferenceAttribute.prototype.isReference = true;
ReferenceAttribute.prototype.transient = true;
ReferenceAttribute.prototype.autoResolve = true;
ReferenceAttribute.prototype._initialize = function() {
return this._result = new Varying();
};
ReferenceAttribute.prototype.request = null;
ReferenceAttribute.prototype.result = function() {
return this.result$ != null ? this.result$ : this.result$ = this._result.flatten();
};
ReferenceAttribute.prototype.resolveWith = function(app) {
var observation, request;
if (this._resolving === true) {
return;
}
this._resolving = true;
request = isFunction(this.request) ? this.request() : this.request;
if (request == null) {
return;
}
observation = null;
this.reactTo(this.model.get(this.key).refCount(), (function(_this) {
return function(count) {
var result;
if (count === 0 && (observation != null)) {
observation.stop();
return observation = null;
} else if (count > 0 && (observation == null)) {
result = request.all != null ? request.all.point(_this.model.pointer()).flatMap(function(request) {
return app.resolve(request);
}) : request.isVarying === true ? request.flatMap(app.resolve) : app.resolve(request);
if (result == null) {
return;
}
_this._result.set(result);
return observation = _this.reactTo(result, function(x) {
return types.result.success.match(x, function(y) {
return _this.setValue(y);
});
});
}
};
})(this));
};
ReferenceAttribute.to = function(x) {
return (function(superClass1) {
extend(_Class, superClass1);
function _Class() {
return _Class.__super__.constructor.apply(this, arguments);
}
_Class.prototype.request = x;
return _Class;
})(this);
};
return ReferenceAttribute;
})(Attribute);
module.exports = {
Attribute: Attribute,
Text: TextAttribute,
Enum: EnumAttribute,
Number: NumberAttribute,
Boolean: BooleanAttribute,
Date: DateAttribute,
Model: ModelAttribute,
List: ListAttribute,
Reference: ReferenceAttribute
};
}).call(this);