Skip to content

Commit

Permalink
Improve model functionality (#88)
Browse files Browse the repository at this point in the history
* Update working with attributes in model

* Extend Model get functionality to work with complex keys

* Make attributes assign more compact
  • Loading branch information
spzm authored and mAiNiNfEcTiOn committed Feb 12, 2017
1 parent 382918f commit 3d6c7ce
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ import _ from 'lodash';

export default class Model {
constructor(attributes) {
this.attributes = attributes;
this.attributes = Object.assign({}, attributes);
}

get(key) {
if (typeof key !== 'string') return undefined;

return _.get(this.attributes, key);
}

toJS() {
Expand Down
20 changes: 19 additions & 1 deletion test/Model.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import { Model } from '../src';
describe('Model', () => {
const myAttributes = {
attribute1: 'value1',
attribute2: 'value2'
attribute2: 'value2',
attribute3: {
attribute31: 'value31'
}
};

const myModelInstance = new Model(myAttributes);
Expand All @@ -23,4 +26,19 @@ describe('Model', () => {
expect('toJS' in myModelInstance).to.be.equal(true);
expect(myModelInstance.toJS()).to.be.deep.equal(myAttributes);
});

it('must have the attributes available with .get() method', () => {
expect(myModelInstance.get('attribute1')).to.be.deep.equal('value1');
expect(myModelInstance.get('attribute2')).to.be.deep.equal('value2');
expect(myModelInstance.get('attribute3')).to.be.deep.equal(myAttributes.attribute3);
});

it('must return nested attribut value for the key by get() method', () => {
expect(myModelInstance.get('attribute3.attribute31')).to.be.deep.equal('value31');
});

it('must return an undefined using .get() method if attribute doesn\'t exist', () => {
expect(myModelInstance.get()).to.be.deep.equal(undefined);
expect(myModelInstance.get('attributeNotExist')).to.be.deep.equal(undefined);
});
});

0 comments on commit 3d6c7ce

Please sign in to comment.