config 0.0.1 → 1.0.0.beta1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.codeclimate.yml +6 -0
- data/.gitignore +14 -0
- data/.rspec +2 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/.travis.yml +23 -0
- data/Appraisals +15 -0
- data/CHANGELOG.md +25 -0
- data/Gemfile +6 -0
- data/LICENSE.md +27 -0
- data/README.md +315 -0
- data/Rakefile +42 -0
- data/config.gemspec +46 -0
- data/gemfiles/rails_3.gemfile +7 -0
- data/gemfiles/rails_4.1.gemfile +7 -0
- data/gemfiles/rails_4.2.gemfile +7 -0
- data/gemfiles/rails_4.gemfile +7 -0
- data/lib/config.rb +64 -0
- data/lib/config/engine.rb +5 -0
- data/lib/config/integration/rails.rb +36 -0
- data/lib/config/integration/sinatra.rb +26 -0
- data/lib/config/options.rb +157 -0
- data/lib/config/rack/reloader.rb +15 -0
- data/lib/config/railtie.rb +9 -0
- data/lib/config/sources/yaml_source.rb +26 -0
- data/lib/config/tasks.rb +59 -0
- data/lib/config/tasks/heroku.rake +8 -0
- data/lib/config/version.rb +3 -0
- data/lib/generators/config/install_generator.rb +32 -0
- data/lib/generators/config/templates/rails_config.rb +3 -0
- data/lib/generators/config/templates/settings.local.yml +0 -0
- data/lib/generators/config/templates/settings.yml +0 -0
- data/lib/generators/config/templates/settings/development.yml +0 -0
- data/lib/generators/config/templates/settings/production.yml +0 -0
- data/lib/generators/config/templates/settings/test.yml +0 -0
- data/spec/app/rails_3/Rakefile +7 -0
- data/spec/app/rails_3/app/assets/javascripts/application.js +13 -0
- data/spec/app/rails_3/app/assets/stylesheets/application.css +13 -0
- data/spec/app/rails_3/app/controllers/application_controller.rb +3 -0
- data/spec/app/rails_3/app/helpers/application_helper.rb +2 -0
- data/spec/app/rails_3/app/models/.gitkeep +0 -0
- data/spec/app/rails_3/app/views/layouts/application.html.erb +14 -0
- data/spec/app/rails_3/bin/bundle +3 -0
- data/spec/app/rails_3/bin/rails +4 -0
- data/spec/app/rails_3/bin/rake +4 -0
- data/spec/app/rails_3/config.ru +4 -0
- data/spec/app/rails_3/config/application.rb +29 -0
- data/spec/app/rails_3/config/boot.rb +5 -0
- data/spec/app/rails_3/config/database.yml +25 -0
- data/spec/app/rails_3/config/environment.rb +5 -0
- data/spec/app/rails_3/config/environments/development.rb +42 -0
- data/spec/app/rails_3/config/environments/production.rb +72 -0
- data/spec/app/rails_3/config/environments/test.rb +42 -0
- data/spec/app/rails_3/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/app/rails_3/config/initializers/inflections.rb +15 -0
- data/spec/app/rails_3/config/initializers/mime_types.rb +5 -0
- data/spec/app/rails_3/config/initializers/secret_token.rb +7 -0
- data/spec/app/rails_3/config/initializers/session_store.rb +8 -0
- data/spec/app/rails_3/config/initializers/wrap_parameters.rb +14 -0
- data/spec/app/rails_3/config/locales/en.yml +5 -0
- data/spec/app/rails_3/config/routes.rb +2 -0
- data/spec/app/rails_3/config/settings.yml +7 -0
- data/spec/app/rails_3/db/.gitkeep +0 -0
- data/spec/app/rails_3/public/404.html +26 -0
- data/spec/app/rails_3/public/422.html +26 -0
- data/spec/app/rails_3/public/500.html +25 -0
- data/spec/app/rails_3/script/rails +6 -0
- data/spec/app/rails_4.1/Rakefile +6 -0
- data/spec/app/rails_4.1/app/assets/javascripts/application.js +13 -0
- data/spec/app/rails_4.1/app/assets/stylesheets/application.css +15 -0
- data/spec/app/rails_4.1/app/controllers/application_controller.rb +5 -0
- data/spec/app/rails_4.1/app/helpers/application_helper.rb +2 -0
- data/spec/app/rails_4.1/app/models/.keep +0 -0
- data/spec/app/rails_4.1/app/views/layouts/application.html.erb +14 -0
- data/spec/app/rails_4.1/bin/bundle +3 -0
- data/spec/app/rails_4.1/bin/rails +4 -0
- data/spec/app/rails_4.1/bin/rake +4 -0
- data/spec/app/rails_4.1/config.ru +4 -0
- data/spec/app/rails_4.1/config/application.rb +27 -0
- data/spec/app/rails_4.1/config/boot.rb +5 -0
- data/spec/app/rails_4.1/config/database.yml +25 -0
- data/spec/app/rails_4.1/config/environment.rb +5 -0
- data/spec/app/rails_4.1/config/environments/development.rb +42 -0
- data/spec/app/rails_4.1/config/environments/production.rb +88 -0
- data/spec/app/rails_4.1/config/environments/test.rb +44 -0
- data/spec/app/rails_4.1/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/app/rails_4.1/config/initializers/cookies_serializer.rb +3 -0
- data/spec/app/rails_4.1/config/initializers/filter_parameter_logging.rb +4 -0
- data/spec/app/rails_4.1/config/initializers/inflections.rb +16 -0
- data/spec/app/rails_4.1/config/initializers/mime_types.rb +4 -0
- data/spec/app/rails_4.1/config/initializers/session_store.rb +3 -0
- data/spec/app/rails_4.1/config/initializers/wrap_parameters.rb +14 -0
- data/spec/app/rails_4.1/config/locales/en.yml +23 -0
- data/spec/app/rails_4.1/config/routes.rb +2 -0
- data/spec/app/rails_4.1/config/secrets.yml +22 -0
- data/spec/app/rails_4.1/config/settings.yml +7 -0
- data/spec/app/rails_4.1/db/.keep +0 -0
- data/spec/app/rails_4.1/public/404.html +67 -0
- data/spec/app/rails_4.1/public/422.html +67 -0
- data/spec/app/rails_4.1/public/500.html +66 -0
- data/spec/app/rails_4.2/Rakefile +6 -0
- data/spec/app/rails_4.2/app/assets/images/.keep +0 -0
- data/spec/app/rails_4.2/app/assets/javascripts/application.js +16 -0
- data/spec/app/rails_4.2/app/assets/stylesheets/application.css +15 -0
- data/spec/app/rails_4.2/app/controllers/application_controller.rb +5 -0
- data/spec/app/rails_4.2/app/controllers/concerns/.keep +0 -0
- data/spec/app/rails_4.2/app/helpers/application_helper.rb +2 -0
- data/spec/app/rails_4.2/app/mailers/.keep +0 -0
- data/spec/app/rails_4.2/app/models/.keep +0 -0
- data/spec/app/rails_4.2/app/models/concerns/.keep +0 -0
- data/spec/app/rails_4.2/app/views/layouts/application.html.erb +14 -0
- data/spec/app/rails_4.2/bin/bundle +3 -0
- data/spec/app/rails_4.2/bin/rails +8 -0
- data/spec/app/rails_4.2/bin/rake +8 -0
- data/spec/app/rails_4.2/bin/setup +29 -0
- data/spec/app/rails_4.2/bin/spring +15 -0
- data/spec/app/rails_4.2/config.ru +4 -0
- data/spec/app/rails_4.2/config/application.rb +31 -0
- data/spec/app/rails_4.2/config/boot.rb +3 -0
- data/spec/app/rails_4.2/config/database.yml +25 -0
- data/spec/app/rails_4.2/config/environment.rb +5 -0
- data/spec/app/rails_4.2/config/environments/development.rb +41 -0
- data/spec/app/rails_4.2/config/environments/production.rb +79 -0
- data/spec/app/rails_4.2/config/environments/test.rb +42 -0
- data/spec/app/rails_4.2/config/initializers/assets.rb +11 -0
- data/spec/app/rails_4.2/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/app/rails_4.2/config/initializers/cookies_serializer.rb +3 -0
- data/spec/app/rails_4.2/config/initializers/filter_parameter_logging.rb +4 -0
- data/spec/app/rails_4.2/config/initializers/inflections.rb +16 -0
- data/spec/app/rails_4.2/config/initializers/mime_types.rb +4 -0
- data/spec/app/rails_4.2/config/initializers/session_store.rb +3 -0
- data/spec/app/rails_4.2/config/initializers/wrap_parameters.rb +14 -0
- data/spec/app/rails_4.2/config/locales/en.yml +23 -0
- data/spec/app/rails_4.2/config/routes.rb +56 -0
- data/spec/app/rails_4.2/config/secrets.yml +22 -0
- data/spec/app/rails_4.2/config/settings.yml +7 -0
- data/spec/app/rails_4.2/db/seeds.rb +7 -0
- data/spec/app/rails_4.2/public/404.html +67 -0
- data/spec/app/rails_4.2/public/422.html +67 -0
- data/spec/app/rails_4.2/public/500.html +66 -0
- data/spec/app/rails_4.2/public/favicon.ico +0 -0
- data/spec/app/rails_4.2/public/robots.txt +5 -0
- data/spec/app/rails_4/Rakefile +6 -0
- data/spec/app/rails_4/app/assets/javascripts/application.js +13 -0
- data/spec/app/rails_4/app/assets/stylesheets/application.css +13 -0
- data/spec/app/rails_4/app/controllers/application_controller.rb +5 -0
- data/spec/app/rails_4/app/helpers/application_helper.rb +2 -0
- data/spec/app/rails_4/app/models/.keep +0 -0
- data/spec/app/rails_4/app/views/layouts/application.html.erb +14 -0
- data/spec/app/rails_4/bin/bundle +3 -0
- data/spec/app/rails_4/bin/rails +4 -0
- data/spec/app/rails_4/bin/rake +4 -0
- data/spec/app/rails_4/config.ru +4 -0
- data/spec/app/rails_4/config/application.rb +27 -0
- data/spec/app/rails_4/config/boot.rb +5 -0
- data/spec/app/rails_4/config/database.yml +25 -0
- data/spec/app/rails_4/config/environment.rb +5 -0
- data/spec/app/rails_4/config/environments/development.rb +34 -0
- data/spec/app/rails_4/config/environments/production.rb +85 -0
- data/spec/app/rails_4/config/environments/test.rb +41 -0
- data/spec/app/rails_4/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/app/rails_4/config/initializers/filter_parameter_logging.rb +4 -0
- data/spec/app/rails_4/config/initializers/inflections.rb +16 -0
- data/spec/app/rails_4/config/initializers/mime_types.rb +5 -0
- data/spec/app/rails_4/config/initializers/secret_token.rb +12 -0
- data/spec/app/rails_4/config/initializers/session_store.rb +3 -0
- data/spec/app/rails_4/config/initializers/wrap_parameters.rb +14 -0
- data/spec/app/rails_4/config/locales/en.yml +23 -0
- data/spec/app/rails_4/config/routes.rb +2 -0
- data/spec/app/rails_4/config/settings.yml +7 -0
- data/spec/app/rails_4/db/.keep +0 -0
- data/spec/app/rails_4/public/404.html +58 -0
- data/spec/app/rails_4/public/422.html +58 -0
- data/spec/app/rails_4/public/500.html +57 -0
- data/spec/config_spec.rb +317 -0
- data/spec/fixtures/bool_override/config1.yml +2 -0
- data/spec/fixtures/bool_override/config2.yml +2 -0
- data/spec/fixtures/custom_types/hash.yml +7 -0
- data/spec/fixtures/deep_merge/config1.yml +28 -0
- data/spec/fixtures/deep_merge/config2.yml +28 -0
- data/spec/fixtures/deep_merge2/config1.yml +3 -0
- data/spec/fixtures/deep_merge2/config2.yml +2 -0
- data/spec/fixtures/development.yml +4 -0
- data/spec/fixtures/empty1.yml +0 -0
- data/spec/fixtures/empty2.yml +0 -0
- data/spec/fixtures/env/settings.yml +4 -0
- data/spec/fixtures/malformed.yml +6 -0
- data/spec/fixtures/reserved_keywords.yml +2 -0
- data/spec/fixtures/settings.yml +21 -0
- data/spec/fixtures/settings2.yml +1 -0
- data/spec/fixtures/with_erb.yml +4 -0
- data/spec/options_spec.rb +84 -0
- data/spec/sources/yaml_source_spec.rb +77 -0
- data/spec/spec_helper.rb +54 -0
- data/spec/support/rails_config_helper.rb +22 -0
- data/spec/support/shared_contexts/rake.rb +8 -0
- data/spec/tasks/db_spec.rb +9 -0
- metadata +580 -42
data/Rakefile
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
|
3
|
+
begin
|
4
|
+
require 'bundler/setup'
|
5
|
+
|
6
|
+
Bundler::GemHelper.install_tasks
|
7
|
+
|
8
|
+
rescue LoadError
|
9
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
10
|
+
end
|
11
|
+
|
12
|
+
##
|
13
|
+
# Testing
|
14
|
+
#
|
15
|
+
require "rspec"
|
16
|
+
require "rspec/core/rake_task"
|
17
|
+
|
18
|
+
RSpec::Core::RakeTask.new(:spec)
|
19
|
+
|
20
|
+
# Test for multiple Rails scenarios
|
21
|
+
if !ENV["APPRAISAL_INITIALIZED"] && !ENV["TRAVIS"]
|
22
|
+
require "appraisal"
|
23
|
+
|
24
|
+
task :default => :appraisal
|
25
|
+
else
|
26
|
+
task :default => :spec
|
27
|
+
end
|
28
|
+
|
29
|
+
##
|
30
|
+
# Documentation
|
31
|
+
#
|
32
|
+
require 'rdoc/task'
|
33
|
+
|
34
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
35
|
+
rdoc.rdoc_dir = 'rdoc'
|
36
|
+
rdoc.title = "Config #{Config::VERSION}"
|
37
|
+
rdoc.options << '--line-numbers'
|
38
|
+
rdoc.rdoc_files.include('README.*')
|
39
|
+
rdoc.rdoc_files.include('CHANGELOG.*')
|
40
|
+
rdoc.rdoc_files.include('LICENSE.*')
|
41
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
42
|
+
end
|
data/config.gemspec
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
$:.push File.expand_path("../lib", __FILE__)
|
2
|
+
|
3
|
+
# Maintain your gem's version:
|
4
|
+
require 'config/version'
|
5
|
+
|
6
|
+
# Describe your gem and declare its dependencies:
|
7
|
+
Gem::Specification.new do |s|
|
8
|
+
s.name = "config"
|
9
|
+
s.version = Config::VERSION
|
10
|
+
s.date = Time.now.strftime '%F'
|
11
|
+
s.authors = ["Jacques Crocker", "Fred Wu", "Piotr Kuczynski"]
|
12
|
+
s.email = ["railsjedi@gmail.com", "ifredwu@gmail.com", "piotr.kuczynski@gmail.com"]
|
13
|
+
s.summary = "Easiest way to add multi-environment yaml settings to Rails, Sinatra, Pandrino and other ruby projects."
|
14
|
+
s.description = "Easiest way to add multi-environment yaml settings to Rails, Sinatra, Pandrino and other ruby projects."
|
15
|
+
s.homepage = "https://github.com/railsconfig/config"
|
16
|
+
s.license = "MIT"
|
17
|
+
s.extra_rdoc_files = ["README.md"]
|
18
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
19
|
+
|
20
|
+
s.files = `git ls-files`.split($/)
|
21
|
+
|
22
|
+
s.executables = s.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
23
|
+
s.test_files = s.files.grep(%r{^(test|spec|features)/})
|
24
|
+
s.require_paths = ["lib"]
|
25
|
+
|
26
|
+
s.add_dependency "activesupport", ">= 3.0"
|
27
|
+
s.add_dependency "deep_merge", "~> 1.0.0"
|
28
|
+
|
29
|
+
s.add_development_dependency "bundler", "~> 1.10.5"
|
30
|
+
s.add_development_dependency "rake"
|
31
|
+
s.add_development_dependency "rdoc"
|
32
|
+
s.add_development_dependency "pry"
|
33
|
+
|
34
|
+
# Testing
|
35
|
+
s.add_development_dependency "appraisal", "~> 2.0.2"
|
36
|
+
s.add_development_dependency "rails", "~> 4.2.3"
|
37
|
+
s.add_development_dependency "rspec", "~> 3.3.0"
|
38
|
+
s.add_development_dependency "rspec-rails", "~> 3.3.3"
|
39
|
+
s.add_development_dependency "test-unit", "~> 3.1.3"
|
40
|
+
s.add_development_dependency "sqlite3"
|
41
|
+
s.add_development_dependency "rubocop"
|
42
|
+
|
43
|
+
if ENV['TRAVIS']
|
44
|
+
s.add_development_dependency "codeclimate-test-reporter"
|
45
|
+
end
|
46
|
+
end
|
data/lib/config.rb
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
require 'active_support/core_ext/module/attribute_accessors'
|
2
|
+
|
3
|
+
require 'config/options'
|
4
|
+
require 'config/version'
|
5
|
+
require 'config/engine' if defined?(::Rails)
|
6
|
+
require 'config/sources/yaml_source'
|
7
|
+
require 'deep_merge'
|
8
|
+
|
9
|
+
module Config
|
10
|
+
# ensures the setup only gets run once
|
11
|
+
@@_ran_once = false
|
12
|
+
|
13
|
+
mattr_accessor :const_name, :use_env
|
14
|
+
@@const_name = "Settings"
|
15
|
+
@@use_env = false
|
16
|
+
|
17
|
+
def self.setup
|
18
|
+
yield self if @@_ran_once == false
|
19
|
+
@@_ran_once = true
|
20
|
+
end
|
21
|
+
|
22
|
+
# Create a populated Options instance from a yaml file. If a second yaml file is given, then the sections of that
|
23
|
+
# file will overwrite existing sections of the first file.
|
24
|
+
def self.load_files(*files)
|
25
|
+
config = Options.new
|
26
|
+
|
27
|
+
# add yaml sources
|
28
|
+
[files].flatten.compact.uniq.each do |file|
|
29
|
+
config.add_source!(file.to_s)
|
30
|
+
end
|
31
|
+
|
32
|
+
config.load!
|
33
|
+
config.load_env! if @@use_env
|
34
|
+
config
|
35
|
+
end
|
36
|
+
|
37
|
+
# Loads and sets the settings constant!
|
38
|
+
def self.load_and_set_settings(*files)
|
39
|
+
Kernel.send(:remove_const, Config.const_name) if Kernel.const_defined?(Config.const_name)
|
40
|
+
Kernel.const_set(Config.const_name, Config.load_files(files))
|
41
|
+
end
|
42
|
+
|
43
|
+
def self.setting_files(config_root, env)
|
44
|
+
[
|
45
|
+
File.join(config_root, "settings.yml").to_s,
|
46
|
+
File.join(config_root, "settings", "#{env}.yml").to_s,
|
47
|
+
File.join(config_root, "environments", "#{env}.yml").to_s,
|
48
|
+
|
49
|
+
File.join(config_root, "settings.local.yml").to_s,
|
50
|
+
File.join(config_root, "settings", "#{env}.local.yml").to_s,
|
51
|
+
File.join(config_root, "environments", "#{env}.local.yml").to_s
|
52
|
+
].freeze
|
53
|
+
end
|
54
|
+
|
55
|
+
def self.reload!
|
56
|
+
Kernel.const_get(Config.const_name).reload!
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
# add rails integration
|
61
|
+
require('config/integration/rails') if defined?(::Rails)
|
62
|
+
|
63
|
+
# add sinatra integration
|
64
|
+
require('config/integration/sinatra') if defined?(::Sinatra)
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module Config
|
2
|
+
module Integration
|
3
|
+
module Rails
|
4
|
+
if defined?(::Rails::Railtie)
|
5
|
+
class Railtie < ::Rails::Railtie
|
6
|
+
# Load rake tasks (eg. Heroku)
|
7
|
+
rake_tasks do
|
8
|
+
Dir[File.join(File.dirname(__FILE__),'../tasks/*.rake')].each { |f| load f }
|
9
|
+
end
|
10
|
+
|
11
|
+
config.before_configuration { preload }
|
12
|
+
|
13
|
+
def preload
|
14
|
+
# Manually load the custom initializer before everything else
|
15
|
+
initializer = ::Rails.root.join("config", "initializers", "config.rb")
|
16
|
+
require initializer if File.exist?(initializer)
|
17
|
+
|
18
|
+
# Parse the settings before any of the initializers
|
19
|
+
Config.load_and_set_settings(
|
20
|
+
Config.setting_files(::Rails.root.join("config"), ::Rails.env)
|
21
|
+
)
|
22
|
+
end
|
23
|
+
|
24
|
+
# Rails Dev environment should reload the Settings on every request
|
25
|
+
if ::Rails.env.development?
|
26
|
+
initializer :config_reload_on_development do
|
27
|
+
ActionController::Base.class_eval do
|
28
|
+
prepend_before_filter { ::Config.reload! }
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require "config/rack/reloader"
|
2
|
+
|
3
|
+
module Config
|
4
|
+
# provide helper to register within your Sinatra app
|
5
|
+
#
|
6
|
+
# set :root, File.dirname(__FILE__)
|
7
|
+
# register Config
|
8
|
+
#
|
9
|
+
def self.registered(app)
|
10
|
+
app.configure do |inner_app|
|
11
|
+
|
12
|
+
env = inner_app.environment || ENV["RACK_ENV"]
|
13
|
+
root = inner_app.root
|
14
|
+
|
15
|
+
# use Padrino settings if applicable
|
16
|
+
if defined?(Padrino)
|
17
|
+
env = Padrino.env
|
18
|
+
root = Padrino.root
|
19
|
+
end
|
20
|
+
|
21
|
+
Config.load_and_set_settings(Config.setting_files(File.join(root, 'config'), env))
|
22
|
+
|
23
|
+
inner_app.use(::Config::Rack::Reloader) if inner_app.development?
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,157 @@
|
|
1
|
+
require 'ostruct'
|
2
|
+
module Config
|
3
|
+
class Options < OpenStruct
|
4
|
+
include Enumerable
|
5
|
+
|
6
|
+
def keys
|
7
|
+
marshal_dump.keys
|
8
|
+
end
|
9
|
+
|
10
|
+
def empty?
|
11
|
+
marshal_dump.empty?
|
12
|
+
end
|
13
|
+
|
14
|
+
def add_source!(source)
|
15
|
+
# handle yaml file paths
|
16
|
+
source = (Sources::YAMLSource.new(source)) if source.is_a?(String)
|
17
|
+
|
18
|
+
@config_sources ||= []
|
19
|
+
@config_sources << source
|
20
|
+
end
|
21
|
+
|
22
|
+
def prepend_source!(source)
|
23
|
+
source = (Sources::YAMLSource.new(source)) if source.is_a?(String)
|
24
|
+
|
25
|
+
@config_sources ||= []
|
26
|
+
@config_sources.unshift(source)
|
27
|
+
end
|
28
|
+
|
29
|
+
def reload_env!
|
30
|
+
return self if ENV.nil? || ENV.empty?
|
31
|
+
conf = Hash.new
|
32
|
+
ENV.each do |key, value|
|
33
|
+
next unless key.to_s.index(Config.const_name) == 0
|
34
|
+
hash = value
|
35
|
+
key.to_s.split('.').reverse.each do |element|
|
36
|
+
hash = {element => hash}
|
37
|
+
end
|
38
|
+
DeepMerge.deep_merge!(hash, conf, :preserve_unmergeables => false)
|
39
|
+
end
|
40
|
+
|
41
|
+
merge!(conf[Config.const_name] || {})
|
42
|
+
end
|
43
|
+
|
44
|
+
alias :load_env! :reload_env!
|
45
|
+
|
46
|
+
# look through all our sources and rebuild the configuration
|
47
|
+
def reload!
|
48
|
+
conf = {}
|
49
|
+
@config_sources.each do |source|
|
50
|
+
source_conf = source.load
|
51
|
+
|
52
|
+
if conf.empty?
|
53
|
+
conf = source_conf
|
54
|
+
else
|
55
|
+
DeepMerge.deep_merge!(source_conf, conf, :preserve_unmergeables => false)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
# swap out the contents of the OStruct with a hash (need to recursively convert)
|
60
|
+
marshal_load(__convert(conf).marshal_dump)
|
61
|
+
|
62
|
+
reload_env! if Config.use_env
|
63
|
+
|
64
|
+
return self
|
65
|
+
end
|
66
|
+
|
67
|
+
alias :load! :reload!
|
68
|
+
|
69
|
+
def reload_from_files(*files)
|
70
|
+
Config.load_and_set_settings(files)
|
71
|
+
reload!
|
72
|
+
end
|
73
|
+
|
74
|
+
def to_hash
|
75
|
+
result = {}
|
76
|
+
marshal_dump.each do |k, v|
|
77
|
+
if v.instance_of? Config::Options
|
78
|
+
result[k] = v.to_hash
|
79
|
+
elsif v.instance_of? Array
|
80
|
+
result[k] = descend_array(v)
|
81
|
+
else
|
82
|
+
result[k] = v
|
83
|
+
end
|
84
|
+
end
|
85
|
+
result
|
86
|
+
end
|
87
|
+
|
88
|
+
def each(*args, &block)
|
89
|
+
marshal_dump.each(*args, &block)
|
90
|
+
end
|
91
|
+
|
92
|
+
def to_json(*args)
|
93
|
+
require "json" unless defined?(JSON)
|
94
|
+
to_hash.to_json(*args)
|
95
|
+
end
|
96
|
+
|
97
|
+
def merge!(hash)
|
98
|
+
current = to_hash
|
99
|
+
DeepMerge.deep_merge!(hash.dup, current)
|
100
|
+
marshal_load(__convert(current).marshal_dump)
|
101
|
+
self
|
102
|
+
end
|
103
|
+
|
104
|
+
# Some keywords that don't play nicely with OpenStruct
|
105
|
+
SETTINGS_RESERVED_NAMES = %w{select collect}
|
106
|
+
|
107
|
+
# An alternative mechanism for property access.
|
108
|
+
# This let's you do foo['bar'] along with foo.bar.
|
109
|
+
def [](param)
|
110
|
+
return super if SETTINGS_RESERVED_NAMES.include?(param)
|
111
|
+
send("#{param}")
|
112
|
+
end
|
113
|
+
|
114
|
+
def []=(param, value)
|
115
|
+
send("#{param}=", value)
|
116
|
+
end
|
117
|
+
|
118
|
+
SETTINGS_RESERVED_NAMES.each do |name|
|
119
|
+
define_method name do
|
120
|
+
self[name]
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
protected
|
125
|
+
|
126
|
+
def descend_array(array)
|
127
|
+
array.length.times do |i|
|
128
|
+
value = array[i]
|
129
|
+
if value.instance_of? Config::Options
|
130
|
+
array[i] = value.to_hash
|
131
|
+
elsif value.instance_of? Array
|
132
|
+
array[i] = descend_array(value)
|
133
|
+
end
|
134
|
+
end
|
135
|
+
array
|
136
|
+
end
|
137
|
+
|
138
|
+
# Recursively converts Hashes to Options (including Hashes inside Arrays)
|
139
|
+
def __convert(h) #:nodoc:
|
140
|
+
s = self.class.new
|
141
|
+
|
142
|
+
h.each do |k, v|
|
143
|
+
k = k.to_s if !k.respond_to?(:to_sym) && k.respond_to?(:to_s)
|
144
|
+
s.new_ostruct_member(k)
|
145
|
+
|
146
|
+
if v.is_a?(Hash)
|
147
|
+
v = v["type"] == "hash" ? v["contents"] : __convert(v)
|
148
|
+
elsif v.is_a?(Array)
|
149
|
+
v = v.collect { |e| e.instance_of?(Hash) ? __convert(e) : e }
|
150
|
+
end
|
151
|
+
|
152
|
+
s.send("#{k}=".to_sym, v)
|
153
|
+
end
|
154
|
+
s
|
155
|
+
end
|
156
|
+
end
|
157
|
+
end
|