Skip to content

Commit

Permalink
Merge pull request #2 from FreeAllMedia/feature/quirk
Browse files Browse the repository at this point in the history
Feature/quirk
  • Loading branch information
dcrockwell committed Jul 15, 2015
2 parents 22b8230 + 6529b7f commit c58eb02
Show file tree
Hide file tree
Showing 6 changed files with 976 additions and 782 deletions.
33 changes: 29 additions & 4 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 @@ -144,6 +155,9 @@ var Model = (function () {
}
});

//add the quirk to this instance
this.additionalAttributes.addAttributes(this);

this.associate();
this.validate();

Expand Down Expand Up @@ -802,13 +816,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 @@ -1081,7 +1095,13 @@ var Model = (function () {
var attributeNames = Object.keys(this.attributes);
var fieldAttributes = {};
attributeNames.forEach(function (attributeName) {
fieldAttributes[(0, _jargon2["default"])(attributeName).snake.toString()] = _this10[attributeName];
var found = Object.keys(_this10.additionalAttributes).find(function (additionalAttributeName) {
return additionalAttributeName === attributeName;
});
//is just on db if is not an additional attribute
if (!found) {
fieldAttributes[(0, _jargon2["default"])(attributeName).snake.toString()] = _this10[attributeName];
}
});

//add belongsTo associations and remove others
Expand Down Expand Up @@ -1205,5 +1225,10 @@ Object.defineProperties(Model, {
var modelQuery = new _modelFinderJs.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 _quirk2["default"]()
}
});
67 changes: 67 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,69 @@ describe("Model(attributes, options)", function () {
});

describe("(static properties)", function () {
describe(".attributes", function () {
afterEach(function () {
delete User.attributes.specialAttribute; //so does not affect other tests
user = new User();
});

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 = new User();
user.specialAttribute.should.equal(2);
});

describe("(read only)", function () {
beforeEach(function () {
User.attributes.specialAttribute = function specialAttributeGetter() {
return this.id;
};
user = new User({ id: 2 });
});

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

it("should throw when assign to a new read only property", function () {
(function () {
user.specialAttribute = 3;
}).should["throw"]("Cannot set property");
});
});

describe("(getter and setter)", function () {
beforeEach(function () {
User.attributes.specialAttribute = {
get: function getSpecialAttribute() {
return this.id;
},
set: function setSpecialAttribute(newValue) {
this.id = newValue;
}
};
user = new User({ id: 2 });
});

it("should be able to define a new property with get and set to the model", function () {
user.specialAttribute.should.equal(2);
});

it("should be able to change a property with get and set to the model", function () {
user.specialAttribute = 3;
user.specialAttribute.should.equal(3);
});
});
});

describe(".find", function () {
var users = undefined,
userCollection = undefined;
Expand Down
30 changes: 26 additions & 4 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 @@ -113,6 +121,9 @@ export default class Model {
}
});

//add the quirk to this instance
this.additionalAttributes.addAttributes(this);

this.associate();
this.validate();

Expand Down Expand Up @@ -746,13 +757,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 @@ -1019,7 +1030,13 @@ export default class Model {
let attributeNames = Object.keys(this.attributes);
let fieldAttributes = {};
attributeNames.forEach((attributeName) => {
fieldAttributes[inflect(attributeName).snake.toString()] = this[attributeName];
let found = Object.keys(this.additionalAttributes).find((additionalAttributeName) => {
return additionalAttributeName === attributeName;
});
//is just on db if is not an additional attribute
if(!found) {
fieldAttributes[inflect(attributeName).snake.toString()] = this[attributeName];
}
});

//add belongsTo associations and remove others
Expand Down Expand Up @@ -1114,6 +1131,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
64 changes: 64 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,69 @@ describe("Model(attributes, options)", () => {
});

describe("(static properties)", () => {
describe(".attributes", () => {
afterEach(() => {
delete User.attributes.specialAttribute; //so does not affect other tests
user = new User();
});

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 = new User();
user.specialAttribute.should.equal(2);
});

describe("(read only)", () => {
beforeEach(() => {
User.attributes.specialAttribute = function specialAttributeGetter() {
return this.id;
};
user = new User({id: 2});
});

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

it("should throw when assign to a new read only property", () => {
() => {
user.specialAttribute = 3;
}.should.throw("Cannot set property");
});
});

describe("(getter and setter)", () => {
beforeEach(() => {
User.attributes.specialAttribute = {
get: function getSpecialAttribute() {
return this.id;
},
set: function setSpecialAttribute(newValue) {
this.id = newValue;
}
};
user = new User({id: 2});
});

it("should be able to define a new property with get and set to the model", () => {
user.specialAttribute.should.equal(2);
});

it("should be able to change a property with get and set to the model", () => {
user.specialAttribute = 3;
user.specialAttribute.should.equal(3);
});
});
});

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

0 comments on commit c58eb02

Please sign in to comment.