Skip to content
This repository was archived by the owner on Jul 27, 2020. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.textile
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ Configuration is stored within the @config/environments@ directory of your Rails
app. Files ending in ".yml" are loaded from the @common/@ subdirectory and a
subdirectory named after the current environment.

Configuration files are preprocessed as ERB, in the same way that Rails
preprocesses @config/database.yml@. This allows dynamic configuration,
e.g. @host: <%= ENV["DB_HOST"] || "localhost" %>@.

Each file goes into its own hash in the configuration. For example, if you
placed a file called @memcache.yml@ within @config/environments/development@,
you would be able to access your Memcache timeout using
Expand Down
1 change: 1 addition & 0 deletions lib/configoro.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
raise "Configoro must be used in the context of a Rails 3 application" unless defined?(Rails)

require 'erb'
require 'yaml'
require 'bundler'
Bundler.setup
Expand Down
7 changes: 6 additions & 1 deletion lib/configoro/hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def <<(hsh_or_path)
when String
raise ArgumentError, "Only files ending in .yml can be added" unless File.extname(hsh_or_path) == '.yml'
return self unless File.exist?(hsh_or_path)
data = YAML.load_file(hsh_or_path)
data = load_preprocessed_yaml(hsh_or_path)
deep_merge! File.basename(hsh_or_path, ".yml") => data
when ::Hash
deep_merge! hsh_or_path
Expand Down Expand Up @@ -146,4 +146,9 @@ def remove_getter(meth)

raise NameError, "undefined local variable or method `#{meth}' for #{self.inspect}"
end

def load_preprocessed_yaml(path)
YAML.load(ERB.new(IO.read(path)).result)
end

end
5 changes: 5 additions & 0 deletions spec/configoro/hash_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@
subject << "example.yml"
subject.should be_empty
end

it "should preprocess YAML file as ERB" do
subject << "#{File.dirname __FILE__}/../data/config/environments/common/erb_test.yml"
subject.erb_test.sum.should == 2
end
end

describe "#deep_merge!" do
Expand Down
2 changes: 1 addition & 1 deletion spec/configoro_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ module MyApp; end
it "should not complain when there is no directory for the current environment" do
Rails.stub!(:env).and_return('unknown')
Configoro.initialize
MyApp::Configuration.should eql({"basic"=>{"common_only"=>"common", "env_name"=>"common"}, "hash_test"=>{"akey"=>"value", "subhash"=>{"key1"=>"val1", "key2"=>"val2"}}})
MyApp::Configuration.should eql({"basic"=>{"common_only"=>"common", "env_name"=>"common"}, "erb_test" => {"sum" => 2}, "hash_test"=>{"akey"=>"value", "subhash"=>{"key1"=>"val1", "key2"=>"val2"}}})
end
end
end
2 changes: 2 additions & 0 deletions spec/data/config/environments/common/erb_test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
sum: <%= 1 + 1 %>