Commit eeadde31f7995f4a2fffa14364a0de5deb37d512

Authored by Nate Wiger
Committed by Ben Johnson
1 parent 872a1032
Exists in master

handling of nil (missing) file specified as source

lib/settingslogic.rb
@@ -9,7 +9,7 @@ class Settingslogic < Hash @@ -9,7 +9,7 @@ class Settingslogic < Hash
9 def name # :nodoc: 9 def name # :nodoc:
10 instance.key?("name") ? instance.name : super 10 instance.key?("name") ? instance.name : super
11 end 11 end
12 - 12 +
13 # Enables Settings.get('nested.key.name') for dynamic access 13 # Enables Settings.get('nested.key.name') for dynamic access
14 def get(key) 14 def get(key)
15 parts = key.split('.') 15 parts = key.split('.')
@@ -97,6 +97,8 @@ class Settingslogic < Hash @@ -97,6 +97,8 @@ class Settingslogic < Hash
97 def initialize(hash_or_file = self.class.source, section = nil) 97 def initialize(hash_or_file = self.class.source, section = nil)
98 #puts "new! #{hash_or_file}" 98 #puts "new! #{hash_or_file}"
99 case hash_or_file 99 case hash_or_file
  100 + when nil
  101 + raise Errno::ENOENT, "No file specified as Settingslogic source"
100 when Hash 102 when Hash
101 self.replace hash_or_file 103 self.replace hash_or_file
102 else 104 else
spec/settingslogic_spec.rb
@@ -96,6 +96,17 @@ describe "Settingslogic" do @@ -96,6 +96,17 @@ describe "Settingslogic" do
96 Settings.toplevel.should == '42' 96 Settings.toplevel.should == '42'
97 end 97 end
98 98
  99 + it "should raise an error on a nil source argument" do
  100 + class NoSource < Settingslogic; end
  101 + e = nil
  102 + begin
  103 + NoSource.foo.bar
  104 + rescue => e
  105 + e.should be_kind_of Errno::ENOENT
  106 + end
  107 + e.should_not be_nil
  108 + end
  109 +
99 # This one edge case currently does not pass, because it requires very 110 # This one edge case currently does not pass, because it requires very
100 # esoteric code in order to make it pass. It was judged not worth fixing, 111 # esoteric code in order to make it pass. It was judged not worth fixing,
101 # as it introduces significant complexity for minor gain. 112 # as it introduces significant complexity for minor gain.