add reimplemenation of sinatra-config-file
This commit is contained in:
parent
99029a0b41
commit
ec248a8b5a
|
@ -0,0 +1,40 @@
|
|||
require 'sinatra/base'
|
||||
require 'yaml'
|
||||
|
||||
module Sinatra
|
||||
module ConfigFile
|
||||
def self.registered(base)
|
||||
base.set :environments, %w[test production development]
|
||||
end
|
||||
|
||||
def config_file(*paths)
|
||||
Dir.chdir(root || '.') do
|
||||
paths.each do |pattern|
|
||||
Dir.glob(pattern) do |file|
|
||||
$stderr.puts "loading config file '#{file}'" if logging?
|
||||
yaml = config_for_env(YAML.load_file(file)) || {}
|
||||
yaml.each_pair do |key, value|
|
||||
for_env = config_for_env(value)
|
||||
set key, for_env unless value and for_env.nil? and respond_to? key
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
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]
|
||||
end
|
||||
|
||||
if hash.respond_to? :to_hash
|
||||
indifferent_hash = Hash.new {|hash,key| hash[key.to_s] if Symbol === key }
|
||||
indifferent_hash.merge hash.to_hash
|
||||
else
|
||||
hash
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -0,0 +1,6 @@
|
|||
---
|
||||
foo: bar
|
||||
something: 42
|
||||
nested:
|
||||
a: 1
|
||||
b: 2
|
|
@ -0,0 +1,4 @@
|
|||
---
|
||||
foo:
|
||||
production: 10
|
||||
development: 20
|
|
@ -0,0 +1,7 @@
|
|||
---
|
||||
development:
|
||||
foo: development
|
||||
production:
|
||||
foo: production
|
||||
test:
|
||||
foo: test
|
|
@ -0,0 +1,11 @@
|
|||
---
|
||||
database:
|
||||
production:
|
||||
adapter: postgresql
|
||||
database: foo_production
|
||||
development:
|
||||
adapter: sqlite
|
||||
database: db/development.db
|
||||
test:
|
||||
adapter: sqlite
|
||||
database: db/test.db
|
|
@ -0,0 +1,47 @@
|
|||
require 'backports'
|
||||
require_relative 'spec_helper'
|
||||
|
||||
describe Sinatra::ConfigFile do
|
||||
attr_accessor :settings
|
||||
def config_file(*args, &block)
|
||||
test = self
|
||||
mock_app do
|
||||
test.settings = settings
|
||||
register Sinatra::ConfigFile
|
||||
set :root, File.expand_path('../config_file', __FILE__)
|
||||
instance_eval(&block) if block
|
||||
config_file(*args)
|
||||
end
|
||||
end
|
||||
|
||||
it 'should set options from a simple config_file' do
|
||||
config_file 'key_value.yml'
|
||||
settings.foo.should == 'bar'
|
||||
settings.something.should == 42
|
||||
end
|
||||
|
||||
it 'should create indifferent hashes' do
|
||||
config_file 'key_value.yml'
|
||||
settings.nested['a'].should == 1
|
||||
settings.nested[:a].should == 1
|
||||
end
|
||||
|
||||
it 'should recognize env specific settings per file' do
|
||||
config_file 'with_envs.yml'
|
||||
settings.foo.should == 'test'
|
||||
end
|
||||
|
||||
it 'should recognize env specific settings per setting' do
|
||||
config_file 'with_nested_envs.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 }
|
||||
settings.foo.should == 10
|
||||
# now test it
|
||||
config_file('missing_env.yml') { set :foo => 42, :environment => :test }
|
||||
settings.foo.should == 42
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue