From 05a8a18c1eade1e76022bb66e86c81624feb6df2 Mon Sep 17 00:00:00 2001 From: EnoF Date: Sun, 23 Feb 2014 16:59:05 +0100 Subject: [PATCH] (#26) auto generated is and isSet ```javascript this.private = { foo: { is: false }, bar: { isSet: true } } ``` will generate ```javascript this.public = { isFoo: function generatedIs(){ return this.private.foo; }, isBar: function generatedIs(){ return this.private.bar; }, setBar: function generatedSet(value){ this.private.bar = value; } } ``` This could probably be optimized --- src/ClassFactory.js | 22 ++++++++++++++++++++-- test/spec/ClassFactorySpec.js | 21 +++++++++++++++++++++ 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/src/ClassFactory.js b/src/ClassFactory.js index 170fe1a..95c6c77 100644 --- a/src/ClassFactory.js +++ b/src/ClassFactory.js @@ -137,7 +137,7 @@ var memberValue = members[member]; if (memberValue instanceof Object && - (hasGet(memberValue) || hasSet(memberValue))) { + (hasGet(memberValue) || hasSet(memberValue) || hasIs(memberValue))) { generateAutoGetSet(scope, thisInstanceScope, member, memberValue, superScope); } else if (typeof memberValue === 'function') { thisInstanceScope[member] = modifyFunctionScope(scope, memberValue); @@ -185,7 +185,12 @@ } function hasSet(value) { - return value.hasOwnProperty('set') || value.hasOwnProperty('getSet'); + return value.hasOwnProperty('set') || value.hasOwnProperty('getSet') || + value.hasOwnProperty('isSet'); + } + + function hasIs(value) { + return value.hasOwnProperty('is') || value.hasOwnProperty('isSet'); } function generateAutoGet(scope, thisInstanceScope, member) { @@ -202,11 +207,22 @@ }; } + function generateAutoIs(scope, thisInstanceScope, member) { + var is = ('is' + member.capitaliseFirstLetter()); + scope.public[is] = function generatedIs() { + return thisInstanceScope[member]; + }; + } + function getDefaultValue(value) { if (value.hasOwnProperty('get')) { return value.get; } else if (value.hasOwnProperty('set')) { return value.set; + } else if (value.hasOwnProperty('is')) { + return value.is; + } else if (value.hasOwnProperty('isSet')) { + return value.isSet; } else { return value.getSet; } @@ -215,6 +231,8 @@ function generateAutoGetSet(scope, thisInstanceScope, member, value) { if (hasGet(value)) { generateAutoGet(scope, thisInstanceScope, member); + } else if (hasIs(value)) { + generateAutoIs(scope, thisInstanceScope, member); } if (hasSet(value)) { generateAutoSet(scope, thisInstanceScope, member); diff --git a/test/spec/ClassFactorySpec.js b/test/spec/ClassFactorySpec.js index 16b9ae4..f46f8f2 100644 --- a/test/spec/ClassFactorySpec.js +++ b/test/spec/ClassFactorySpec.js @@ -26,6 +26,12 @@ }, autoSet: { set: 'nothing yet' + }, + autoIsSet: { + isSet: false + }, + autoIs: { + is: true } }; @@ -73,6 +79,9 @@ }, getAutoSettedValue: function getAutoSettedValue() { return this.private.autoSet; + }, + setAutoIsSetter: function isAutoIsGetter(value) { + this.private.autoIs = value; } }; @@ -147,6 +156,18 @@ classConstructorTestClass.setAutoSet('setter'); expect(classConstructorTestClass.getAutoSettedValue()).toEqual('setter'); }); + + it('should automatically generate a is and setter for an member', function autoIsSet() { + expect(classConstructorTestClass.isAutoIsSet()).toEqual(false); + classConstructorTestClass.setAutoIsSet(true); + expect(classConstructorTestClass.isAutoIsSet()).toEqual(true); + }); + + it('should automatically generate a is for an member', function autoIs() { + expect(classConstructorTestClass.isAutoIs()).toEqual(true); + classConstructorTestClass.setAutoIsSetter(false); + expect(classConstructorTestClass.isAutoIs()).toEqual(false); + }); }); describe('Class extending', function classExtendingSpecs() {