Don't store config values that equal their default config value

If 'foo' has not been set and has a default value of 1, 
config.set('foo', 1) will not store the 1.

If 'foo' has been set to X and has a default value of 1, 
config.set('foo', 1) will remove 'foo' from config
This commit is contained in:
probablycorey
2013-05-02 11:35:13 -07:00
parent d294bf4d05
commit 70191ea368
2 changed files with 22 additions and 4 deletions

View File

@@ -1,12 +1,17 @@
fsUtils = require 'fs-utils'
describe "Config", ->
describe ".get(keyPath) and .set(keyPath, value)", ->
it "allows a key path's value to be read and written", ->
describe ".get(keyPath)", ->
it "allows a key path's value to be read", ->
expect(config.set("foo.bar.baz", 42)).toBe 42
expect(config.get("foo.bar.baz")).toBe 42
expect(config.get("bogus.key.path")).toBeUndefined()
describe ".set(keyPath, value)", ->
it "allows a key path's value to be written", ->
expect(config.set("foo.bar.baz", 42)).toBe 42
expect(config.get("foo.bar.baz")).toBe 42
it "updates observers and saves when a key path is set", ->
observeHandler = jasmine.createSpy "observeHandler"
config.observe "foo.bar.baz", observeHandler
@@ -17,6 +22,17 @@ describe "Config", ->
expect(config.save).toHaveBeenCalled()
expect(observeHandler).toHaveBeenCalledWith 42
describe "when the value equals the default value", ->
it "does not store the value", ->
config.setDefaults("foo", same: 1, changes: 1)
expect(config.settings.foo).toBeUndefined()
config.set('foo.same', 1)
config.set('foo.changes', 2)
expect(config.settings.foo).toEqual {changes: 2}
config.set('foo.changes', 1)
expect(config.settings.foo).toEqual {}
describe ".getPositiveInt(keyPath, defaultValue)", ->
it "returns the proper current or default value", ->
config.set('editor.preferredLineLength', 0)

View File

@@ -127,8 +127,10 @@ class Config
#
# Returns the `value`.
set: (keyPath, value) ->
_.setValueForKeyPath(@settings, keyPath, value)
@update()
if @get(keyPath) != value
value = undefined if _.valueForKeyPath(@defaultSettings, keyPath) == value
_.setValueForKeyPath(@settings, keyPath, value)
@update()
value
setDefaults: (keyPath, defaults) ->