Skip to content

Handle empty yaml files. #40

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 23, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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