Commit 6254489bd84ddeb2e380722aa1103ed507e24b83

Authored by Winfield Peterson
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.
lib/settingslogic.rb
... ... @@ -35,7 +35,15 @@ class Settingslogic < Hash
35 35 @namespace = value
36 36 end
37 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 47 def [](key)
40 48 instance.fetch(key.to_s, nil)
41 49 end
... ... @@ -104,7 +112,7 @@ class Settingslogic < Hash
104 112 else
105 113 hash = YAML.load(ERB.new(File.read(hash_or_file)).result).to_hash
106 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 116 end
109 117 self.replace hash
110 118 end
... ... @@ -116,7 +124,7 @@ class Settingslogic < Hash
116 124 # Otherwise, create_accessors! (called by new) will have created actual methods for each key.
117 125 def method_missing(name, *args, &block)
118 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 128 value = fetch(key)
121 129 create_accessor_for(key)
122 130 value.is_a?(Hash) ? self.class.new(value, "'#{key}' section in #{@section}") : value
... ... @@ -152,10 +160,16 @@ class Settingslogic < Hash
152 160 self.class.class_eval <<-EndEval
153 161 def #{key}
154 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 164 value = fetch('#{key}')
157 165 @#{key} = value.is_a?(Hash) ? self.class.new(value, "'#{key}' section in #{@section}") : value
158 166 end
159 167 EndEval
160 168 end
  169 +
  170 + def missing_key(msg)
  171 + return nil if self.class.suppress_errors
  172 +
  173 + raise MissingSetting, msg
  174 + end
161 175 end
... ...
spec/settings4.rb 0 → 100644
... ... @@ -0,0 +1,4 @@
  1 +class Settings4 < Settingslogic
  2 + source "#{File.dirname(__FILE__)}/settings.yml"
  3 + suppress_errors true
  4 +end
0 5 \ No newline at end of file
... ...
spec/settingslogic_spec.rb
... ... @@ -107,6 +107,10 @@ describe &quot;Settingslogic&quot; do
107 107 e.should_not be_nil
108 108 end
109 109  
  110 + it "should allow suppressing errors" do
  111 + Settings4.non_existent_key.should be_nil
  112 + end
  113 +
110 114 # This one edge case currently does not pass, because it requires very
111 115 # esoteric code in order to make it pass. It was judged not worth fixing,
112 116 # as it introduces significant complexity for minor gain.
... ...
spec/spec_helper.rb
... ... @@ -4,6 +4,7 @@ require &#39;settingslogic&#39;
4 4 require 'settings'
5 5 require 'settings2'
6 6 require 'settings3'
  7 +require 'settings4'
7 8  
8 9 # Needed to test Settings3
9 10 Object.send :define_method, 'collides' do
... ...