Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Handle empty yaml files. #40

Merged
merged 1 commit into from

2 participants

@craigmarksmith

Sometimes the yaml file passed to SettingLogic could be empty. This change handles that rather attempt to convert an empty string to a hash and throwing an error.

Craig Smith Handle empty yaml files.
Sometimes a yaml file could be empty. This change allows settings logic to handle that rather than throw an error.
2e1a1cd
@binarylogic binarylogic merged commit 6be535a into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 23, 2012
  1. Handle empty yaml files.

    Craig Smith authored
    Sometimes a yaml file could be empty. This change allows settings logic to handle that rather than throw an error.
This page is out of date. Refresh to see the latest.
View
3  lib/settingslogic.rb
@@ -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
View
3  spec/settings_empty.rb
@@ -0,0 +1,3 @@
+class SettingsEmpty < Settingslogic
+ source "#{File.dirname(__FILE__)}/settings_empty.yml"
+end
View
0  spec/settings_empty.yml
No changes.
View
4 spec/settingslogic_spec.rb
@@ -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
View
1  spec/spec_helper.rb
@@ -6,6 +6,7 @@
require 'settings2'
require 'settings3'
require 'settings4'
+require 'settings_empty'
# Needed to test Settings3
Object.send :define_method, 'collides' do
Something went wrong with that request. Please try again.