Commit ea15363811fad493c90596007b592ed0d7322ae2
Exists in
master
Merge pull request #41 from ebeigarts/fix-falsy-settings
Fix dynamic settings with falsy values
Showing
2 changed files
with
13 additions
and
1 deletions
Show diff stats
lib/settingslogic.rb
@@ -163,7 +163,7 @@ class Settingslogic < Hash | @@ -163,7 +163,7 @@ class Settingslogic < Hash | ||
163 | # http://bmorearty.wordpress.com/2009/01/09/fun-with-rubys-instance_eval-and-class_eval/ | 163 | # http://bmorearty.wordpress.com/2009/01/09/fun-with-rubys-instance_eval-and-class_eval/ |
164 | def create_accessor_for(key, val=nil) | 164 | def create_accessor_for(key, val=nil) |
165 | return unless key.to_s =~ /^\w+$/ # could have "some-setting:" which blows up eval | 165 | return unless key.to_s =~ /^\w+$/ # could have "some-setting:" which blows up eval |
166 | - instance_variable_set("@#{key}", val) if val | 166 | + instance_variable_set("@#{key}", val) |
167 | self.class.class_eval <<-EndEval | 167 | self.class.class_eval <<-EndEval |
168 | def #{key} | 168 | def #{key} |
169 | return @#{key} if @#{key} | 169 | return @#{key} if @#{key} |
spec/settingslogic_spec.rb
@@ -130,6 +130,18 @@ describe "Settingslogic" do | @@ -130,6 +130,18 @@ describe "Settingslogic" do | ||
130 | Settings.language['some-dash-setting#'].should == 'dashtastic' | 130 | Settings.language['some-dash-setting#'].should == 'dashtastic' |
131 | end | 131 | end |
132 | 132 | ||
133 | + it "should handle settings with nil value" do | ||
134 | + Settings["flag"] = true | ||
135 | + Settings["flag"] = nil | ||
136 | + Settings.flag.should == nil | ||
137 | + end | ||
138 | + | ||
139 | + it "should handle settings with false value" do | ||
140 | + Settings["flag"] = true | ||
141 | + Settings["flag"] = false | ||
142 | + Settings.flag.should == false | ||
143 | + end | ||
144 | + | ||
133 | it "should support instance usage as well" do | 145 | it "should support instance usage as well" do |
134 | settings = SettingsInst.new(Settings.source) | 146 | settings = SettingsInst.new(Settings.source) |
135 | settings.setting1.setting1_child.should == "saweet" | 147 | settings.setting1.setting1_child.should == "saweet" |