From 388ff62fbd85e18ea7702e7b6af23c0cb011359e Mon Sep 17 00:00:00 2001 From: Tiago Fernandes Date: Wed, 4 May 2016 17:52:12 +0100 Subject: [PATCH 1/3] Added config.prepend_sources option to Config.setup --- config.gemspec | 2 +- lib/config.rb | 6 ++++-- spec/config_spec.rb | 17 +++++++++++++++++ spec/spec_helper.rb | 1 + 4 files changed, 23 insertions(+), 3 deletions(-) diff --git a/config.gemspec b/config.gemspec index 40f5e756..868f06b6 100644 --- a/config.gemspec +++ b/config.gemspec @@ -27,7 +27,7 @@ Gem::Specification.new do |s| s.add_dependency "activesupport", ">= 3.0" s.add_dependency "deep_merge", "~> 1.0.1" - s.add_development_dependency "bundler", "~> 1.11.2" + s.add_development_dependency "bundler", ">= 1.11.2" s.add_development_dependency "rake" s.add_development_dependency "rdoc" s.add_development_dependency "pry" diff --git a/lib/config.rb b/lib/config.rb index faeab004..86e32d39 100644 --- a/lib/config.rb +++ b/lib/config.rb @@ -12,8 +12,10 @@ module Config @@_ran_once = false mattr_accessor :const_name, :use_env + mattr_accessor :prepend_sources, :_ran_once @@const_name = "Settings" @@use_env = false + @@prepend_sources = [] # deep_merge options mattr_accessor :knockout_prefix @@ -46,7 +48,7 @@ def self.load_and_set_settings(*files) end def self.setting_files(config_root, env) - [ + (@@prepend_sources + [ File.join(config_root, "settings.yml").to_s, File.join(config_root, "settings", "#{env}.yml").to_s, File.join(config_root, "environments", "#{env}.yml").to_s, @@ -54,7 +56,7 @@ def self.setting_files(config_root, env) File.join(config_root, "settings.local.yml").to_s, File.join(config_root, "settings", "#{env}.local.yml").to_s, File.join(config_root, "environments", "#{env}.local.yml").to_s - ].freeze + ]).freeze end def self.reload! diff --git a/spec/config_spec.rb b/spec/config_spec.rb index 8d5fd3bb..32329c67 100644 --- a/spec/config_spec.rb +++ b/spec/config_spec.rb @@ -190,6 +190,11 @@ end context "Custom Configuration" do + + before :each do + Config._ran_once=false + end + it "should have the default settings constant as 'Settings'" do expect(Config.const_name).to eq("Settings") end @@ -199,6 +204,18 @@ expect(Config.const_name).to eq("Settings2") end + + it "should have prepend_sources empty" do + expect(Config.prepend_sources).to eq([]) + end + + it "should be able to assign prepend_sources" do + Config.setup { |config| config.prepend_sources = ["file.yml"] } + + expect(Config.prepend_sources).to eq(["file.yml"]) + expect(Config.setting_files("./","test")[0]).to eq("file.yml") + end + end context "Settings with a type value of 'hash'" do diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index ee5e69d1..282c3510 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -30,6 +30,7 @@ # Load Rspec # require 'rspec/rails' +require 'pry' # Configure RSpec.configure do |config| From ffd84e8e56d9601203ebcc49ebd50e543d73206f Mon Sep 17 00:00:00 2001 From: Tiago Fernandes Date: Wed, 4 May 2016 17:52:12 +0100 Subject: [PATCH 2/3] Added config.prepend_sources option to Config.setup --- README.md | 5 +++++ config.gemspec | 2 +- lib/config.rb | 6 ++++-- spec/config_spec.rb | 17 +++++++++++++++++ spec/spec_helper.rb | 1 + 5 files changed, 28 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 326d88ea..7ce8bbab 100644 --- a/README.md +++ b/README.md @@ -258,6 +258,7 @@ You can customize `Config` only once, preferably during application initializati Config.setup do |config| config.const_name = 'Settings' config.knockout_prefix = nil + config.prepend_sources = ["path_to_some_custom_settings_to_prepend.yml"] end ``` @@ -271,6 +272,10 @@ Inheritance customization (check [Deep Merge](https://github.com/danielsdeleo/de * `knockout_prefix` - ability to remove elements of the array set in earlier loaded settings file. Default: `nil` +Prepend sources + +* `prepend_sources` - ability to prepend settings via Config without the need to reload settings after they run once. + ## Working with Heroku Heroku uses ENV object to store sensitive settings which are like the local files described above. You cannot upload diff --git a/config.gemspec b/config.gemspec index 40f5e756..868f06b6 100644 --- a/config.gemspec +++ b/config.gemspec @@ -27,7 +27,7 @@ Gem::Specification.new do |s| s.add_dependency "activesupport", ">= 3.0" s.add_dependency "deep_merge", "~> 1.0.1" - s.add_development_dependency "bundler", "~> 1.11.2" + s.add_development_dependency "bundler", ">= 1.11.2" s.add_development_dependency "rake" s.add_development_dependency "rdoc" s.add_development_dependency "pry" diff --git a/lib/config.rb b/lib/config.rb index faeab004..86e32d39 100644 --- a/lib/config.rb +++ b/lib/config.rb @@ -12,8 +12,10 @@ module Config @@_ran_once = false mattr_accessor :const_name, :use_env + mattr_accessor :prepend_sources, :_ran_once @@const_name = "Settings" @@use_env = false + @@prepend_sources = [] # deep_merge options mattr_accessor :knockout_prefix @@ -46,7 +48,7 @@ def self.load_and_set_settings(*files) end def self.setting_files(config_root, env) - [ + (@@prepend_sources + [ File.join(config_root, "settings.yml").to_s, File.join(config_root, "settings", "#{env}.yml").to_s, File.join(config_root, "environments", "#{env}.yml").to_s, @@ -54,7 +56,7 @@ def self.setting_files(config_root, env) File.join(config_root, "settings.local.yml").to_s, File.join(config_root, "settings", "#{env}.local.yml").to_s, File.join(config_root, "environments", "#{env}.local.yml").to_s - ].freeze + ]).freeze end def self.reload! diff --git a/spec/config_spec.rb b/spec/config_spec.rb index 8d5fd3bb..32329c67 100644 --- a/spec/config_spec.rb +++ b/spec/config_spec.rb @@ -190,6 +190,11 @@ end context "Custom Configuration" do + + before :each do + Config._ran_once=false + end + it "should have the default settings constant as 'Settings'" do expect(Config.const_name).to eq("Settings") end @@ -199,6 +204,18 @@ expect(Config.const_name).to eq("Settings2") end + + it "should have prepend_sources empty" do + expect(Config.prepend_sources).to eq([]) + end + + it "should be able to assign prepend_sources" do + Config.setup { |config| config.prepend_sources = ["file.yml"] } + + expect(Config.prepend_sources).to eq(["file.yml"]) + expect(Config.setting_files("./","test")[0]).to eq("file.yml") + end + end context "Settings with a type value of 'hash'" do diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index ee5e69d1..282c3510 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -30,6 +30,7 @@ # Load Rspec # require 'rspec/rails' +require 'pry' # Configure RSpec.configure do |config| From 0ae8868da88f80dba7e4060fc3a9136e878f3bf2 Mon Sep 17 00:00:00 2001 From: Tiago Fernandes Date: Fri, 13 May 2016 09:06:07 +0100 Subject: [PATCH 3/3] README update for prepend_sources config option --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index 326d88ea..7ce8bbab 100644 --- a/README.md +++ b/README.md @@ -258,6 +258,7 @@ You can customize `Config` only once, preferably during application initializati Config.setup do |config| config.const_name = 'Settings' config.knockout_prefix = nil + config.prepend_sources = ["path_to_some_custom_settings_to_prepend.yml"] end ``` @@ -271,6 +272,10 @@ Inheritance customization (check [Deep Merge](https://github.com/danielsdeleo/de * `knockout_prefix` - ability to remove elements of the array set in earlier loaded settings file. Default: `nil` +Prepend sources + +* `prepend_sources` - ability to prepend settings via Config without the need to reload settings after they run once. + ## Working with Heroku Heroku uses ENV object to store sensitive settings which are like the local files described above. You cannot upload