Skip to content
This repository was archived by the owner on Jul 27, 2020. It is now read-only.

Commit 41543d3

Browse files
committed
Merge pull request #2 from pda/erb-preprocessor
Preprocess YAML files as ERB.
2 parents 3ea822e + 9b8b4c8 commit 41543d3

6 files changed

Lines changed: 19 additions & 2 deletions

File tree

README.textile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ Configuration is stored within the @config/environments@ directory of your Rails
7070
app. Files ending in ".yml" are loaded from the @common/@ subdirectory and a
7171
subdirectory named after the current environment.
7272

73+
Configuration files are preprocessed as ERB, in the same way that Rails
74+
preprocesses @config/database.yml@. This allows dynamic configuration,
75+
e.g. @host: <%= ENV["DB_HOST"] || "localhost" %>@.
76+
7377
Each file goes into its own hash in the configuration. For example, if you
7478
placed a file called @memcache.yml@ within @config/environments/development@,
7579
you would be able to access your Memcache timeout using

lib/configoro.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
raise "Configoro must be used in the context of a Rails 3 application" unless defined?(Rails)
22

3+
require 'erb'
34
require 'yaml'
45
require 'bundler'
56
Bundler.setup

lib/configoro/hash.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def <<(hsh_or_path)
3030
when String
3131
raise ArgumentError, "Only files ending in .yml can be added" unless File.extname(hsh_or_path) == '.yml'
3232
return self unless File.exist?(hsh_or_path)
33-
data = YAML.load_file(hsh_or_path)
33+
data = load_preprocessed_yaml(hsh_or_path)
3434
deep_merge! File.basename(hsh_or_path, ".yml") => data
3535
when ::Hash
3636
deep_merge! hsh_or_path
@@ -146,4 +146,9 @@ def remove_getter(meth)
146146

147147
raise NameError, "undefined local variable or method `#{meth}' for #{self.inspect}"
148148
end
149+
150+
def load_preprocessed_yaml(path)
151+
YAML.load(ERB.new(IO.read(path)).result)
152+
end
153+
149154
end

spec/configoro/hash_spec.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,11 @@
9595
subject << "example.yml"
9696
subject.should be_empty
9797
end
98+
99+
it "should preprocess YAML file as ERB" do
100+
subject << "#{File.dirname __FILE__}/../data/config/environments/common/erb_test.yml"
101+
subject.erb_test.sum.should == 2
102+
end
98103
end
99104

100105
describe "#deep_merge!" do

spec/configoro_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ module MyApp; end
3434
it "should not complain when there is no directory for the current environment" do
3535
Rails.stub!(:env).and_return('unknown')
3636
Configoro.initialize
37-
MyApp::Configuration.should eql({"basic"=>{"common_only"=>"common", "env_name"=>"common"}, "hash_test"=>{"akey"=>"value", "subhash"=>{"key1"=>"val1", "key2"=>"val2"}}})
37+
MyApp::Configuration.should eql({"basic"=>{"common_only"=>"common", "env_name"=>"common"}, "erb_test" => {"sum" => 2}, "hash_test"=>{"akey"=>"value", "subhash"=>{"key1"=>"val1", "key2"=>"val2"}}})
3838
end
3939
end
4040
end
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
---
2+
sum: <%= 1 + 1 %>

0 commit comments

Comments
 (0)