Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added extra sources in initializer #366

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,18 @@ Example production environment config file:
#{Rails.root}/config/environments/production.yml
```

### Extra sources

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

```ruby
Config.setup do |config|
config.extra_sources = ['extra_settings']
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi,

It might be interesting to allow it to be any "source-like" implementation, rather than just a filename within the config root.

config.add_source! will then take care of either attaching it as a file source, or as a custom source.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

got it. will make the changes

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
5 changes: 4 additions & 1 deletion 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 @@ -63,6 +64,7 @@ def self.load_and_set_settings(*sources)
def self.setting_files(config_root, env)
[
File.join(config_root, "#{Config.file_name}.yml").to_s,
*Config.extra_sources.map { |source| File.join(config_root, "#{source}.yml") },
File.join(config_root, Config.dir_name, "#{env}.yml").to_s,
File.join(config_root, 'environments', "#{env}.yml").to_s,
*local_setting_files(config_root, env)
Expand All @@ -72,6 +74,7 @@ def self.setting_files(config_root, env)
def self.local_setting_files(config_root, env)
[
(File.join(config_root, "#{Config.file_name}.local.yml").to_s if env != 'test'),
*Config.extra_sources.map { |source| File.join(config_root, "#{source}.local.yml") },
File.join(config_root, Config.dir_name, "#{env}.local.yml").to_s,
File.join(config_root, 'environments', "#{env}.local.yml").to_s
].compact
Expand Down
4 changes: 4 additions & 0 deletions lib/generators/config/templates/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,8 @@
#
# config.file_name = 'settings'
# config.dir_name = 'settings'

# Load extra sources from config folder
#
# config.extra_sources = ['extra_settings']
end
18 changes: 18 additions & 0 deletions spec/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,24 @@
expect(config.root['google.com']).to eq(3)
end

it "should load extra_sources files" do
Config.setup do |config|
config.extra_sources = ['settings2']
end

config = Config.setting_files("root/config", "staging")
expect(config).to eq([
'root/config/settings.yml',
'root/config/settings2.yml',
'root/config/settings/staging.yml',
'root/config/environments/staging.yml',
'root/config/settings.local.yml',
'root/config/settings2.local.yml',
'root/config/settings/staging.local.yml',
'root/config/environments/staging.local.yml'
])
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
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,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