diff --git a/src/Model.js b/src/Model.js index 078fa38e..61494bde 100644 --- a/src/Model.js +++ b/src/Model.js @@ -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() { diff --git a/test/Model.spec.js b/test/Model.spec.js index dca12272..3ac0adde 100644 --- a/test/Model.spec.js +++ b/test/Model.spec.js @@ -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); @@ -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); + }); });