Skip to content

Commit

Permalink
(#24) Autogenerated Getters and Setters
Browse files Browse the repository at this point in the history
  • Loading branch information
Andy Tang committed Feb 20, 2014
1 parent d22f609 commit bfe7011
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 32 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ Extending a class
});

var Dog = clazz(function Dog() {
this.extend = Animal;
this.extend = 'Animal';

this.constructor = function(birthDate){
this.super.constructor(birthDate);
Expand Down
27 changes: 24 additions & 3 deletions src/ClassFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,15 @@
if (!members.hasOwnProperty(member)) {
continue;
}
if (typeof members[member] === 'function') {
thisInstanceScope[member] = modifyFunctionScope(scope, members[member]);
var memberValue = members[member];

if (memberValue instanceof Object &&
memberValue.getSet !== undefined) {
generateAutoGetSet(scope, thisInstanceScope, member, memberValue.getSet, superScope);
} else if (typeof memberValue === 'function') {
thisInstanceScope[member] = modifyFunctionScope(scope, memberValue);
} else {
thisInstanceScope[member] = members[member];
thisInstanceScope[member] = memberValue;
}
if (superScope instanceof Object) {
superScope[member] = thisInstanceScope[member];
Expand Down Expand Up @@ -174,4 +179,20 @@
}
}
}

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

String.prototype.capitaliseFirstLetter = function capitaliseFirstLetter() {
return this.charAt(0).toUpperCase() + this.slice(1);
};
}(window));
35 changes: 8 additions & 27 deletions src/LinkedHashMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,36 +12,17 @@
*/
var Node = clazz(function Node() {
this.private = {
key: null,
value: null,
previous: null,
next: null
};

this.public = {
getKey: function getKey() {
return this.private.key;
},
setKey: function setKey(key) {
this.private.key = key;
},
getValue: function getValue() {
return this.private.value;
},
setValue: function setValue(value) {
this.private.value = value;
},
getPrevious: function getPrevious() {
return this.private.previous;
key: {
getSet: null
},
setPrevious: function setPrevious(previous) {
this.private.previous = previous;
value: {
getSet: null
},
getNext: function getNext() {
return this.private.next;
previous: {
getSet: null
},
setNext: function setNext(next) {
this.private.next = next;
next: {
getSet: null
}
};

Expand Down
38 changes: 37 additions & 1 deletion test/spec/ClassFactorySpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
},
discard: function discard() {
this.protected.safe = 'unsafe';
},
autoGetSet: {
getSet: 'hello'
}
};

Expand Down Expand Up @@ -58,6 +61,9 @@
},
getProperty: function getProperty() {
return this.protected.accessPublic();
},
setAutoGeneratedHelloToBye: function setAutoGeneratedHelloToBye() {
this.private.autoGetSet = 'BYEBYE';
}
};

Expand Down Expand Up @@ -111,6 +117,17 @@
it('should be possible to access public through protected', function publicToProtected() {
expect(classConstructorTestClass.getProperty()).toEqual('10!');
});

it('should automatically generate a getter and setter for an member', function autoGetSet() {
expect(classConstructorTestClass.getAutoGetSet()).toEqual('hello');
classConstructorTestClass.setAutoGetSet('bye');
expect(classConstructorTestClass.getAutoGetSet()).toEqual('bye');
});

it('should be able access the private auto generated getset member via an other public', function otherPublic() {
classConstructorTestClass.setAutoGeneratedHelloToBye();
expect(classConstructorTestClass.getAutoGetSet()).toEqual('BYEBYE');
});
});

describe('Class extending', function classExtendingSpecs() {
Expand Down Expand Up @@ -192,7 +209,11 @@
},
affect: function affect() {
return this.protected.lick();
}
},
protectedHi: {
getSet: 'hello'
},
ninja: 'NINJA'
};

this.constructor = function constructor() {
Expand All @@ -210,6 +231,12 @@
}
};

this.public = {
miaw: function miaw() {
return this.public.getProtectedHi() + 'miaw';
}
}

this.constructor = function constructor() {
this.super.constructor();
this.public.setName('hello kitty');
Expand Down Expand Up @@ -264,6 +291,15 @@
var kitten = new Kitten();
expect(kitten instanceof Animal).toEqual(true);
});

it('should automatically genearte a getter and setter for an protected', function autoProtectGetSet() {
var kitten = new Kitten();
expect(kitten.getProtectedHi()).toEqual('hello');
expect(kitten.miaw()).toEqual('hellomiaw');
kitten.setProtectedHi('HIHI!');
expect(kitten.getProtectedHi()).toEqual('HIHI!');
expect(kitten.miaw()).toEqual('HIHI!miaw');
});
});

describe('Border cases', function borderCasesSpecs() {
Expand Down

0 comments on commit bfe7011

Please sign in to comment.