var JRecord, slice = [].slice; JRecord = (function() { JRecord._index = 1; JRecord.attributes = function() { var attr, attrs, i, len, results; attrs = 1 <= arguments.length ? slice.call(arguments, 0) : []; results = []; for (i = 0, len = attrs.length; i < len; i++) { attr = attrs[i]; this.prototype["set_" + attr] = (function(attr) { return function(val) { return this[attr] = val; }; })(attr); results.push(this.prototype["get_" + attr] = (function(attr) { return function() { return this[attr]; }; })(attr)); } return results; }; JRecord.all = function() { return new Collection(this, Object.keys(this._objects)); }; JRecord.find = function(ids) { if (Array.isArray(ids)) { return new Collection(this, ids); } else { if (ids in this._objects) { return this._objects[ids]; } else { console.error('Record not found'); return null; } } }; JRecord["new"] = function(args) { var record; if (args == null) { args = {}; } if (this._objects == null) { this._objects = []; } if (args['index'] != null) { throw new Error('Index is a reserved key'); } args['index'] = this._index++; record = new this(args); this._objects[record.index] = record; return record; }; JRecord.first = function() { return this.all().first(); }; JRecord.last = function() { return this.all().last(); }; JRecord.where = function(cond) { return this.all().where(cond); }; function JRecord(args) { if (args == null) { args = {}; } if (args['index'] == null) { throw new Error('Do not call constructor directly. User `class.new` instead.'); } this.index = args['index']; this.assign_attributes(args); } JRecord.prototype.assign_attributes = function(args) { var key, name, results, val; if (args == null) { args = []; } results = []; for (key in args) { val = args[key]; results.push(typeof this[name = "set_" + key] === "function" ? this[name](val) : void 0); } return results; }; JRecord.prototype.is = function(conds) { var key, value; return ((function() { var results; results = []; for (key in conds) { value = conds[key]; if (('' + this["get_" + key]()) !== ("" + value)) { results.push(true); } } return results; }).call(this)).length === 0; }; return JRecord; })(); window.JRecord = JRecord;