Skip to content

Commit

Permalink
(#25) auto generated get or set
Browse files Browse the repository at this point in the history
  • Loading branch information
EnoF committed Feb 22, 2014
1 parent 636212c commit ace027d
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 5 deletions.
40 changes: 35 additions & 5 deletions src/ClassFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@
var memberValue = members[member];

if (memberValue instanceof Object &&
memberValue.getSet !== undefined) {
generateAutoGetSet(scope, thisInstanceScope, member, memberValue.getSet, superScope);
(hasGet(memberValue) || hasSet(memberValue))) {
generateAutoGetSet(scope, thisInstanceScope, member, memberValue, superScope);
} else if (typeof memberValue === 'function') {
thisInstanceScope[member] = modifyFunctionScope(scope, memberValue);
} else {
Expand Down Expand Up @@ -180,18 +180,48 @@
}
}

function generateAutoGetSet(scope, thisInstanceScope, member, value) {
function hasGet(value) {
return value.hasOwnProperty('get') || value.hasOwnProperty('getSet');
}

function hasSet(value) {
return value.hasOwnProperty('set') || value.hasOwnProperty('getSet');
}

function generateAutoGet(scope, thisInstanceScope, member) {
var getter = ('get' + member.capitaliseFirstLetter());
var setter = ('set' + member.capitaliseFirstLetter());
thisInstanceScope[member] = value;
scope.public[getter] = function generatedGet() {
return thisInstanceScope[member];
};
}

function generateAutoSet(scope, thisInstanceScope, member) {
var setter = ('set' + member.capitaliseFirstLetter());
scope.public[setter] = function generatedSet(newValue) {
thisInstanceScope[member] = newValue;
};
}

function getDefaultValue(value) {
if (value.hasOwnProperty('get')) {
return value.get;
} else if (value.hasOwnProperty('set')) {
return value.set;
} else {
return value.getSet;
}
}

function generateAutoGetSet(scope, thisInstanceScope, member, value) {
if (hasGet(value)) {
generateAutoGet(scope, thisInstanceScope, member);
}
if (hasSet(value)) {
generateAutoSet(scope, thisInstanceScope, member);
}
thisInstanceScope[member] = getDefaultValue(value);
}

String.prototype.capitaliseFirstLetter = function capitaliseFirstLetter() {
return this.charAt(0).toUpperCase() + this.slice(1);
};
Expand Down
19 changes: 19 additions & 0 deletions test/spec/ClassFactorySpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@
},
autoGetSet: {
getSet: 'hello'
},
autoGet: {
get: 'getter'
},
autoSet: {
set: 'nothing yet'
}
};

Expand Down Expand Up @@ -64,6 +70,9 @@
},
setAutoGeneratedHelloToBye: function setAutoGeneratedHelloToBye() {
this.private.autoGetSet = 'BYEBYE';
},
getAutoSettedValue: function getAutoSettedValue() {
return this.private.autoSet;
}
};

Expand Down Expand Up @@ -128,6 +137,16 @@
classConstructorTestClass.setAutoGeneratedHelloToBye();
expect(classConstructorTestClass.getAutoGetSet()).toEqual('BYEBYE');
});

it('should automatically generate a getter for an member', function autoGet() {
expect(classConstructorTestClass.getAutoGet()).toEqual('getter');
});

it('should automatically generate a setter for an member', function autoSet() {
expect(classConstructorTestClass.getAutoSettedValue()).toEqual('nothing yet');
classConstructorTestClass.setAutoSet('setter');
expect(classConstructorTestClass.getAutoSettedValue()).toEqual('setter');
});
});

describe('Class extending', function classExtendingSpecs() {
Expand Down

0 comments on commit ace027d

Please sign in to comment.