Skip to content

Commit

Permalink
Making it possible to have any environment without having a configura…
Browse files Browse the repository at this point in the history
…tion section for it. Still erroring out when a parent does not exist.
  • Loading branch information
amikula committed Jul 1, 2011
1 parent fa6c11e commit e93536f
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 19 deletions.
24 changes: 13 additions & 11 deletions lib/yacht/loader.rb
Expand Up @@ -85,19 +85,21 @@ def load_config_file(file_type, opts={})
end

def chain_configs(config, env)
raise Yacht::LoadError.new "environment '#{env}' does not exist" unless config.has_key?(env)

parent = if config[env]['_parent']
chain_configs(config, config[env]['_parent'])
else
config['default'] || {}
end

parent.deep_merge(config[env])
if config.has_key?(env)
parent = if parent_env = config[env]['_parent']
raise Yacht::LoadError.new "environment '#{parent_env}' does not exist" unless config.has_key?(parent_env)
chain_configs(config, config[env]['_parent'])
else
config['default'] || {}
end

parent.deep_merge(config[env])
else
config['default'] || {}
end
end
end

end

# Alias for Yacht::Loader for backwards compatibility
Object.const_set(:YachtLoader, Yacht::Loader)
Object.const_set(:YachtLoader, Yacht::Loader)
54 changes: 46 additions & 8 deletions spec/yacht/loader_spec.rb
Expand Up @@ -38,13 +38,6 @@
subject.to_hash(:env => 'wacky').should == mock_base_config['wacky']
end

it "raises an error if an environment is requested that doesn't exist" do
expect {
subject.environment = 'nonexistent'
subject.to_hash
}.to raise_error( Yacht::LoadError, /does not exist/)
end

context "with inheritance" do
let(:mock_base_config_with_inheritance) do
{
Expand Down Expand Up @@ -87,6 +80,36 @@
}
end
end

context "with inheritance when a parent is missing" do
let(:mock_base_config_with_inheritance) do
{
'default' => {
:doggies => {
'lassie' => 'nice',
'cujo' => 'mean'
}
},
'kid' => {
'_parent' => 'deadbeat_dad',
:doggies => {
'cerberus' => '3-headed'
}
}
}
end

before do
subject.stub(:base_config).and_return(mock_base_config_with_inheritance)
subject.environment = 'kid'
end

it "raises an error if an environment is requested whose parent doesn't exist" do
expect {
subject.to_hash
}.to raise_error( Yacht::LoadError, /does not exist/)
end
end
end

describe :all do
Expand All @@ -104,6 +127,21 @@
subject.environment = 'wacky'
subject.to_hash[:color_of_the_day].should == 'purple'
end

it "returns the defaults for environments that do not exist" do
subject.stub(:base_config).and_return({
'default' => {
:color_of_the_day => 'orange',
},
'wacky' => {
:color_of_the_day => 'purple',
}
})
subject.stub(:local_config).and_return({})

subject.environment = 'nerdy'
subject.to_hash.should == {:color_of_the_day => 'orange'}
end
end

describe :base_config do
Expand Down Expand Up @@ -246,4 +284,4 @@
it "should alias YachtLoader to Yacht::Loader for backwards compatibility" do
::YachtLoader.should == Yacht::Loader
end
end
end

0 comments on commit e93536f

Please sign in to comment.