Skip to content

Commit

Permalink
Handle empty yaml files.
Browse files Browse the repository at this point in the history
Sometimes a yaml file could be empty. This change allows settings logic to handle that rather than throw an error.
  • Loading branch information
Craig Smith authored and Craig Smith committed Oct 23, 2012
1 parent 4884d45 commit 2e1a1cd
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/settingslogic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ def initialize(hash_or_file = self.class.source, section = nil)
when Hash
self.replace hash_or_file
else
hash = YAML.load(ERB.new(open(hash_or_file).read).result).to_hash
file_contents = open(hash_or_file).read
hash = file_contents.empty? ? {} : YAML.load(ERB.new(file_contents).result).to_hash
if self.class.namespace
hash = hash[self.class.namespace] or return missing_key("Missing setting '#{self.class.namespace}' in #{hash_or_file}")
end
Expand Down
3 changes: 3 additions & 0 deletions spec/settings_empty.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class SettingsEmpty < Settingslogic
source "#{File.dirname(__FILE__)}/settings_empty.yml"
end
Empty file added spec/settings_empty.yml
Empty file.
4 changes: 4 additions & 0 deletions spec/settingslogic_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@ class NoSource < Settingslogic; end
Settings.name.should == 'test'
end

it "should handle empty file" do
SettingsEmpty.keys.should eql([])
end

# Put this test last or else call to .instance will load @instance,
# masking bugs.
it "should be a hash" do
Expand Down
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
require 'settings2'
require 'settings3'
require 'settings4'
require 'settings_empty'

# Needed to test Settings3
Object.send :define_method, 'collides' do
Expand Down

0 comments on commit 2e1a1cd

Please sign in to comment.