From edabf1075d28aa244d1a2d687715cf78e48e9c0f Mon Sep 17 00:00:00 2001 From: Salahutdinov Dmitry Date: Tue, 19 Dec 2017 20:24:21 +0500 Subject: [PATCH] chore: fixes after review --- lib/anyway/config.rb | 24 +++++++++++++++--------- spec/config_spec.rb | 8 ++++---- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/lib/anyway/config.rb b/lib/anyway/config.rb index 2a740fd..d76a346 100644 --- a/lib/anyway/config.rb +++ b/lib/anyway/config.rb @@ -43,22 +43,28 @@ def config_name(val = nil) # # my_config = Anyway::Config.for(:my_app) # # will load data from config/my_app.yml, secrets.my_app, ENV["MY_APP_*"] - def for(explicit_values: {}, name: nil) - new(explicit_values: explicit_values, name: name, load: false).load_from_sources + def for(name) + new(name, false).load_from_sources end end attr_reader :config_name - def initialize(explicit_values: {}, name: nil, load: true) - @config_name = name || self.class.config_name + # Instantiate config with specified name, loads the data and applies overrides + # + # Example: + # + # my_config = Anyway::Config.new(:my_app, true, {overrides: {some: :value}}) + # + def initialize(config_name = nil, do_load = true, overrides: {}) + @config_name = config_name || self.class.config_name raise ArgumentError, "Config name is missing" unless @config_name - load(explicit_values: explicit_values) if load + load(overrides) if do_load end - def reload(explicit_values: {}) + def reload(overrides = {}) clear - load(explicit_values: explicit_values) + load(overrides) self end @@ -69,10 +75,10 @@ def clear self end - def load(explicit_values: {}) + def load(overrides = {}) config = load_from_sources((self.class.defaults || {}).deep_dup) - config.merge!(explicit_values) unless explicit_values.nil? + config.merge!(overrides) unless overrides.nil? config.each do |key, val| set_value(key, val) end diff --git a/spec/config_spec.rb b/spec/config_spec.rb index e4426e2..3778847 100644 --- a/spec/config_spec.rb +++ b/spec/config_spec.rb @@ -55,7 +55,7 @@ end it "sets overrides after loading YAML" do - config = CoolConfig.new(explicit_values: { host: 'overrided.host' }) + config = CoolConfig.new(overrides: { host: 'overrided.host' }) expect(config.host).to eq "overrided.host" end @@ -89,7 +89,7 @@ ENV['ANYWAY_SECRET_PASSWORD'] = 'my_pass' config = CoolConfig.new( - explicit_values: { + overrides: { user: { password: 'explicit_password' } } ) @@ -131,7 +131,7 @@ ENV['MYAPP_TEST'] = '1' ENV['MYAPP_NAME'] = 'my_app' Anyway.env.clear - data = Anyway::Config.for(name: :my_app) + data = Anyway::Config.for(:my_app) expect(data[:test]).to eq 1 expect(data[:name]).to eq 'my_app' expect(data[:secret]).to eq 'my_secret' if Rails.application.respond_to?(:secrets) @@ -148,7 +148,7 @@ end context "config with initial hash values" do - let(:conf) { SmallConfig.new(explicit_values: { 'meta': 'dummy' }) } + let(:conf) { SmallConfig.new(overrides: { 'meta': 'dummy' }) } it "works" do expect(conf.meta).to eq 'dummy'