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.
Files changed (199) hide show
  1. checksums.yaml +7 -0
  2. data/.codeclimate.yml +6 -0
  3. data/.gitignore +14 -0
  4. data/.rspec +2 -0
  5. data/.ruby-gemset +1 -0
  6. data/.ruby-version +1 -0
  7. data/.travis.yml +23 -0
  8. data/Appraisals +15 -0
  9. data/CHANGELOG.md +25 -0
  10. data/Gemfile +6 -0
  11. data/LICENSE.md +27 -0
  12. data/README.md +315 -0
  13. data/Rakefile +42 -0
  14. data/config.gemspec +46 -0
  15. data/gemfiles/rails_3.gemfile +7 -0
  16. data/gemfiles/rails_4.1.gemfile +7 -0
  17. data/gemfiles/rails_4.2.gemfile +7 -0
  18. data/gemfiles/rails_4.gemfile +7 -0
  19. data/lib/config.rb +64 -0
  20. data/lib/config/engine.rb +5 -0
  21. data/lib/config/integration/rails.rb +36 -0
  22. data/lib/config/integration/sinatra.rb +26 -0
  23. data/lib/config/options.rb +157 -0
  24. data/lib/config/rack/reloader.rb +15 -0
  25. data/lib/config/railtie.rb +9 -0
  26. data/lib/config/sources/yaml_source.rb +26 -0
  27. data/lib/config/tasks.rb +59 -0
  28. data/lib/config/tasks/heroku.rake +8 -0
  29. data/lib/config/version.rb +3 -0
  30. data/lib/generators/config/install_generator.rb +32 -0
  31. data/lib/generators/config/templates/rails_config.rb +3 -0
  32. data/lib/generators/config/templates/settings.local.yml +0 -0
  33. data/lib/generators/config/templates/settings.yml +0 -0
  34. data/lib/generators/config/templates/settings/development.yml +0 -0
  35. data/lib/generators/config/templates/settings/production.yml +0 -0
  36. data/lib/generators/config/templates/settings/test.yml +0 -0
  37. data/spec/app/rails_3/Rakefile +7 -0
  38. data/spec/app/rails_3/app/assets/javascripts/application.js +13 -0
  39. data/spec/app/rails_3/app/assets/stylesheets/application.css +13 -0
  40. data/spec/app/rails_3/app/controllers/application_controller.rb +3 -0
  41. data/spec/app/rails_3/app/helpers/application_helper.rb +2 -0
  42. data/spec/app/rails_3/app/models/.gitkeep +0 -0
  43. data/spec/app/rails_3/app/views/layouts/application.html.erb +14 -0
  44. data/spec/app/rails_3/bin/bundle +3 -0
  45. data/spec/app/rails_3/bin/rails +4 -0
  46. data/spec/app/rails_3/bin/rake +4 -0
  47. data/spec/app/rails_3/config.ru +4 -0
  48. data/spec/app/rails_3/config/application.rb +29 -0
  49. data/spec/app/rails_3/config/boot.rb +5 -0
  50. data/spec/app/rails_3/config/database.yml +25 -0
  51. data/spec/app/rails_3/config/environment.rb +5 -0
  52. data/spec/app/rails_3/config/environments/development.rb +42 -0
  53. data/spec/app/rails_3/config/environments/production.rb +72 -0
  54. data/spec/app/rails_3/config/environments/test.rb +42 -0
  55. data/spec/app/rails_3/config/initializers/backtrace_silencers.rb +7 -0
  56. data/spec/app/rails_3/config/initializers/inflections.rb +15 -0
  57. data/spec/app/rails_3/config/initializers/mime_types.rb +5 -0
  58. data/spec/app/rails_3/config/initializers/secret_token.rb +7 -0
  59. data/spec/app/rails_3/config/initializers/session_store.rb +8 -0
  60. data/spec/app/rails_3/config/initializers/wrap_parameters.rb +14 -0
  61. data/spec/app/rails_3/config/locales/en.yml +5 -0
  62. data/spec/app/rails_3/config/routes.rb +2 -0
  63. data/spec/app/rails_3/config/settings.yml +7 -0
  64. data/spec/app/rails_3/db/.gitkeep +0 -0
  65. data/spec/app/rails_3/public/404.html +26 -0
  66. data/spec/app/rails_3/public/422.html +26 -0
  67. data/spec/app/rails_3/public/500.html +25 -0
  68. data/spec/app/rails_3/script/rails +6 -0
  69. data/spec/app/rails_4.1/Rakefile +6 -0
  70. data/spec/app/rails_4.1/app/assets/javascripts/application.js +13 -0
  71. data/spec/app/rails_4.1/app/assets/stylesheets/application.css +15 -0
  72. data/spec/app/rails_4.1/app/controllers/application_controller.rb +5 -0
  73. data/spec/app/rails_4.1/app/helpers/application_helper.rb +2 -0
  74. data/spec/app/rails_4.1/app/models/.keep +0 -0
  75. data/spec/app/rails_4.1/app/views/layouts/application.html.erb +14 -0
  76. data/spec/app/rails_4.1/bin/bundle +3 -0
  77. data/spec/app/rails_4.1/bin/rails +4 -0
  78. data/spec/app/rails_4.1/bin/rake +4 -0
  79. data/spec/app/rails_4.1/config.ru +4 -0
  80. data/spec/app/rails_4.1/config/application.rb +27 -0
  81. data/spec/app/rails_4.1/config/boot.rb +5 -0
  82. data/spec/app/rails_4.1/config/database.yml +25 -0
  83. data/spec/app/rails_4.1/config/environment.rb +5 -0
  84. data/spec/app/rails_4.1/config/environments/development.rb +42 -0
  85. data/spec/app/rails_4.1/config/environments/production.rb +88 -0
  86. data/spec/app/rails_4.1/config/environments/test.rb +44 -0
  87. data/spec/app/rails_4.1/config/initializers/backtrace_silencers.rb +7 -0
  88. data/spec/app/rails_4.1/config/initializers/cookies_serializer.rb +3 -0
  89. data/spec/app/rails_4.1/config/initializers/filter_parameter_logging.rb +4 -0
  90. data/spec/app/rails_4.1/config/initializers/inflections.rb +16 -0
  91. data/spec/app/rails_4.1/config/initializers/mime_types.rb +4 -0
  92. data/spec/app/rails_4.1/config/initializers/session_store.rb +3 -0
  93. data/spec/app/rails_4.1/config/initializers/wrap_parameters.rb +14 -0
  94. data/spec/app/rails_4.1/config/locales/en.yml +23 -0
  95. data/spec/app/rails_4.1/config/routes.rb +2 -0
  96. data/spec/app/rails_4.1/config/secrets.yml +22 -0
  97. data/spec/app/rails_4.1/config/settings.yml +7 -0
  98. data/spec/app/rails_4.1/db/.keep +0 -0
  99. data/spec/app/rails_4.1/public/404.html +67 -0
  100. data/spec/app/rails_4.1/public/422.html +67 -0
  101. data/spec/app/rails_4.1/public/500.html +66 -0
  102. data/spec/app/rails_4.2/Rakefile +6 -0
  103. data/spec/app/rails_4.2/app/assets/images/.keep +0 -0
  104. data/spec/app/rails_4.2/app/assets/javascripts/application.js +16 -0
  105. data/spec/app/rails_4.2/app/assets/stylesheets/application.css +15 -0
  106. data/spec/app/rails_4.2/app/controllers/application_controller.rb +5 -0
  107. data/spec/app/rails_4.2/app/controllers/concerns/.keep +0 -0
  108. data/spec/app/rails_4.2/app/helpers/application_helper.rb +2 -0
  109. data/spec/app/rails_4.2/app/mailers/.keep +0 -0
  110. data/spec/app/rails_4.2/app/models/.keep +0 -0
  111. data/spec/app/rails_4.2/app/models/concerns/.keep +0 -0
  112. data/spec/app/rails_4.2/app/views/layouts/application.html.erb +14 -0
  113. data/spec/app/rails_4.2/bin/bundle +3 -0
  114. data/spec/app/rails_4.2/bin/rails +8 -0
  115. data/spec/app/rails_4.2/bin/rake +8 -0
  116. data/spec/app/rails_4.2/bin/setup +29 -0
  117. data/spec/app/rails_4.2/bin/spring +15 -0
  118. data/spec/app/rails_4.2/config.ru +4 -0
  119. data/spec/app/rails_4.2/config/application.rb +31 -0
  120. data/spec/app/rails_4.2/config/boot.rb +3 -0
  121. data/spec/app/rails_4.2/config/database.yml +25 -0
  122. data/spec/app/rails_4.2/config/environment.rb +5 -0
  123. data/spec/app/rails_4.2/config/environments/development.rb +41 -0
  124. data/spec/app/rails_4.2/config/environments/production.rb +79 -0
  125. data/spec/app/rails_4.2/config/environments/test.rb +42 -0
  126. data/spec/app/rails_4.2/config/initializers/assets.rb +11 -0
  127. data/spec/app/rails_4.2/config/initializers/backtrace_silencers.rb +7 -0
  128. data/spec/app/rails_4.2/config/initializers/cookies_serializer.rb +3 -0
  129. data/spec/app/rails_4.2/config/initializers/filter_parameter_logging.rb +4 -0
  130. data/spec/app/rails_4.2/config/initializers/inflections.rb +16 -0
  131. data/spec/app/rails_4.2/config/initializers/mime_types.rb +4 -0
  132. data/spec/app/rails_4.2/config/initializers/session_store.rb +3 -0
  133. data/spec/app/rails_4.2/config/initializers/wrap_parameters.rb +14 -0
  134. data/spec/app/rails_4.2/config/locales/en.yml +23 -0
  135. data/spec/app/rails_4.2/config/routes.rb +56 -0
  136. data/spec/app/rails_4.2/config/secrets.yml +22 -0
  137. data/spec/app/rails_4.2/config/settings.yml +7 -0
  138. data/spec/app/rails_4.2/db/seeds.rb +7 -0
  139. data/spec/app/rails_4.2/public/404.html +67 -0
  140. data/spec/app/rails_4.2/public/422.html +67 -0
  141. data/spec/app/rails_4.2/public/500.html +66 -0
  142. data/spec/app/rails_4.2/public/favicon.ico +0 -0
  143. data/spec/app/rails_4.2/public/robots.txt +5 -0
  144. data/spec/app/rails_4/Rakefile +6 -0
  145. data/spec/app/rails_4/app/assets/javascripts/application.js +13 -0
  146. data/spec/app/rails_4/app/assets/stylesheets/application.css +13 -0
  147. data/spec/app/rails_4/app/controllers/application_controller.rb +5 -0
  148. data/spec/app/rails_4/app/helpers/application_helper.rb +2 -0
  149. data/spec/app/rails_4/app/models/.keep +0 -0
  150. data/spec/app/rails_4/app/views/layouts/application.html.erb +14 -0
  151. data/spec/app/rails_4/bin/bundle +3 -0
  152. data/spec/app/rails_4/bin/rails +4 -0
  153. data/spec/app/rails_4/bin/rake +4 -0
  154. data/spec/app/rails_4/config.ru +4 -0
  155. data/spec/app/rails_4/config/application.rb +27 -0
  156. data/spec/app/rails_4/config/boot.rb +5 -0
  157. data/spec/app/rails_4/config/database.yml +25 -0
  158. data/spec/app/rails_4/config/environment.rb +5 -0
  159. data/spec/app/rails_4/config/environments/development.rb +34 -0
  160. data/spec/app/rails_4/config/environments/production.rb +85 -0
  161. data/spec/app/rails_4/config/environments/test.rb +41 -0
  162. data/spec/app/rails_4/config/initializers/backtrace_silencers.rb +7 -0
  163. data/spec/app/rails_4/config/initializers/filter_parameter_logging.rb +4 -0
  164. data/spec/app/rails_4/config/initializers/inflections.rb +16 -0
  165. data/spec/app/rails_4/config/initializers/mime_types.rb +5 -0
  166. data/spec/app/rails_4/config/initializers/secret_token.rb +12 -0
  167. data/spec/app/rails_4/config/initializers/session_store.rb +3 -0
  168. data/spec/app/rails_4/config/initializers/wrap_parameters.rb +14 -0
  169. data/spec/app/rails_4/config/locales/en.yml +23 -0
  170. data/spec/app/rails_4/config/routes.rb +2 -0
  171. data/spec/app/rails_4/config/settings.yml +7 -0
  172. data/spec/app/rails_4/db/.keep +0 -0
  173. data/spec/app/rails_4/public/404.html +58 -0
  174. data/spec/app/rails_4/public/422.html +58 -0
  175. data/spec/app/rails_4/public/500.html +57 -0
  176. data/spec/config_spec.rb +317 -0
  177. data/spec/fixtures/bool_override/config1.yml +2 -0
  178. data/spec/fixtures/bool_override/config2.yml +2 -0
  179. data/spec/fixtures/custom_types/hash.yml +7 -0
  180. data/spec/fixtures/deep_merge/config1.yml +28 -0
  181. data/spec/fixtures/deep_merge/config2.yml +28 -0
  182. data/spec/fixtures/deep_merge2/config1.yml +3 -0
  183. data/spec/fixtures/deep_merge2/config2.yml +2 -0
  184. data/spec/fixtures/development.yml +4 -0
  185. data/spec/fixtures/empty1.yml +0 -0
  186. data/spec/fixtures/empty2.yml +0 -0
  187. data/spec/fixtures/env/settings.yml +4 -0
  188. data/spec/fixtures/malformed.yml +6 -0
  189. data/spec/fixtures/reserved_keywords.yml +2 -0
  190. data/spec/fixtures/settings.yml +21 -0
  191. data/spec/fixtures/settings2.yml +1 -0
  192. data/spec/fixtures/with_erb.yml +4 -0
  193. data/spec/options_spec.rb +84 -0
  194. data/spec/sources/yaml_source_spec.rb +77 -0
  195. data/spec/spec_helper.rb +54 -0
  196. data/spec/support/rails_config_helper.rb +22 -0
  197. data/spec/support/shared_contexts/rake.rb +8 -0
  198. data/spec/tasks/db_spec.rb +9 -0
  199. 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
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "rails", "3.2.22"
6
+
7
+ gemspec :path => "../"
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "rails", "4.1.12"
6
+
7
+ gemspec :path => "../"
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "rails", "4.2.3"
6
+
7
+ gemspec :path => "../"
@@ -0,0 +1,7 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "rails", "4.0.13"
6
+
7
+ gemspec :path => "../"
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,5 @@
1
+ module Config
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace Config
4
+ end
5
+ end
@@ -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
@@ -0,0 +1,15 @@
1
+ module Config
2
+ module Rack
3
+ # Rack middleware the reloads Config on every request (only use in dev mode)
4
+ class Reloader
5
+ def initialize(app)
6
+ @app = app
7
+ end
8
+
9
+ def call(env)
10
+ Config.reload!
11
+ @app.call(env)
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,9 @@
1
+ require 'rails'
2
+
3
+ module Config
4
+ class Railtie < Rails::Railtie
5
+ rake_tasks do
6
+ Dir[File.join(File.dirname(__FILE__),'tasks/*.rake')].each { |f| load f }
7
+ end
8
+ end
9
+ end