Skip to content

Commit

Permalink
chore: fixes after review
Browse files Browse the repository at this point in the history
  • Loading branch information
dsalahutdinov committed Dec 19, 2017
1 parent 06fd233 commit edabf10
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
24 changes: 15 additions & 9 deletions lib/anyway/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions spec/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -89,7 +89,7 @@
ENV['ANYWAY_SECRET_PASSWORD'] = 'my_pass'

config = CoolConfig.new(
explicit_values: {
overrides: {
user: { password: 'explicit_password' }
}
)
Expand Down Expand Up @@ -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)
Expand All @@ -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'
Expand Down

0 comments on commit edabf10

Please sign in to comment.