Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Support for defaults in config_file #38

Closed
wants to merge 3 commits into from

4 participants

sinm Trevor Bramble Katrina Owen Konstantin Haase
sinm

No description provided.

sinm

You may not need this as a simple workaround exists.

Trevor Bramble

FYI: Not ignoring this. I see the validity of the change but want to make sure the inversion of "expected environments" to "given environments" doesn't have unexpected (undesirable) consequences.

Thanks for your patience!

Konstantin Haase rkh referenced this pull request from a commit
Katrina Owen kytrinyx Document sharing default settings (ConfigFile).
This addresses #32 and #38.

Issue #32 suggest a change in the `ConfigFile#config_for_env(hash)`
method in order to allow a separate entry to use for shared settings,
while issue #38 provides an implementation of an additional,
non-environment, key `default` which default values can be added to.

It seems like the existing method of sharing settings does not have any
significant disadvantages, and just needed to be explicitly documented.
dbcfa77
Katrina Owen
Owner

@rkh, @TrevorBramble Want to close this, since the existing behavior has been documented?

Konstantin Haase rkh closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 9, 2012
  1. Support for default settings

    Alex Sinm authored
Commits on Mar 11, 2012
This page is out of date. Refresh to see the latest.
23 lib/sinatra/config_file.rb
View
@@ -98,6 +98,16 @@ module Sinatra
#
# set :environments, %w{development test production staging}
#
+ # It is also possible to supply <tt>default</tt> environment:
+ # development:
+ # zoo: zoo
+ # bar: bar
+ # default:
+ # foo: default-foo
+ # bar: default-bar
+ # , that will give <tt>{:zoo => 'zoo', :bar => 'bar', :foo => 'default-foo'}</tt>
+ # in a development environment
+ # and <tt>{:bar => 'default-bar', :foo => 'default-foo'}</tt> otherwise.
module ConfigFile
# When the extension is registered sets the +environments+ setting to the
@@ -133,8 +143,17 @@ def config_file(*paths)
# returned config is a indifferently accessible Hash, which means that you
# can get its values using Strings or Symbols as keys.
def config_for_env(hash)
- if hash.respond_to? :keys and hash.keys.all? { |k| environments.include? k.to_s }
- hash = hash[environment.to_s] || hash[environment.to_sym]
+ if hash.respond_to? :keys
+ hash_test = hash.keys.delete_if {|k| environments.include? k.to_s}
+ if hash_test.empty? or (hash_test.size == 1 && hash_test.last.to_s == 'default')
+ env_hash = hash[environment.to_s] || hash[environment.to_sym]
+ default_hash = hash['default'] || hash[:default]
+ if env_hash.respond_to?(:to_hash) and default_hash.respond_to?(:to_hash)
+ hash = default_hash.to_hash.merge env_hash.to_hash
+ else
+ hash = env_hash || default_hash
+ end
+ end
end
if hash.respond_to? :to_hash
7 spec/config_file/default_merge.yml
View
@@ -0,0 +1,7 @@
+---
+test:
+ zoo: zoo
+ bar: bar
+default:
+ foo: default-foo
+ bar: default-bar
4 spec/config_file/key_value_override_default.yml
View
@@ -0,0 +1,4 @@
+---
+foo:
+ default: default-foo
+ production: production-foo
5 spec/config_file/missing_env_but_default.yml
View
@@ -0,0 +1,5 @@
+---
+foo:
+ production: 10
+ default: 30
+ development: 20
2  spec/config_file/with_envs.yml
View
@@ -1,4 +1,6 @@
---
+default:
+ foo: default
development:
foo: development
production:
11 spec/config_file/with_nested_default.yml
View
@@ -0,0 +1,11 @@
+---
+database:
+ production:
+ adapter: postgresql
+ database: foo_production
+ development:
+ adapter: sqlite
+ database: db/development.db
+ default:
+ adapter: sqlite
+ database: db/test.db
33 spec/config_file_spec.rb
View
@@ -33,6 +33,11 @@ def config_file(*args, &block)
settings.database[:adapter].should == 'sqlite'
end
+ it 'should recognize env specific settings per setting when default clause is in use' do
+ config_file 'with_nested_default.yml'
+ settings.database[:adapter].should == 'sqlite'
+ end
+
it 'should not set present values to nil if the current env is missing' do
# first let's check the test is actually working properly
config_file('missing_env.yml') { set :foo => 42, :environment => :production }
@@ -42,6 +47,15 @@ def config_file(*args, &block)
settings.foo.should == 42
end
+ it 'should set present values if the current env is missing but default clause is here' do
+ # first let's check the test is actually working properly
+ config_file('missing_env_but_default.yml') { set :foo => 42, :environment => :production }
+ settings.foo.should == 10
+ # now test it
+ config_file('missing_env_but_default.yml') { set :foo => 42, :environment => :test }
+ settings.foo.should == 30
+ end
+
it 'should prioritize settings in latter files' do
# first let's check the test is actually working properly
config_file 'key_value.yml'
@@ -50,4 +64,23 @@ def config_file(*args, &block)
config_file 'key_value_override.yml'
settings.foo.should == 'foo'
end
+
+ it 'should prioritize settings in latter files respecting defaults' do
+ config_file 'key_value.yml', 'key_value_override_default.yml'
+ settings.foo.should == 'default-foo'
+ end
+
+ it 'should merge defaults' do
+ config_file('default_merge.yml')
+ settings.foo.should == 'default-foo'
+ settings.bar.should == 'bar'
+ settings.zoo.should == 'zoo'
+ end
+
+ it 'should not merge defaults when it should not' do
+ config_file('default_merge.yml') { set :zoo => 42, :environment => :production }
+ settings.foo.should == 'default-foo'
+ settings.bar.should == 'default-bar'
+ settings.zoo.should == 42
+ end
end
37 spec/namespace_spec.rb
View
@@ -598,6 +598,43 @@ def magic
b.should_not == 'CUSTOM!!!'
end
+ require 'sinatra/multi_route'
+ it 'is not compatible with MultiRoute extension' do
+ proc do
+ mock_app do
+ register(Sinatra::MultiRoute)
+ namespace('/') do
+ get('foo', 'bar') {'no go'}
+ end
+ end
+ end.should raise_error TypeError, "can't convert String into Hash"
+ end
+
+ it 'is not compatible with MultiRoute extension 2' do
+ mock_app do
+ namespace('/') do
+ register(Sinatra::MultiRoute)
+ get('foo', 'bar') {'ok'}
+ end
+ end
+ if verb == :get
+ send(verb, '/foo').body == 'ok'
+ send(verb, '/bar').status.should == 404
+ end
+ end
+
+ it 'is compatible with MultiRoute#route method though' do
+ mock_app do
+ register(Sinatra::MultiRoute)
+ namespace('/') do
+ route(:get, 'COPY', :post, '*') {'ok'}
+ end
+ end
+ if [:get, :post].any?{|v| v==verb}
+ send(verb, '/foo').body == 'ok'
+ end
+ end
+
it 'triggers route_added hook' do
route = nil
extension = Module.new
Something went wrong with that request. Please try again.