Skip to content

Commit

Permalink
feat(computed): Adds mask to computed properties.
Browse files Browse the repository at this point in the history
Also adds some minor code improvements.
  • Loading branch information
iobaixas committed Nov 26, 2014
1 parent d585e76 commit 9a01531
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 20 deletions.
4 changes: 2 additions & 2 deletions src/module/extended/builder-computed.js
Expand Up @@ -25,14 +25,14 @@ RMModule.factory('RMBuilderComputed', ['restmod',
* @description Registers a model computed property
*
* @param {string} _attr Attribute name
* @param {function} _fn Function that returns the desired attribute value when run.
* @param {function} _fn Function that returns the desired attribute value when run.
* @return {BuilderApi} self
*/
attrAsComputed: function(_attr, _fn) {
this.attrComputed(_attr, _fn);
return this;
}
}
};

return restmod.mixin(function() {
this.extend('attrAsComputed', EXT.attrAsComputed, ['computed']);
Expand Down
22 changes: 11 additions & 11 deletions src/module/factory.js
Expand Up @@ -24,8 +24,8 @@ RMModule.factory('RMModelFactory', ['$injector', 'inflector', 'RMUtils', 'RMScop
urlPrefix: null
},
serializer = new Serializer(Model),
computes = {},
defaults = [], // attribute defaults as an array of [key, value]
computes = [], // computed attributes
meta = {}, // atribute metadata
hooks = {},
builder; // the model builder
Expand Down Expand Up @@ -332,19 +332,17 @@ RMModule.factory('RMModelFactory', ['$injector', 'inflector', 'RMUtils', 'RMScop

// default initializer: loads the default parameter values
$initialize: function() {
var tmp, self = this;
for(var i = 0; (tmp = defaults[i]); i++) {
var tmp, i, self = this;
for(i = 0; (tmp = defaults[i]); i++) {
this[tmp[0]] = (typeof tmp[1] === 'function') ? tmp[1].apply(this) : tmp[1];
}
Object.keys(computes).forEach(function(key) {
//console.log(self);
Object.defineProperty(self, key, {

for(i = 0; (tmp = computes[i]); i++) {
Object.defineProperty(self, tmp[0], {
enumerable: true,
get: function() {
return computes[key].apply(self);
}
get: tmp[1]
});
});
}
}

}, CommonApi, RecordApi, ExtendedApi);
Expand Down Expand Up @@ -449,13 +447,15 @@ RMModule.factory('RMModelFactory', ['$injector', 'inflector', 'RMUtils', 'RMScop
* @description Sets a computed value for an attribute.
*
* Computed values are set only on object construction phase.
* Computed values are always masked
*
* @param {string} _attr Attribute name
* @param {function} _fn Function that returns value
* @return {BuilderApi} self
*/
attrComputed: function(_attr, _fn) {
computes[_attr] = _fn;
computes.push([_attr, _fn]);
this.attrMask(_attr, true);
return this;
},

Expand Down
26 changes: 19 additions & 7 deletions test/computed-spec.js
Expand Up @@ -6,15 +6,14 @@ describe('RMBuilderComputed', function() {

beforeEach(module('restmod'));

beforeEach(module(function($provide, restmodProvider) {
beforeEach(module(function($provide) {
$provide.factory('UserModel', function(restmod) {
return restmod.model('/api/users', {
firstName: ''
});
});
}));


// cache entities to be used in tests
beforeEach(inject(['$injector',
function(_$injector) {
Expand All @@ -32,6 +31,7 @@ describe('RMBuilderComputed', function() {
describe('basics', function() {

var DeviceModel, device;

beforeEach(function() {
DeviceModel = restmod.model('/api/devices', {
vendor: 'default vendor',
Expand All @@ -50,16 +50,28 @@ describe('RMBuilderComputed', function() {
});

it('changes when model properties update', function() {
device.vendor = "Apple";
device.model = "iPhone";
device.vendor = 'Apple';
device.model = 'iPhone';
expect(device.fancyName).toEqual('Apple: iPhone');
});

it('should be masked by default',function() {
var encoded = device.$encode();
expect(encoded.fancyName).toBeUndefined();
});

it('should be listed in $each',function() {
var test = {};
device.$each(function(v, k) { test[k] = v; });
expect(test.fancyName).toBeDefined();
});

});

describe('with relations', function() {

var DeviceModel, UserModel, device;
var DeviceModel, device;

beforeEach(function() {

DeviceModel = restmod.model('/api/devices', {
Expand All @@ -70,7 +82,7 @@ describe('RMBuilderComputed', function() {
},
ownedBy: {
computed: function() {
return this.user.firstName + "'s " + this.model;
return this.user.firstName + '\'s ' + this.model;
}
}
});
Expand All @@ -84,7 +96,7 @@ describe('RMBuilderComputed', function() {
});

it('can access related models', function() {
expect(device.ownedBy).toEqual("Johnny's Watch");
expect(device.ownedBy).toEqual('Johnny\'s Watch');
});

});
Expand Down

0 comments on commit 9a01531

Please sign in to comment.