diff --git a/lib/yacht/loader.rb b/lib/yacht/loader.rb index 18ecccc..13b4b21 100644 --- a/lib/yacht/loader.rb +++ b/lib/yacht/loader.rb @@ -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) \ No newline at end of file +Object.const_set(:YachtLoader, Yacht::Loader) diff --git a/spec/yacht/loader_spec.rb b/spec/yacht/loader_spec.rb index b9f4992..91a538b 100644 --- a/spec/yacht/loader_spec.rb +++ b/spec/yacht/loader_spec.rb @@ -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 { @@ -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 @@ -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 @@ -246,4 +284,4 @@ it "should alias YachtLoader to Yacht::Loader for backwards compatibility" do ::YachtLoader.should == Yacht::Loader end -end \ No newline at end of file +end