Commit 6be535aa74f68f35c88109b32fd2038e9819721d
Exists in
master
Merge pull request #40 from craigmarksmith/master
Handle empty yaml files.
Showing
5 changed files
with
10 additions
and
1 deletions
Show diff stats
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/settingslogic_spec.rb
@@ -153,6 +153,10 @@ describe "Settingslogic" do | @@ -153,6 +153,10 @@ describe "Settingslogic" 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 'settings' | @@ -6,6 +6,7 @@ require 'settings' | ||
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 |