Skip to content

Commit

Permalink
(#26) auto generated is and isSet
Browse files Browse the repository at this point in the history
```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
  • Loading branch information
EnoF committed Feb 23, 2014
1 parent 38110b4 commit 05a8a18
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/ClassFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
}
Expand All @@ -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);
Expand Down
21 changes: 21 additions & 0 deletions test/spec/ClassFactorySpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@
},
autoSet: {
set: 'nothing yet'
},
autoIsSet: {
isSet: false
},
autoIs: {
is: true
}
};

Expand Down Expand Up @@ -73,6 +79,9 @@
},
getAutoSettedValue: function getAutoSettedValue() {
return this.private.autoSet;
},
setAutoIsSetter: function isAutoIsGetter(value) {
this.private.autoIs = value;
}
};

Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit 05a8a18

Please sign in to comment.