Skip to content

Commit

Permalink
json api model adaptation to new usage
Browse files Browse the repository at this point in the history
  • Loading branch information
nicosommi committed Jul 23, 2015
1 parent 65d6959 commit 83b7df3
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 20 deletions.
17 changes: 13 additions & 4 deletions es5/lib/jsonApiModelFormatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,26 @@ var _dovima = require("dovima");

var _dovima2 = _interopRequireDefault(_dovima);

function JsonApiModelFormatter(model) {
function convertModel(model) {
if (model instanceof _dovima2["default"]) {
var attributes = model.attributes;
var attributes = model.toJSON();
var id = attributes.id;
delete attributes.id; //so it's just on the root
return {
type: model.constructor.name,
id: model.id,
id: id,
attributes: attributes
};
} else {
throw new Error("The object provided to be formatted as json is not a model.");
throw new Error("The object provided to be formatted as json is not a dovima Model / Collection.");
}
}

function JsonApiModelFormatter(models) {
if (Array.isArray(models) || models instanceof _dovima.Collection) {
return models.map(convertModel);
} else {
return convertModel(models);
}
}

Expand Down
10 changes: 8 additions & 2 deletions es5/spec/jsonApiModelFormatter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var _dovima = require("dovima");

var _dovima2 = _interopRequireDefault(_dovima);

describe("JsonApiModelFormatter", function () {
describe("jsonApiModelFormatter", function () {
var User = (function (_Model) {
function User() {
_classCallCheck(this, User);
Expand Down Expand Up @@ -51,9 +51,15 @@ describe("JsonApiModelFormatter", function () {
(0, _libJsonApiModelFormatterJs2["default"])(user).attributes.should.eql(jsonApiUserAttributes);
});

it("should support a model array", function () {
var jsonApiUserAttributes = userAttributes;
delete jsonApiUserAttributes.id;
(0, _libJsonApiModelFormatterJs2["default"])([user])[0].attributes.should.eql(jsonApiUserAttributes);
});

it("should throw an exception if the provided object is not an instance of Model", function () {
(function () {
(0, _libJsonApiModelFormatterJs2["default"])(userAttributes);
}).should["throw"]("The object provided to be formatted as json is not a model.");
}).should["throw"]("The object provided to be formatted as json is not a dovima Model / Collection.");
});
});
18 changes: 14 additions & 4 deletions es6/lib/jsonApiModelFormatter.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
import Model from "dovima";
import {Collection} from "dovima";

export default function JsonApiModelFormatter(model) {
function convertModel(model) {
if(model instanceof Model) {
let attributes = model.attributes;
let attributes = model.toJSON();
const id = attributes.id;
delete attributes.id; //so it's just on the root
return {
type: model.constructor.name,
id: model.id,
id: id,
attributes: attributes
};
} else {
throw new Error(`The object provided to be formatted as json is not a model.`);
throw new Error(`The object provided to be formatted as json is not a dovima Model / Collection.`);
}
}

export default function JsonApiModelFormatter(models) {
if(Array.isArray(models) || (models instanceof Collection)) {
return models.map(convertModel);
} else {
return convertModel(models);
}
}
20 changes: 13 additions & 7 deletions es6/spec/jsonApiModelFormatter.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import JsonApiModelFormatter from "../lib/jsonApiModelFormatter.js";
import jsonApiModelFormatter from "../lib/jsonApiModelFormatter.js";
import Model from "dovima";

describe("JsonApiModelFormatter", () => {
describe("jsonApiModelFormatter", () => {
class User extends Model {}

let user,
Expand All @@ -13,22 +13,28 @@ describe("JsonApiModelFormatter", () => {
});

it("should add the model type to the root", () => {
JsonApiModelFormatter(user).type.should.equal("User");
jsonApiModelFormatter(user).type.should.equal("User");
});

it("should add the model id to the root", () => {
JsonApiModelFormatter(user).id.should.equal(user.id);
jsonApiModelFormatter(user).id.should.equal(user.id);
});

it("should add the model's attributes on the attributes element", () => {
let jsonApiUserAttributes = userAttributes;
delete jsonApiUserAttributes.id;
JsonApiModelFormatter(user).attributes.should.eql(jsonApiUserAttributes);
jsonApiModelFormatter(user).attributes.should.eql(jsonApiUserAttributes);
});

it("should support a model array", () => {
let jsonApiUserAttributes = userAttributes;
delete jsonApiUserAttributes.id;
jsonApiModelFormatter([user])[0].attributes.should.eql(jsonApiUserAttributes);
});

it("should throw an exception if the provided object is not an instance of Model", () => {
() => {
JsonApiModelFormatter(userAttributes);
}.should.throw("The object provided to be formatted as json is not a model.");
jsonApiModelFormatter(userAttributes);
}.should.throw("The object provided to be formatted as json is not a dovima Model / Collection.");
});
});
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jsonapi-model-formatter",
"version": "0.1.1",
"version": "0.1.2",
"description": "JSON API formatter for dovima models.",
"main": "index.js",
"scripts": {
Expand Down Expand Up @@ -38,20 +38,22 @@
},
"devDependencies": {
"babel": "^5.5.6",
"babel-eslint": "^3.1.26",
"chai": "^3.0.0",
"coveralls": "^2.11.2",
"eslint": "^0.24.1",
"gulp": "^3.9.0",
"gulp-babel": "^5.1.0",
"gulp-istanbul": "^0.10.0",
"gulp-mocha": "^2.1.1",
"karma": "^0.12.36",
"karma-browserify": "^4.2.1",
"karma-chai": "^0.1.0",
"karma-mocha": "^0.1.10",
"karma-detect-browsers": "^2.0.0",
"karma-chrome-launcher": "^0.2.0",
"karma-detect-browsers": "^2.0.0",
"karma-firefox-launcher": "^0.1.6",
"karma-ie-launcher": "^0.2.0",
"karma-mocha": "^0.1.10",
"karma-phantomjs-launcher": "^0.2.0",
"karma-safari-launcher": "^0.1.1",
"karma-sauce-launcher": "^0.2.11",
Expand Down

0 comments on commit 83b7df3

Please sign in to comment.