From fd7e30429e936d8fe0ddc75b44be55ac167818c3 Mon Sep 17 00:00:00 2001 From: Nuzair46 Date: Thu, 24 Oct 2024 18:47:25 +0900 Subject: [PATCH 1/4] Added extra sources --- README.md | 10 ++++++++++ lib/config.rb | 5 ++++- lib/generators/config/templates/config.rb | 5 +++++ spec/config_spec.rb | 18 ++++++++++++++++++ spec/spec_helper.rb | 1 + 5 files changed, 38 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index d979634c..de11685f 100644 --- a/README.md +++ b/README.md @@ -154,6 +154,16 @@ Example production environment config file: #{Rails.root}/config/environments/production.yml ``` +### Extra sources + +You can load extra sources from the config folder during initialization by setting the `extra_sources` configuration option. + +```ruby +Config.setup do |config| + config.extra_sources = ['extra_settings'] +end +``` + ### Developer specific config files If you want to have local settings, specific to your machine or development environment, you can use the following files, which are automatically `.gitignore` : diff --git a/lib/config.rb b/lib/config.rb index e98d1e70..a8de23aa 100644 --- a/lib/config.rb +++ b/lib/config.rb @@ -29,7 +29,8 @@ module Config merge_hash_arrays: false, validation_contract: nil, evaluate_erb_in_yaml: true, - environment: nil + environment: nil, + extra_sources: [] ) def self.setup @@ -63,6 +64,7 @@ def self.load_and_set_settings(*sources) def self.setting_files(config_root, env) [ File.join(config_root, "#{Config.file_name}.yml").to_s, + *Config.extra_sources.map { |source| File.join(config_root, "#{source}.yml") }, File.join(config_root, Config.dir_name, "#{env}.yml").to_s, File.join(config_root, 'environments', "#{env}.yml").to_s, *local_setting_files(config_root, env) @@ -72,6 +74,7 @@ def self.setting_files(config_root, env) def self.local_setting_files(config_root, env) [ (File.join(config_root, "#{Config.file_name}.local.yml").to_s if env != 'test'), + *Config.extra_sources.map { |source| File.join(config_root, "#{source}.local.yml") }, File.join(config_root, Config.dir_name, "#{env}.local.yml").to_s, File.join(config_root, 'environments', "#{env}.local.yml").to_s ].compact diff --git a/lib/generators/config/templates/config.rb b/lib/generators/config/templates/config.rb index f6a222d0..02f0de9b 100644 --- a/lib/generators/config/templates/config.rb +++ b/lib/generators/config/templates/config.rb @@ -65,4 +65,9 @@ # # config.file_name = 'settings' # config.dir_name = 'settings' + + # Extra sources to load + # Loads extra sources from config folder + # + # config.extra_sources = ['extra_settings'] end diff --git a/spec/config_spec.rb b/spec/config_spec.rb index 505c6081..771c6061 100644 --- a/spec/config_spec.rb +++ b/spec/config_spec.rb @@ -38,6 +38,24 @@ expect(config.root['google.com']).to eq(3) end + it "should get load extra_sources files" do + Config.setup do |config| + config.extra_sources = ['settings2'] + end + + config = Config.setting_files("root/config", "staging") + expect(config).to eq([ + 'root/config/settings.yml', + 'root/config/settings2.yml', + 'root/config/settings/staging.yml', + 'root/config/environments/staging.yml', + 'root/config/settings.local.yml', + 'root/config/settings2.local.yml', + 'root/config/settings/staging.local.yml', + 'root/config/environments/staging.local.yml' + ]) + end + it "should load 2 basic config files" do config = Config.load_files("#{fixture_path}/settings.yml", "#{fixture_path}/settings2.yml") expect(config.size).to eq(1) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index ca0739e7..554223b2 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -87,6 +87,7 @@ def self.reset self.fail_on_missing = false self.file_name = 'settings' self.dir_name = 'settings' + self.extra_sources = [] instance_variable_set(:@_ran_once, false) end end From cfd5a41b6aaf243f1aca05dbdae799330ada24ef Mon Sep 17 00:00:00 2001 From: Nuzair46 Date: Thu, 24 Oct 2024 18:55:36 +0900 Subject: [PATCH 2/4] add more info in readme --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index de11685f..4e5c2714 100644 --- a/README.md +++ b/README.md @@ -164,6 +164,8 @@ Config.setup do |config| end ``` +This will also overwrite the same config entries from the main file. + ### Developer specific config files If you want to have local settings, specific to your machine or development environment, you can use the following files, which are automatically `.gitignore` : From b0f45ff24e914e607b767538b59439d7e4850a6a Mon Sep 17 00:00:00 2001 From: Nuzair46 Date: Thu, 24 Oct 2024 18:56:16 +0900 Subject: [PATCH 3/4] fix typo --- spec/config_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/config_spec.rb b/spec/config_spec.rb index 771c6061..95517d7c 100644 --- a/spec/config_spec.rb +++ b/spec/config_spec.rb @@ -38,7 +38,7 @@ expect(config.root['google.com']).to eq(3) end - it "should get load extra_sources files" do + it "should load extra_sources files" do Config.setup do |config| config.extra_sources = ['settings2'] end From 7f97332fd6e33b16dd496d5b3ca52e7a591489ed Mon Sep 17 00:00:00 2001 From: Nuzair46 Date: Thu, 24 Oct 2024 18:57:09 +0900 Subject: [PATCH 4/4] fix template comment --- lib/generators/config/templates/config.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/generators/config/templates/config.rb b/lib/generators/config/templates/config.rb index 02f0de9b..9614f267 100644 --- a/lib/generators/config/templates/config.rb +++ b/lib/generators/config/templates/config.rb @@ -66,8 +66,7 @@ # config.file_name = 'settings' # config.dir_name = 'settings' - # Extra sources to load - # Loads extra sources from config folder + # Load extra sources from config folder # # config.extra_sources = ['extra_settings'] end