From 70191ea368f866bd52e8253214d2bbf8a302f573 Mon Sep 17 00:00:00 2001 From: probablycorey Date: Thu, 2 May 2013 11:35:13 -0700 Subject: [PATCH] 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 --- spec/app/config-spec.coffee | 20 ++++++++++++++++++-- src/app/config.coffee | 6 ++++-- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/spec/app/config-spec.coffee b/spec/app/config-spec.coffee index f09fa913b..ec8a81d52 100644 --- a/spec/app/config-spec.coffee +++ b/spec/app/config-spec.coffee @@ -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) diff --git a/src/app/config.coffee b/src/app/config.coffee index 8209307cf..a406a9ea2 100644 --- a/src/app/config.coffee +++ b/src/app/config.coffee @@ -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) ->