Commit 6be535aa74f68f35c88109b32fd2038e9819721d

Authored by Ben Johnson
2 parents 4884d455 2e1a1cd2
Exists in master

Merge pull request #40 from craigmarksmith/master

Handle empty yaml files.
lib/settingslogic.rb
@@ -111,7 +111,8 @@ class Settingslogic < Hash @@ -111,7 +111,8 @@ class Settingslogic < Hash
111 when Hash 111 when Hash
112 self.replace hash_or_file 112 self.replace hash_or_file
113 else 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 if self.class.namespace 116 if self.class.namespace
116 hash = hash[self.class.namespace] or return missing_key("Missing setting '#{self.class.namespace}' in #{hash_or_file}") 117 hash = hash[self.class.namespace] or return missing_key("Missing setting '#{self.class.namespace}' in #{hash_or_file}")
117 end 118 end
spec/settings_empty.rb 0 → 100644
@@ -0,0 +1,3 @@ @@ -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,6 +153,10 @@ describe &quot;Settingslogic&quot; do
153 Settings.name.should == 'test' 153 Settings.name.should == 'test'
154 end 154 end
155 155
  156 + it "should handle empty file" do
  157 + SettingsEmpty.keys.should eql([])
  158 + end
  159 +
156 # Put this test last or else call to .instance will load @instance, 160 # Put this test last or else call to .instance will load @instance,
157 # masking bugs. 161 # masking bugs.
158 it "should be a hash" do 162 it "should be a hash" do
spec/spec_helper.rb
@@ -6,6 +6,7 @@ require &#39;settings&#39; @@ -6,6 +6,7 @@ require &#39;settings&#39;
6 require 'settings2' 6 require 'settings2'
7 require 'settings3' 7 require 'settings3'
8 require 'settings4' 8 require 'settings4'
  9 +require 'settings_empty'
9 10
10 # Needed to test Settings3 11 # Needed to test Settings3
11 Object.send :define_method, 'collides' do 12 Object.send :define_method, 'collides' do