Commit 6254489bd84ddeb2e380722aa1103ed507e24b83
1 parent
8b688ab3
Exists in
master
Adds ability to suppress all errors.
Errors are not raised when using the [] accessors, but when embedded in a Rails app it only make sense to raise exceptions in dev/test environments and to fail silently in production.
Showing
4 changed files
with
27 additions
and
4 deletions
Show diff stats
lib/settingslogic.rb
@@ -35,7 +35,15 @@ class Settingslogic < Hash | @@ -35,7 +35,15 @@ class Settingslogic < Hash | ||
35 | @namespace = value | 35 | @namespace = value |
36 | end | 36 | end |
37 | end | 37 | end |
38 | - | 38 | + |
39 | + def suppress_errors(value = nil) | ||
40 | + if value.nil? | ||
41 | + @suppress_errors | ||
42 | + else | ||
43 | + @suppress_errors = value | ||
44 | + end | ||
45 | + end | ||
46 | + | ||
39 | def [](key) | 47 | def [](key) |
40 | instance.fetch(key.to_s, nil) | 48 | instance.fetch(key.to_s, nil) |
41 | end | 49 | end |
@@ -104,7 +112,7 @@ class Settingslogic < Hash | @@ -104,7 +112,7 @@ class Settingslogic < Hash | ||
104 | else | 112 | else |
105 | hash = YAML.load(ERB.new(File.read(hash_or_file)).result).to_hash | 113 | hash = YAML.load(ERB.new(File.read(hash_or_file)).result).to_hash |
106 | if self.class.namespace | 114 | if self.class.namespace |
107 | - hash = hash[self.class.namespace] or raise MissingSetting, "Missing setting '#{self.class.namespace}' in #{hash_or_file}" | 115 | + hash = hash[self.class.namespace] or return missing_key("Missing setting '#{self.class.namespace}' in #{hash_or_file}") |
108 | end | 116 | end |
109 | self.replace hash | 117 | self.replace hash |
110 | end | 118 | end |
@@ -116,7 +124,7 @@ class Settingslogic < Hash | @@ -116,7 +124,7 @@ class Settingslogic < Hash | ||
116 | # Otherwise, create_accessors! (called by new) will have created actual methods for each key. | 124 | # Otherwise, create_accessors! (called by new) will have created actual methods for each key. |
117 | def method_missing(name, *args, &block) | 125 | def method_missing(name, *args, &block) |
118 | key = name.to_s | 126 | key = name.to_s |
119 | - raise MissingSetting, "Missing setting '#{key}' in #{@section}" unless has_key? key | 127 | + return missing_key("Missing setting '#{key}' in #{@section}") unless has_key? key |
120 | value = fetch(key) | 128 | value = fetch(key) |
121 | create_accessor_for(key) | 129 | create_accessor_for(key) |
122 | value.is_a?(Hash) ? self.class.new(value, "'#{key}' section in #{@section}") : value | 130 | value.is_a?(Hash) ? self.class.new(value, "'#{key}' section in #{@section}") : value |
@@ -152,10 +160,16 @@ class Settingslogic < Hash | @@ -152,10 +160,16 @@ class Settingslogic < Hash | ||
152 | self.class.class_eval <<-EndEval | 160 | self.class.class_eval <<-EndEval |
153 | def #{key} | 161 | def #{key} |
154 | return @#{key} if @#{key} | 162 | return @#{key} if @#{key} |
155 | - raise MissingSetting, "Missing setting '#{key}' in #{@section}" unless has_key? '#{key}' | 163 | + return missing_key("Missing setting '#{key}' in #{@section}") unless has_key? '#{key}' |
156 | value = fetch('#{key}') | 164 | value = fetch('#{key}') |
157 | @#{key} = value.is_a?(Hash) ? self.class.new(value, "'#{key}' section in #{@section}") : value | 165 | @#{key} = value.is_a?(Hash) ? self.class.new(value, "'#{key}' section in #{@section}") : value |
158 | end | 166 | end |
159 | EndEval | 167 | EndEval |
160 | end | 168 | end |
169 | + | ||
170 | + def missing_key(msg) | ||
171 | + return nil if self.class.suppress_errors | ||
172 | + | ||
173 | + raise MissingSetting, msg | ||
174 | + end | ||
161 | end | 175 | end |
spec/settingslogic_spec.rb
@@ -107,6 +107,10 @@ describe "Settingslogic" do | @@ -107,6 +107,10 @@ describe "Settingslogic" do | ||
107 | e.should_not be_nil | 107 | e.should_not be_nil |
108 | end | 108 | end |
109 | 109 | ||
110 | + it "should allow suppressing errors" do | ||
111 | + Settings4.non_existent_key.should be_nil | ||
112 | + end | ||
113 | + | ||
110 | # This one edge case currently does not pass, because it requires very | 114 | # This one edge case currently does not pass, because it requires very |
111 | # esoteric code in order to make it pass. It was judged not worth fixing, | 115 | # esoteric code in order to make it pass. It was judged not worth fixing, |
112 | # as it introduces significant complexity for minor gain. | 116 | # as it introduces significant complexity for minor gain. |
spec/spec_helper.rb
@@ -4,6 +4,7 @@ require 'settingslogic' | @@ -4,6 +4,7 @@ require 'settingslogic' | ||
4 | require 'settings' | 4 | require 'settings' |
5 | require 'settings2' | 5 | require 'settings2' |
6 | require 'settings3' | 6 | require 'settings3' |
7 | +require 'settings4' | ||
7 | 8 | ||
8 | # Needed to test Settings3 | 9 | # Needed to test Settings3 |
9 | Object.send :define_method, 'collides' do | 10 | Object.send :define_method, 'collides' do |