FEATURE: allow for additional plugin defined global settings

This commit is contained in:
Sam 2014-01-02 10:46:09 +11:00
parent 6a0072d36e
commit 2ad190b9f6
2 changed files with 29 additions and 4 deletions

View File

@ -1,10 +1,17 @@
class GlobalSetting class GlobalSetting
def self.register(key, default)
define_singleton_method(key) do
provider.lookup(key, default)
end
end
def self.load_defaults def self.load_defaults
default_provider = FileProvider.from(File.expand_path('../../../config/discourse_defaults.conf', __FILE__)) default_provider = FileProvider.from(File.expand_path('../../../config/discourse_defaults.conf', __FILE__))
default_provider.data.each do |name, default| default_provider.keys.concat(@provider.keys).uniq.each do |key|
define_singleton_method(name) do default = default_provider.lookup(key, nil)
provider.lookup(name, default) define_singleton_method(key) do
provider.lookup(key, default)
end end
end end
end end
@ -56,6 +63,10 @@ class GlobalSetting
resolve(var, var.nil? ? default : "") resolve(var, var.nil? ? default : "")
end end
def keys
@data.keys
end
private private
def self.parse(file) def self.parse(file)
@ -70,6 +81,10 @@ class GlobalSetting
var = ENV["DISCOURSE_" << key.to_s.upcase] var = ENV["DISCOURSE_" << key.to_s.upcase]
resolve(var , var.nil? ? default : nil) resolve(var , var.nil? ? default : nil)
end end
def keys
ENV.keys.select{|k| k =~ /^DISCOURSE_/}.map{|k| k[10..-1].downcase.to_sym}
end
end end
@ -78,8 +93,9 @@ class GlobalSetting
end end
load_defaults
@provider = @provider =
FileProvider.from(File.expand_path('../../../config/discourse.conf', __FILE__)) || FileProvider.from(File.expand_path('../../../config/discourse.conf', __FILE__)) ||
EnvProvider.new EnvProvider.new
load_defaults
end end

View File

@ -1,6 +1,12 @@
require 'spec_helper' require 'spec_helper'
require 'tempfile' require 'tempfile'
describe GlobalSetting::EnvProvider do
it "can detect keys from env" do
ENV['DISCOURSE_BLA'] = '1'
GlobalSetting::EnvProvider.new.keys.should include(:bla)
end
end
describe GlobalSetting::FileProvider do describe GlobalSetting::FileProvider do
it "can parse a simple file" do it "can parse a simple file" do
f = Tempfile.new('foo') f = Tempfile.new('foo')
@ -20,6 +26,9 @@ describe GlobalSetting::FileProvider do
provider.lookup(:d,"bob").should == nil provider.lookup(:d,"bob").should == nil
provider.lookup(:e,"bob").should == "bob" provider.lookup(:e,"bob").should == "bob"
provider.keys.sort.should == [:a, :b, :c, :d]
f.unlink f.unlink
end end
end end