var Collection, indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; }, slice = [].slice; Collection = (function() { Collection.has_many = function(owner, record, ids) { var collection; collection = new Collection(record); collection._set_owner(owner); collection.assign_attrs_or_ids(ids); return collection; }; function Collection(record, attrs_or_ids) { this._record = record; this._ids = []; if (attrs_or_ids) { this.assign_attrs_or_ids(attrs_or_ids); } } Collection.prototype.assign_attrs_or_ids = function(attrs_or_ids) { var attr, i, len, results; if (attrs_or_ids == null) { attrs_or_ids = []; } if (!Array.isArray(attrs_or_ids)) { attrs_or_ids = [attrs_or_ids]; } if (attrs_or_ids.length > 0) { if (typeof attrs_or_ids[0] === 'object') { results = []; for (i = 0, len = attrs_or_ids.length; i < len; i++) { attr = attrs_or_ids[i]; results.push(this.create(attr)); } return results; } else { return this._ids = attrs_or_ids; } } }; Collection.prototype.create = function(args) { var record; if (args == null) { args = {}; } if (this._owner != null) { args[this._owner_name + "_index"] = this._owner.index; args["" + this._owner_name] = this._owner; } record = this._record["new"](args); this._ids.push(record.index); return record; }; Collection.prototype.all = function() { return this; }; Collection.prototype.any = function() { return this._ids.length > 0; }; Collection.prototype.find = function(id) { if (indexOf.call(this._ids, id) >= 0) { return this._record.find(id); } }; Collection.prototype.first = function() { return this.find(this._ids[0]); }; Collection.prototype.last = function() { return this.find(this._ids[this._ids.length - 1]); }; Collection.prototype.each = function(callback) { var i, id, len, ref, results; ref = this._ids; results = []; for (i = 0, len = ref.length; i < len; i++) { id = ref[i]; results.push(typeof callback === "function" ? callback(this.find(id)) : void 0); } return results; }; Collection.prototype.select = function() { var attr, attrs, data, i, id, j, len, len1, record, ref, results; attrs = 1 <= arguments.length ? slice.call(arguments, 0) : []; ref = this._ids; results = []; for (i = 0, len = ref.length; i < len; i++) { id = ref[i]; record = this.find(id); data = {}; for (j = 0, len1 = attrs.length; j < len1; j++) { attr = attrs[j]; data[attr] = record[attr]; } results.push(data); } return results; }; Collection.prototype.pluck = function() { var attr, attrs, data, id; attrs = 1 <= arguments.length ? slice.call(arguments, 0) : []; data = (function() { var i, len, ref, results; ref = this._ids; results = []; for (i = 0, len = ref.length; i < len; i++) { id = ref[i]; results.push((function() { var j, len1, results1; results1 = []; for (j = 0, len1 = attrs.length; j < len1; j++) { attr = attrs[j]; results1.push(this.find(id)[attr]); } return results1; }).call(this)); } return results; }).call(this); if (attrs.length === 1) { return [].concat.apply([], data); } else { return data; } }; Collection.prototype.where = function(cond) { var id, ids; ids = (function() { var i, len, ref, results; ref = this._ids; results = []; for (i = 0, len = ref.length; i < len; i++) { id = ref[i]; if (this.find(id).is(cond)) { results.push(id); } } return results; }).call(this); return new Collection(this._record, ids); }; Collection.prototype.update_all = function(attrs) { return this.each(function(record) { return record.assign_attributes(attrs); }); }; Collection.prototype.to_a = function() { var i, id, len, ref, results; ref = this._ids; results = []; for (i = 0, len = ref.length; i < len; i++) { id = ref[i]; results.push(this.find(id)); } return results; }; Collection.prototype._set_owner = function(owner) { this._owner = owner; return this._owner_name = owner.constructor.name.toLowerCase(); }; return Collection; })(); window.Collection = Collection;