Skip to content

Commit

Permalink
feature: allow to override default values
Browse files Browse the repository at this point in the history
  • Loading branch information
dsalahutdinov committed Dec 19, 2017
1 parent 7560414 commit a26625e
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 18 deletions.
3 changes: 1 addition & 2 deletions anyway_config.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ Gem::Specification.new do |s|
s.summary = "Configuration DSL for Ruby libraries and applications"
s.description = %{
Configuration DSL for Ruby libraries and applications.
Allows you to easily follow the twelve-factor application principles (https://12factor.net/config).
}

Expand All @@ -24,6 +23,6 @@ Gem::Specification.new do |s|
s.required_ruby_version = '>= 2.2'

s.add_development_dependency "rspec", "~> 3.5"
s.add_development_dependency "rubocop", "~> 0.49"
s.add_development_dependency "rubocop", "~> 0.52"
s.add_development_dependency "simplecov", ">= 0.3.8"
end
34 changes: 24 additions & 10 deletions lib/anyway/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,21 +44,35 @@ 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(name)
new(name, false).load_from_sources
new(name: name, load: false).load_from_sources
end
end

attr_reader :config_name

def initialize(config_name = nil, do_load = true)
@config_name = config_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}})
#
# rubocop:disable Metrics/LineLength,Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity
def initialize(config_name = nil, do_load = true, name: nil, load: true, overrides: {})
unless config_name.nil? && do_load.nil?
warn "[Deprecated] Positional arguments for Anyway::Config#initialize will be removed in 1.2.0. Use keyword arguments instead: initialize(name:, load:, overrides:)"
end
name = config_name unless config_name.nil?
load = do_load unless do_load.nil?

@config_name = name || self.class.config_name
raise ArgumentError, "Config name is missing" unless @config_name
load if do_load
self.load(overrides) if load
end
# rubocop:enable Metrics/LineLength,Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity

def reload
def reload(overrides = {})
clear
load
load(overrides)
self
end

Expand All @@ -69,8 +83,10 @@ def clear
self
end

def load
def load(overrides = {})
config = load_from_sources((self.class.defaults || {}).deep_dup)

config.merge!(overrides) unless overrides.nil?
config.each do |key, val|
set_value(key, val)
end
Expand All @@ -86,9 +102,7 @@ def load_from_sources(config = {})
def load_from_file(config)
config_path = Anyway.env.fetch(config_name).delete('conf') ||
"./config/#{config_name}.yml"
if config_path && File.file?(config_path)
config.deep_merge!(parse_yml(config_path) || {})
end
config.deep_merge!(parse_yml(config_path) || {}) if config_path && File.file?(config_path)
config
end

Expand Down
4 changes: 1 addition & 3 deletions lib/anyway/rails/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ def load_from_sources(config = {})

def load_from_file(config)
config_path = Rails.root.join("config", "#{@config_name}.yml")
if File.file? config_path
config.deep_merge!(parse_yml(config_path)[Rails.env] || {})
end
config.deep_merge!(parse_yml(config_path)[Rails.env] || {}) if File.file? config_path
config
end

Expand Down
28 changes: 25 additions & 3 deletions spec/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@
expect(conf.host).to eq "test.host"
end

it "sets overrides after loading YAML" do
config = CoolConfig.new(overrides: { host: 'overrided.host' })
expect(config.host).to eq "overrided.host"
end

if Rails.application.respond_to?(:secrets)
it "load config from secrets" do
expect(conf.user[:name]).to eq "test"
Expand All @@ -79,6 +84,17 @@
ENV['ANYWAY_SECRET_PASSWORD'] = 'my_pass'
expect(conf.user[:password]).to eq 'my_pass'
end

it "overrides loaded value by explicit" do
ENV['ANYWAY_SECRET_PASSWORD'] = 'my_pass'

config = CoolConfig.new(
overrides: {
user: { password: 'explicit_password' }
}
)
expect(config.user[:password]).to eq "explicit_password"
end
end

describe "clear" do
Expand Down Expand Up @@ -118,9 +134,7 @@
data = Anyway::Config.for(:my_app)
expect(data[:test]).to eq 1
expect(data[:name]).to eq 'my_app'
if Rails.application.respond_to?(:secrets)
expect(data[:secret]).to eq 'my_secret'
end
expect(data[:secret]).to eq 'my_secret' if Rails.application.respond_to?(:secrets)
end
end

Expand All @@ -133,6 +147,14 @@
end
end

context "config with initial hash values" do
let(:conf) { SmallConfig.new(overrides: { 'meta': 'dummy' }) }

it "works" do
expect(conf.meta).to eq 'dummy'
end
end

context "when name is missing" do
let(:config) do
Class.new(described_class)
Expand Down

0 comments on commit a26625e

Please sign in to comment.