Created
October 20, 2013 01:47
-
-
Save agonbina/7063884 to your computer and use it in GitHub Desktop.
Separating command and query(setter/getter) of an object, enforcing command invocation(in this case a validation function) when a setter is called, making the dependent variable 'private' by making the constructor a closure, and using the ECMA5 Object.preventExtensions to block any further (mutation?) on the instances created by the constructor(…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset=utf-8 /> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*var fields = { | |
docRoot: { | |
validator: validateDocRoot, | |
defaultValue: '/somewhere' | |
}, | |
somethingElse: { | |
validator: validateSomethingElse | |
} | |
}; | |
// Query, because it is returning 'config' | |
function configure(values) { | |
var key, config = { }; | |
for(key in fields) { | |
if(typeof values[key] !== 'undefined') { | |
fields[key].validator(values[key]); | |
config[key] = values[key]; | |
} else { | |
config[key] = values[key].defaultValue; | |
} | |
} | |
return config; | |
} | |
// Command, because it doesn't return anything | |
function validateDocRoot(config) { | |
var fs = require('fs'), | |
stat; | |
stat = fs.statSync(config.docRoot); | |
if(!stat.isDirectory()) { | |
throw { | |
error: 'Path is not a directory' | |
}; | |
} | |
} | |
function validateSomethingElse(config) { | |
} | |
describe('configure tests', function() { | |
it('sets the values correctly to the config obj', function(){ | |
var config = configure({ docRoot: '/tmp', someVal: 'someVal' }); | |
expect(config).not.toBeUndefined(); | |
expect(config.someVal).toEqual('someVal'); | |
expect(config.docRoot).toEqual('/tmp'); | |
}); | |
}); | |
describe('validateDocRoot tests', function() { | |
it('throws an error if docRoot is not a directory', function() { | |
validateDocRoot({ docRoot: '/xxx' }); | |
}); | |
}); */ | |
var Obj = (function() { | |
return function() { | |
var docRoot = '/somewhere'; | |
this.validateDocRoot = function() { | |
// validation logic, throw Error if not OK | |
}; | |
this.setDocRoot = function(value) { | |
this.validateDocRoot(value); | |
docRoot = value; | |
}; | |
this.getDocRoot = function() { | |
return docRoot; | |
}; | |
Object.preventExtensions(this); | |
}; | |
}()); | |
// Using the Obj constructor | |
var myObject = new Obj(); | |
myObject.setDocRoot('/my/tmp/path'); | |
console.log(myObject.getDocRoot()); | |
//Testing the Obj constructor | |
describe('validate docRoot', function() { | |
var config = new Obj(); | |
it('throw if docRoot does not exist', function() { | |
expect(config.validateDocRoot.bind(config, '/xxx')).toThrow(); | |
}); | |
it('not throw if docRoot exists', function() { | |
expect(config.validateDocRoot.bind(config, '/correctPath')).not.toThrow(); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment