From 73df017d83df5a41c94601c66271c2efe5562552 Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Fri, 12 Dec 2014 08:59:45 -0700 Subject: [PATCH] =?UTF-8?q?Add=20a=20=E2=80=98sources=E2=80=99=20and=20?= =?UTF-8?q?=E2=80=98excludeSources=E2=80=99=20options=20to=20Config::get?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit If the option is supplied, we will only retrieve values from the specified sources. --- spec/config-spec.coffee | 24 ++++++++++++++++++++++++ src/config.coffee | 12 ++++++------ 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/spec/config-spec.coffee b/spec/config-spec.coffee index 6828cbfcf..3ff75e405 100644 --- a/spec/config-spec.coffee +++ b/spec/config-spec.coffee @@ -34,6 +34,30 @@ describe "Config", -> atom.config.setDefaults("bar", baz: 7) expect(atom.config.get("bar.baz")).toEqual {a: 3} + describe "when a 'sources' option is specified", -> + it "only retrieves values from the specified sources", -> + atom.config.set("x.y", 1, scopeSelector: ".foo", source: "a") + atom.config.set("x.y", 2, scopeSelector: ".foo", source: "b") + atom.config.set("x.y", 3, scopeSelector: ".foo", source: "c") + atom.config.setSchema("x.y", type: "integer", default: 4) + + expect(atom.config.get("x.y", sources: ["a"], scope: [".foo"])).toBe 1 + expect(atom.config.get("x.y", sources: ["b"], scope: [".foo"])).toBe 2 + expect(atom.config.get("x.y", sources: ["c"], scope: [".foo"])).toBe 3 + expect(atom.config.get("x.y", sources: ["x"], scope: [".foo"])).toBe 4 + + describe "when an 'excludeSources' option is specified", -> + it "only retrieves values from the specified sources", -> + atom.config.set("x.y", 1, scopeSelector: ".foo", source: "a") + atom.config.set("x.y", 2, scopeSelector: ".foo", source: "b") + atom.config.set("x.y", 3, scopeSelector: ".foo", source: "c") + atom.config.setSchema("x.y", type: "integer", default: 4) + + expect(atom.config.get("x.y", excludeSources: ["a"], scope: [".foo"])).toBe 3 + expect(atom.config.get("x.y", excludeSources: ["c"], scope: [".foo"])).toBe 2 + expect(atom.config.get("x.y", excludeSources: ["b", "c"], scope: [".foo"])).toBe 1 + expect(atom.config.get("x.y", excludeSources: ["b", "c", "a"], scope: [".foo"])).toBe 4 + describe ".set(keyPath, value)", -> it "allows a key path's value to be written", -> expect(atom.config.set("foo.bar.baz", 42)).toBe true diff --git a/src/config.coffee b/src/config.coffee index f0efbf87c..bffd06110 100644 --- a/src/config.coffee +++ b/src/config.coffee @@ -472,10 +472,10 @@ class Config keyPath = arguments[0] if scope? - value = @getRawScopedValue(scope, keyPath) - value ? @getRawValue(keyPath) + value = @getRawScopedValue(scope, keyPath, options) + value ? @getRawValue(keyPath, options) else - @getRawValue(keyPath) + @getRawValue(keyPath, options) # Essential: Sets the value for a configuration setting. # @@ -806,7 +806,7 @@ class Config catch e console.warn("'#{keyPath}' could not be set. Attempted value: #{JSON.stringify(value)}; Schema: #{JSON.stringify(@getSchema(keyPath))}") - getRawValue: (keyPath) -> + getRawValue: (keyPath, options) -> value = _.valueForKeyPath(@settings, keyPath) defaultValue = _.valueForKeyPath(@defaultSettings, keyPath) @@ -930,9 +930,9 @@ class Config @usersScopedSettings.add @scopedSettingsStore.addProperties(source, settingsBySelector, @usersScopedSettingPriority) @emitter.emit 'did-change' - getRawScopedValue: (scopeDescriptor, keyPath) -> + getRawScopedValue: (scopeDescriptor, keyPath, options) -> scopeDescriptor = ScopeDescriptor.fromObject(scopeDescriptor) - @scopedSettingsStore.getPropertyValue(scopeDescriptor.getScopeChain(), keyPath) + @scopedSettingsStore.getPropertyValue(scopeDescriptor.getScopeChain(), keyPath, options) observeScopedKeyPath: (scope, keyPath, callback) -> oldValue = @get(keyPath, {scope})