Skip to content

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also .

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also .
...
  • 5 commits
  • 10 files changed
  • 0 commit comments
  • 1 contributor
Showing with 154 additions and 68 deletions.
  1. +1 −1 Gemfile.lock
  2. +6 −1 HISTORY.md
  3. +121 −30 README.md
  4. +1 −1 configy.gemspec
  5. +5 −17 lib/configy.rb
  6. +4 −0 lib/configy/base.rb
  7. +4 −4 spec/load_path_spec.rb
  8. +2 −1 spec/nested_config_spec.rb
  9. +4 −13 spec/section_spec.rb
  10. +6 −0 spec/spec_helper.rb
View
2 Gemfile.lock
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
- configy (1.1.2)
+ configy (1.1.3)
hashie (~> 1.2.0)
GEM
View
7 HISTORY.md
@@ -1,6 +1,11 @@
+## 1.1.3 (2012-03-31)
+
+* Allow hash access from main config object
+* Check environment variables instead of constants
+
## 1.1.2 (2012-03-30)
-* If CONFIGY_ENV is set, use that to determine config section to use.
+* If ENV['CONFIGY_ENV'] is set, use that to determine config section to use.
## 1.1.1 (2012-03-18)
View
151 README.md
@@ -1,55 +1,146 @@
# Configy
-## Description
+Configy creates a config object based on a YAML file.
-It allows to have a file (`config/app_config.yml`) with application configuration parameters.
-It should have a "common" section with all parameters along with default values and can also
-contain a section for each of the rails environments (development, test, production, or
-your custom one). The values from the current environment section will override the values in the
-"common" section.
+## Summary
-If some developer needs his own specific values for his working copy, he can simply create
-a `config/app_config.local.yml` file and override any value there, again having a "common" section
-and a section for each environment.
+It allows you to have a file (`config/app_config.yml`) with application
+configuration parameters. It should have a "common" section with all
+parameters along with default values and can also contain a section for each
+application environment (development, test, production, or your custom one).
+The values from the current environment section will override the values in
+the "common" section.
-Nothing is mandatory (files, sections) you just have what you really need. The files are parsed with ERB,
-so they can contain some Ruby. It also checks for file modifications so you don't have to restart the server to pick up new values on production.
+If a developer needs his own specific values for his working copy, he can
+simply create a `config/app_config.local.yml` file and override any value
+there, again having a "common" section and a section for each environment.
-## Usage
+The files are parsed with ERB. Configy also checks for file modifications so
+you don't have to restart the server to pick up new values on production.
-Configy provides an [optional] generator.
+## Example
- rails g configy:install
+config/app_config.yaml:
+
+``` yaml
+common:
+ appname: "My App"
+ caching:
+ enabled: false
+ facebook:
+ appid: 123
+ secret: abc
+
+production:
+ caching:
+ enabled: true
+ default_max_age: <%= 1.hour %>
+ facebook:
+ appid: 456
+ secret: def
+
+```
+
+Assuming ENV['RACK_ENV'] == "production"
+
+``` ruby
+Configy.create("app_config") # Creates AppConfig constant
+AppConfig.caching.enabled? # => true
+AppConfig.caching.default_max_age # => 3600
+AppConfig.appname # => "My App"
+AppConfig.facebook.appid # => 456
+
+# Or with less magic:
+
+config = Configy::Base.new("app_config", "production", Rails.root.join("config") )
+config.caching.enabled? # => true
+config['caching']['enabled'] # => true
+...
+
+```
+
+## Features
+
+### Sections
+
+Configy assumes that you are breaking your configurations into sections. It
+will automatically detect which section to use. It looks for an environment
+variable in following order
+
+`ENV['CONFIGY_ENV'], ENV['RAILS_ENV'], ENV['RACK_ENV']`
+
+and assumes you have a section in your YAML file named the same way, for
+example:
+
+``` yaml
+development:
+ facebook_app_id: 123456
+ facebook_secret: abcdef
+production:
+ facebook_app_id: 456789
+ facebook_secret: defghi
+```
+
+If none of the environment variables above are set, it defaults to `development`.
+
+### Common section
-An example of a config file (app_config.yml):
+If you have a section named `common`, that section will provide default values
+for all other sections. Under the hood, Configy deep merges the environment
+section into the common section. For example:
``` yaml
common:
- admin_email: admin@domain.com
- xml_rpc_url: http://domain.com:8000/
- media_path: <%= RAILS_ROOT %>/tmp/media
+ newrelic_enabled: true
+ newrelic_key: "123456"
development:
- xml_rpc_url: http://localhost:8000/
+ newrelic_enabled: false
-test:
- xml_rpc_url: http://localhost:8008/
-```
-
-In an initializer:
+staging:
+
+production:
-``` ruby
-Configy.create(:app_config)
```
-Then, in the application you can use the config parameters like this:
+All environments will inherit the `common` section configurations, and the
+`development` environment overrides the `newrelic_enabled` setting.
+
+### Nested configs
+
+You can nest configs as much as you like.
+
+``` yaml
+common:
+ facebook:
+ app:
+ id: 123
+```
``` ruby
-AppConfig.xml_rpc_url
+AppConfig.facebook.app.id # => 123
```
-So it means that you've got a Config object which holds all the configuration parameters defined.
-It doesn't allow to change the values in the application code, BTW.
+### Local config file
+
+If you have a config file named `config/app_config.yml` and another config
+file named `config/app_config.local.yml`, the local config file's configurations
+will override the main config file's configurations.
+
+### ERB
+
+Config files are parsed as ERB templates
+
+### Auto reloading
+
+Configy will check the config file's mtime to see if it needs to
+reload it's configuration. You can disable this with `Configy.cache_config = true`
+
+### Rails generator
+
+Configy provides an [optional] generator.
+
+ rails g configy:install
## Authors
View
2 configy.gemspec
@@ -1,6 +1,6 @@
Gem::Specification.new do |s|
s.name = "configy"
- s.version = "1.1.2"
+ s.version = "1.1.3"
s.authors = [ "Gabe Varela", "Ben Marini", "Chip Miller", "Bram Swenson", "Jeremy Ruppel" ]
s.date = "2012-03-18"
View
22 lib/configy.rb
@@ -14,29 +14,17 @@ def load_path
@load_path
elsif defined? Rails
Rails.root.join("config")
- elsif defined? RAILS_ROOT
- "#{RAILS_ROOT}/config"
- elsif defined? RACK_ROOT
- "#{RACK_ROOT}/config"
+ elsif ENV['RAILS_ROOT']
+ "#{ENV['RAILS_ROOT']}/config"
+ elsif ENV['RACK_ROOT']
+ "#{ENV['RACK_ROOT']}/config"
else
'config'
end
end
def section
- if @section
- @section
- elsif defined? CONFIGY_ENV
- CONFIGY_ENV
- elsif defined? Rails
- Rails.env
- elsif defined? RAILS_ENV
- RAILS_ENV
- elsif defined? RACK_ENV
- RACK_ENV
- else
- 'development'
- end
+ @section ||= ENV["CONFIGY_ENV"] || ENV["RAILS_ENV"] || ENV["RACK_ENV"] || "development"
end
def cache_config
View
4 lib/configy/base.rb
@@ -18,6 +18,10 @@ def method_missing(name, *args, &block)
end
end
+ def [](key)
+ config[key]
+ end
+
def reload
@config = config_file.config.merge(local_config_file.config).tap do |c|
c.mtime = most_recent_mtime
View
8 spec/load_path_spec.rb
@@ -19,14 +19,14 @@
end
it "should detect rails 2 config dir" do
- with_const( :RAILS_ROOT, "path/to/rails/root" ) do
- Configy.load_path.must_equal "#{RAILS_ROOT}/config"
+ with_env( "RAILS_ROOT", "path/to/rails/root" ) do
+ Configy.load_path.must_equal "#{ENV['RAILS_ROOT']}/config"
end
end
it "should detect rack config dir" do
- with_const( :RACK_ROOT, "path/to/rack/root" ) do
- Configy.load_path.must_equal "#{RACK_ROOT}/config"
+ with_env( "RACK_ROOT", "path/to/rack/root" ) do
+ Configy.load_path.must_equal "#{ENV['RACK_ROOT']}/config"
end
end
end
View
3 spec/nested_config_spec.rb
@@ -7,7 +7,7 @@
Configy.cache_config = nil
end
- it "should be accessbile as methods" do
+ it "should be accessbile as methods or hashes" do
with_config_file({
'common' => {
'level1' => {
@@ -20,6 +20,7 @@
}
}, 'nested_config') do
Configy.create('nested_config')
+ NestedConfig['level1']['level2']['level3'].must_equal "whynot"
NestedConfig.level1.level2.level3.must_equal "whynot"
NestedConfig.level1.level2.nokey?.must_equal false
NestedConfig.level1.level2.falsekey?.must_equal false
View
17 spec/section_spec.rb
@@ -10,28 +10,19 @@
end
it "should detect configy environment" do
- with_const( :CONFIGY_ENV, "staging" ) do
+ with_env( "CONFIGY_ENV", "staging" ) do
Configy.section.must_equal "staging"
end
end
- it "should detect rails 3 environment" do
- rails3_obj = MiniTest::Mock.new
- rails3_obj.expect :env, "staging"
-
- with_const(:Rails, rails3_obj) do
- Configy.section.must_equal "staging"
- end
- end
-
- it "should detect rails 2 environment" do
- with_const( :RAILS_ENV, "test" ) do
+ it "should detect rails environment" do
+ with_env( "RAILS_ENV", "test" ) do
Configy.section.must_equal "test"
end
end
it "should detect rack environment" do
- with_const( :RACK_ENV, "production" ) do
+ with_env( "RACK_ENV", "production" ) do
Configy.section.must_equal "production"
end
end
View
6 spec/spec_helper.rb
@@ -35,6 +35,12 @@ def with_const(const, value)
Object.send :remove_const, const
end
+ def with_env(name, value)
+ old = ENV[name]
+ ENV[name] = value
+ yield
+ ENV[name] = old
+ end
end
class MiniTest::Unit::TestCase

No commit comments for this range

Something went wrong with that request. Please try again.