Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,22 @@ Example production environment config file:
#{Rails.root}/config/environments/production.yml
```

### Extra sources

You can load extra sources during initialization by setting the `extra_sources` configuration option.

```ruby
Config.setup do |config|
config.extra_sources = [
'path/to/extra_source.yml', # String: loads extra_source.yml
{ api_key: ENV['API_KEY'] }, # Hash: direct hash source
MyCustomSource.new, # Object: custom source object that responds to `load`
]
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` :
Expand Down
8 changes: 6 additions & 2 deletions lib/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -57,7 +58,10 @@ def self.load_files(*sources)
def self.load_and_set_settings(*sources)
name = Config.const_name
Object.send(:remove_const, name) if Object.const_defined?(name)
Object.const_set(name, Config.load_files(sources))

# Include extra sources in the loading process
all_sources = [sources, Config.extra_sources].flatten.compact
Object.const_set(name, Config.load_files(*all_sources))
end

def self.setting_files(config_root, env)
Expand Down
9 changes: 9 additions & 0 deletions lib/generators/config/templates/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,13 @@
#
# config.file_name = 'settings'
# config.dir_name = 'settings'

# Load extra sources from a path. These can be file paths (strings),
# hashes, or custom source objects that respond to 'load'
#
# config.extra_sources = [
# 'path/to/extra_source.yml', # String: loads extra_source.yml
# { api_key: ENV['API_KEY'] }, # Hash: direct hash source
# MyCustomSource.new, # Custom source object
# ]
end
6 changes: 5 additions & 1 deletion spec/config_env_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@

context 'when overriding settings via ENV variables is enabled' do
let(:config) do
Config.load_files "#{fixture_path}/settings.yml", "#{fixture_path}/multilevel.yml"
config_instance = Config.load_files "#{fixture_path}/settings.yml", "#{fixture_path}/multilevel.yml"
# Ensure the Settings constant is set to the same instance for Config.reload! to work
Object.send(:remove_const, 'Settings') if Object.const_defined?('Settings')
Object.const_set('Settings', config_instance)
config_instance
end

after :all do
Expand Down
24 changes: 24 additions & 0 deletions spec/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,30 @@
expect(config.root['google.com']).to eq(3)
end

it "should load extra_sources and support different types" do
test_hash = { 'extra_key' => 'extra_value' }
object_source = double 'source'

allow(object_source).to receive(:load) do
{ 'server' => 'google.com' }
end

Config.setup do |config|
config.extra_sources = [
"#{fixture_path}/settings2.yml",
test_hash,
object_source
]
end

Config.load_and_set_settings("#{fixture_path}/settings.yml")

expect(Settings.size).to eq(1)
expect(Settings.extra_key).to eq('extra_value')
expect(Settings.another).to eq("something")
expect(Settings.server).to eq('google.com')
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)
Expand Down
13 changes: 13 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,18 @@ def fixture_path

# Extend Config module with ability to reset configuration to the default values
def self.reset
# Clear any existing Settings constant and its sources to prevent mock leakage
current_const_name = self.const_name
if Object.const_defined?(current_const_name)
settings_instance = Object.const_get(current_const_name)
# Clear the config sources to prevent mock doubles from leaking
if settings_instance.respond_to?(:instance_variable_set)
settings_instance.instance_variable_set(:@config_sources, [])
end
Object.send(:remove_const, current_const_name)
end

# Reset configuration to defaults
self.const_name = 'Settings'
self.use_env = false
self.knockout_prefix = nil
Expand All @@ -93,6 +105,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
Expand Down