Commit 2e1a1cd2e6d1d803350fc0e6799387848658df4c

Authored by Craig Smith
1 parent 4884d455
Exists in master

Handle empty yaml files.

Sometimes a yaml file could be empty. This change allows settings logic to handle that rather than throw an error.
lib/settingslogic.rb
... ... @@ -111,7 +111,8 @@ class Settingslogic < Hash
111 111 when Hash
112 112 self.replace hash_or_file
113 113 else
114   - hash = YAML.load(ERB.new(open(hash_or_file).read).result).to_hash
  114 + file_contents = open(hash_or_file).read
  115 + hash = file_contents.empty? ? {} : YAML.load(ERB.new(file_contents).result).to_hash
115 116 if self.class.namespace
116 117 hash = hash[self.class.namespace] or return missing_key("Missing setting '#{self.class.namespace}' in #{hash_or_file}")
117 118 end
... ...
spec/settings_empty.rb 0 → 100644
... ... @@ -0,0 +1,3 @@
  1 +class SettingsEmpty < Settingslogic
  2 + source "#{File.dirname(__FILE__)}/settings_empty.yml"
  3 +end
... ...
spec/settings_empty.yml 0 → 100644
spec/settingslogic_spec.rb
... ... @@ -153,6 +153,10 @@ describe &quot;Settingslogic&quot; do
153 153 Settings.name.should == 'test'
154 154 end
155 155  
  156 + it "should handle empty file" do
  157 + SettingsEmpty.keys.should eql([])
  158 + end
  159 +
156 160 # Put this test last or else call to .instance will load @instance,
157 161 # masking bugs.
158 162 it "should be a hash" do
... ...
spec/spec_helper.rb
... ... @@ -6,6 +6,7 @@ require &#39;settings&#39;
6 6 require 'settings2'
7 7 require 'settings3'
8 8 require 'settings4'
  9 +require 'settings_empty'
9 10  
10 11 # Needed to test Settings3
11 12 Object.send :define_method, 'collides' do
... ...