Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
created a config module to apply default options and grant access to …
…current options.
- Loading branch information
ricardo
committed
Apr 22, 2014
1 parent
c0cc9a0
commit dddd119
Showing
14 changed files
with
215 additions
and
54 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
'use strict'; | ||
// Module dependencies. | ||
var _ = require('lodash'); | ||
// we will expose these objects via the manager. | ||
var options, | ||
controllerConfig; | ||
|
||
//provides access to the current options and configs. | ||
var config = { | ||
setOptions: function(opts) { | ||
//no options were passed | ||
options = opts || {}; | ||
|
||
// Defaults: | ||
var defaultValues = { | ||
config: "dualShock3", | ||
accelerometerSmoothing: true, | ||
analogStickSmoothing: false | ||
}; | ||
var defaults = _.partialRight(_.assign, function(a, b) { | ||
return typeof a == 'undefined' ? b : a; | ||
}); | ||
|
||
defaults(options, defaultValues); | ||
|
||
var controllerConfiguration; | ||
//use passed config or load from built-in configs | ||
if (typeof options.config === "object") { | ||
controllerConfiguration = options.config; | ||
} else { | ||
controllerConfiguration = require('./../controllerConfigurations/' + options.config); | ||
} | ||
|
||
//set the current controllerConfiguration | ||
config.setControllerConfig(controllerConfiguration); | ||
}, | ||
getOptions: function() { | ||
return options; | ||
}, | ||
setControllerConfig: function(config) { | ||
controllerConfig = config; | ||
}, | ||
getControllerConfig: function() { | ||
return controllerConfig; | ||
} | ||
}; | ||
|
||
module.exports = config; |
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
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
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
'use strict'; | ||
|
||
var assert = require('assert'); | ||
|
||
describe('The Config component', function() { | ||
var mockConfig = { | ||
vendorId: 1556, | ||
productId: 616 | ||
}, | ||
mockOptions = { | ||
config: 'dualShock3', | ||
accelerometerSmoothing: true, | ||
logging: false | ||
}, | ||
instance = [{ | ||
name: 'setOptions' | ||
}, { | ||
name: 'getOptions' | ||
}, { | ||
name: 'setControllerConfig' | ||
}, { | ||
name: 'getControllerConfig' | ||
}], | ||
defaultOptionsInstance = [{ | ||
name: 'config' | ||
}, { | ||
name: 'accelerometerSmoothing' | ||
}, { | ||
name: 'analogStickSmoothing' | ||
}], | ||
configA, | ||
configB; | ||
|
||
beforeEach(function() { | ||
configA = require('../lib/config'); | ||
configB = require('../lib/config'); | ||
}); | ||
|
||
|
||
describe('object instance', function() { | ||
it('should have the following shape', function() { | ||
instance.forEach(function(method) { | ||
assert.equal(typeof configA[method.name], 'function'); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('option methods', function() { | ||
it('should be able to save options', function() { | ||
configA.setOptions(mockOptions); | ||
assert.equal(configA.getOptions(), mockOptions); | ||
}); | ||
|
||
it('should provide a single object accross instances', function() { | ||
configA.setOptions(mockOptions); | ||
assert.equal(configA.getOptions(), configB.getOptions()); | ||
}); | ||
}); | ||
|
||
describe('controllerConfig methods', function() { | ||
it('should be able to save controllerConfig settings', function() { | ||
configA.setControllerConfig(mockConfig); | ||
//change the object | ||
mockConfig.vendorId = 22; | ||
assert.equal(configA.getControllerConfig(), mockConfig); | ||
}); | ||
|
||
it('should provide a single object accross instances', function() { | ||
configA.setControllerConfig(mockConfig); | ||
assert.equal(configA.getControllerConfig(), configB.getControllerConfig()); | ||
}); | ||
}); | ||
|
||
describe('default values', function() { | ||
beforeEach(function() { | ||
configA.setOptions(); | ||
}); | ||
it('should apply default values', function() { | ||
var ops = configA.getOptions(); | ||
defaultOptionsInstance.forEach(function(property) { | ||
assert.notEqual(ops[property.name], void 0); | ||
}); | ||
}); | ||
it('should load default config', function() { | ||
var controllerConfig = configA.getControllerConfig(); | ||
assert.notEqual(controllerConfig, null); | ||
assert.notEqual(controllerConfig, void 0); | ||
}); | ||
}); | ||
}); |
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
Oops, something went wrong.