Merge pull request #199 from stjhimy/config_erb

Allow erb syntax on yml config file
This commit is contained in:
Zachary Scott 2016-07-20 20:02:36 +09:00 committed by GitHub
commit 5a4cba9307
4 changed files with 24 additions and 3 deletions

View File

@ -128,9 +128,9 @@ module Sinatra
Dir.chdir(root || '.') do
paths.each do |pattern|
Dir.glob(pattern) do |file|
raise UnsupportedConfigType unless ['.yml', '.erb'].include?(File.extname(file))
$stderr.puts "loading config file '#{file}'" if logging?
document = IO.read(file)
document = ERB.new(document).result if file.split('.').include?('erb')
document = ERB.new(IO.read(file)).result
yaml = config_for_env(YAML.load(document)) || {}
yaml.each_pair do |key, value|
for_env = config_for_env(value)
@ -141,6 +141,12 @@ module Sinatra
end
end
class UnsupportedConfigType < Exception
def message
'Invalid config file type, use .yml or .yml.erb'
end
end
private
# Given a +hash+ with some application configuration it returns the

View File

@ -1,5 +1,6 @@
---
foo: bar
bar: <%= "bar" %>
something: 42
nested:
a: 1

View File

@ -22,7 +22,17 @@ describe Sinatra::ConfigFile do
expect(settings.nested[:a]).to eq(1)
end
it 'should render options in ERB tags' do
it 'should render options in ERB tags when using .yml files' do
config_file 'key_value.yml'
settings.bar.should == "bar"
settings.something.should == 42
settings.nested['a'].should == 1
settings.nested[:a].should == 1
settings.nested['b'].should == 2
settings.nested[:b].should == 2
end
it 'should render options in ERB tags when using .yml.erb files' do
config_file 'key_value.yml.erb'
expect(settings.foo).to eq("bar")
expect(settings.something).to eq(42)
@ -32,6 +42,10 @@ describe Sinatra::ConfigFile do
expect(settings.nested[:b]).to eq(2)
end
it 'should raise error if config file extension is not .yml or .erb' do
expect{ config_file 'config.txt' }.to raise_error(Sinatra::ConfigFile::UnsupportedConfigType)
end
it 'should recognize env specific settings per file' do
config_file 'with_envs.yml'
expect(settings.foo).to eq('test')