UNPKG

fireball-db

Version:
85 lines (68 loc) 2.21 kB
var Instance, assign, fn, func, i, len, patch, ref, 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; assign = require('lodash').assign; patch = require('./update_builder').patch; Instance = (function() { function Instance(data) { assign(this, data); } Instance.prototype.key = function() { return this._model().key_schema.key_for(this); }; Instance.prototype.refresh = function(params) { return this._model().get(this.key(), params); }; Instance.prototype.patch = function(changes, params) { var Model; if (params == null) { params = {}; } Model = require('./model'); assign(params, patch(changes)); return this._model().update(this.key(), params); }; Instance.prototype.toString = function() { var key, ref, ref1, value, values; values = []; ref = this; for (key in ref) { value = ref[key]; if (this.hasOwnProperty(key)) { values.push(key + ": '" + value + "'"); } } values = values.join(','); return "Instance[" + ((ref1 = this._model()) != null ? ref1.name : void 0) + "](" + values + ")"; }; Instance.extend_with = function(model) { var extension; if (model == null) { throw new Error("Instance extensions require model!"); } extension = (function(superClass) { extend(_Class, superClass); function _Class() { return _Class.__super__.constructor.apply(this, arguments); } return _Class; })(Instance); extension.model = model; return extension; }; Instance.prototype._model = function() { return this.constructor.model; }; return Instance; })(); ref = ['put', 'delete', 'update']; fn = function(func) { return Instance.prototype[func] = function(params) { return this.constructor.model[func](this, params); }; }; for (i = 0, len = ref.length; i < len; i++) { func = ref[i]; fn(func); } module.exports = Instance;