Skip to content

Commit

Permalink
partial advance on quirk implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
nicosommi committed Jul 14, 2015
1 parent 22b8230 commit 0865848
Show file tree
Hide file tree
Showing 6 changed files with 863 additions and 773 deletions.
22 changes: 19 additions & 3 deletions es5/lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ var _jargon = require("jargon");

var _jargon2 = _interopRequireDefault(_jargon);

var _quirk = require("quirk");

var _quirk2 = _interopRequireDefault(_quirk);

var _modelFinderJs = require("./modelFinder.js");

var _collectionJs = require("./collection.js");
Expand Down Expand Up @@ -82,6 +86,13 @@ var Model = (function () {
value: []
},

"additionalAttributes": {
enumerable: false, //this fix db related issues
get: function get() {
return _this.constructor.attributes;
}
},

"isNew": {
get: this[isNew]
},
Expand Down Expand Up @@ -802,13 +813,13 @@ var Model = (function () {
value: function value() {
var _this7 = this;

var attributeNames = {};
var attributes = {};
this.properties.forEach(function (propertyName) {
if (!_this7._associations[propertyName]) {
attributeNames[propertyName] = _this7[propertyName];
attributes[propertyName] = _this7[propertyName];
}
});
return attributeNames;
return attributes;
}
}, {
key: isNew,
Expand Down Expand Up @@ -1205,5 +1216,10 @@ Object.defineProperties(Model, {
var modelQuery = new _modelFinderJs.ModelQuery(Model.database);
return modelQuery.find(this);
}
},
//problem here: if it's static on Model, it will be shared for every Model
//if it's static on Item, it will be inaccesible from Model methods to use that on save for eg
"attributes": {
value: new _quirk2["default"]()
}
});
27 changes: 27 additions & 0 deletions es5/spec/model.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ var _libModelJs = require("../lib/model.js");

var _libModelJs2 = _interopRequireDefault(_libModelJs);

var _quirk = require("quirk");

var _quirk2 = _interopRequireDefault(_quirk);

var _libModelFinderJs = require("../lib/modelFinder.js");

var _ = require("../../");
Expand Down Expand Up @@ -349,6 +353,29 @@ describe("Model(attributes, options)", function () {
});

describe("(static properties)", function () {
describe(".attributes", function () {
it("should be an instance of Quirk", function () {
User.attributes.should.be.instanceOf(_quirk2["default"]);
});

it("should be able to get the attributes from the regular methods", function () {
user.additionalAttributes.should.be.instanceOf(_quirk2["default"]);
});

it("should be able to define a new static property to the model", function () {
User.attributes.specialAttribute = 2;
user.specialAttribute.should.equal(2);
});

it("should be able to define a new static property to the model", function () {
User.attributes.specialAttribute = function specialAttributeGetter() {
return this.id;
};
user = new User({ id: 2 });
user.specialAttribute.should.equal(2);
});
});

describe(".find", function () {
var users = undefined,
userCollection = undefined;
Expand Down
21 changes: 18 additions & 3 deletions es6/lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const flowsync = require("flowsync");
import MultiError from "blunder";
import Datetime from "fleming";
import inflect from "jargon";
import Quirk from "quirk";

import {ModelQuery} from "./modelFinder.js";

Expand Down Expand Up @@ -51,6 +52,13 @@ export default class Model {
value: []
},

"additionalAttributes": {
enumerable: false, //this fix db related issues
get: () => {
return this.constructor.attributes;
}
},

"isNew": {
get: this[isNew]
},
Expand Down Expand Up @@ -746,13 +754,13 @@ export default class Model {
}

[attributes]() {
var attributeNames = {};
var attributes = {};
this.properties.forEach((propertyName) => {
if(!this._associations[propertyName]) {
attributeNames[propertyName] = this[propertyName];
attributes[propertyName] = this[propertyName];
}
});
return attributeNames;
return attributes;
}

[isNew]() {
Expand Down Expand Up @@ -1016,6 +1024,8 @@ export default class Model {
}

[getFieldAttributes]() {
//this, using quirk, will return target with the new attributes on it, useful to EXCLUDE them from here
//TODO: this.additionalAttributes.addAttributes(target); //magic line
let attributeNames = Object.keys(this.attributes);
let fieldAttributes = {};
attributeNames.forEach((attributeName) => {
Expand Down Expand Up @@ -1114,6 +1124,11 @@ Object.defineProperties(Model, {
let modelQuery = new ModelQuery(Model.database);
return modelQuery.find(this);
}
},
//problem here: can't assign property automatically to the concrete model to use it
//https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/observe#Browser_compatibility
"attributes": {
value: new Quirk()
}
});

Expand Down
24 changes: 24 additions & 0 deletions es6/spec/model.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const sinon = require("sinon");
import Database from "almaden";
import Collection from "../lib/collection.js";
import Model, {AssociationSetter} from "../lib/model.js";
import Quirk from "quirk";
import {ModelQuery} from "../lib/modelFinder.js";
import {isPresent} from "../../";

Expand Down Expand Up @@ -248,6 +249,29 @@ describe("Model(attributes, options)", () => {
});

describe("(static properties)", () => {
describe(".attributes", () => {
it("should be an instance of Quirk", () => {
User.attributes.should.be.instanceOf(Quirk);
});

it("should be able to get the attributes from the regular methods", () => {
user.additionalAttributes.should.be.instanceOf(Quirk);
});

it("should be able to define a new static property to the model", () => {
User.attributes.specialAttribute = 2;
user.specialAttribute.should.equal(2);
});

it("should be able to define a new static property to the model", () => {
User.attributes.specialAttribute = function specialAttributeGetter() {
return this.id;
};
user = new User({id: 2});
user.specialAttribute.should.equal(2);
});
});

describe(".find", () => {
let users,
userCollection;
Expand Down
Loading

0 comments on commit 0865848

Please sign in to comment.