diff --git a/README.md b/README.md index 807df73..a6461d5 100644 --- a/README.md +++ b/README.md @@ -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); diff --git a/src/ClassFactory.js b/src/ClassFactory.js index f22adda..bfff92d 100644 --- a/src/ClassFactory.js +++ b/src/ClassFactory.js @@ -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]; @@ -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)); diff --git a/src/LinkedHashMap.js b/src/LinkedHashMap.js index 7e5a2c6..4629809 100644 --- a/src/LinkedHashMap.js +++ b/src/LinkedHashMap.js @@ -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 } }; diff --git a/test/spec/ClassFactorySpec.js b/test/spec/ClassFactorySpec.js index df10ef8..52ba3ac 100644 --- a/test/spec/ClassFactorySpec.js +++ b/test/spec/ClassFactorySpec.js @@ -17,6 +17,9 @@ }, discard: function discard() { this.protected.safe = 'unsafe'; + }, + autoGetSet: { + getSet: 'hello' } }; @@ -58,6 +61,9 @@ }, getProperty: function getProperty() { return this.protected.accessPublic(); + }, + setAutoGeneratedHelloToBye: function setAutoGeneratedHelloToBye() { + this.private.autoGetSet = 'BYEBYE'; } }; @@ -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() { @@ -192,7 +209,11 @@ }, affect: function affect() { return this.protected.lick(); - } + }, + protectedHi: { + getSet: 'hello' + }, + ninja: 'NINJA' }; this.constructor = function constructor() { @@ -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'); @@ -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() {