Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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.
  • Loading branch information...
commit 6254489bd84ddeb2e380722aa1103ed507e24b83 1 parent 8b688ab
Winfield Peterson authored
View
22 lib/settingslogic.rb
@@ -35,7 +35,15 @@ def namespace(value = nil)
@namespace = value
end
end
-
+
+ def suppress_errors(value = nil)
+ if value.nil?
+ @suppress_errors
+ else
+ @suppress_errors = value
+ end
+ end
+
def [](key)
instance.fetch(key.to_s, nil)
end
@@ -104,7 +112,7 @@ def initialize(hash_or_file = self.class.source, section = nil)
else
hash = YAML.load(ERB.new(File.read(hash_or_file)).result).to_hash
if self.class.namespace
- hash = hash[self.class.namespace] or raise MissingSetting, "Missing setting '#{self.class.namespace}' in #{hash_or_file}"
+ hash = hash[self.class.namespace] or return missing_key("Missing setting '#{self.class.namespace}' in #{hash_or_file}")
end
self.replace hash
end
@@ -116,7 +124,7 @@ def initialize(hash_or_file = self.class.source, section = nil)
# Otherwise, create_accessors! (called by new) will have created actual methods for each key.
def method_missing(name, *args, &block)
key = name.to_s
- raise MissingSetting, "Missing setting '#{key}' in #{@section}" unless has_key? key
+ return missing_key("Missing setting '#{key}' in #{@section}") unless has_key? key
value = fetch(key)
create_accessor_for(key)
value.is_a?(Hash) ? self.class.new(value, "'#{key}' section in #{@section}") : value
@@ -152,10 +160,16 @@ def create_accessor_for(key, val=nil)
self.class.class_eval <<-EndEval
def #{key}
return @#{key} if @#{key}
- raise MissingSetting, "Missing setting '#{key}' in #{@section}" unless has_key? '#{key}'
+ return missing_key("Missing setting '#{key}' in #{@section}") unless has_key? '#{key}'
value = fetch('#{key}')
@#{key} = value.is_a?(Hash) ? self.class.new(value, "'#{key}' section in #{@section}") : value
end
EndEval
end
+
+ def missing_key(msg)
+ return nil if self.class.suppress_errors
+
+ raise MissingSetting, msg
+ end
end
View
4 spec/settings4.rb
@@ -0,0 +1,4 @@
+class Settings4 < Settingslogic
+ source "#{File.dirname(__FILE__)}/settings.yml"
+ suppress_errors true
+end
View
4 spec/settingslogic_spec.rb
@@ -107,6 +107,10 @@ class NoSource < Settingslogic; end
e.should_not be_nil
end
+ it "should allow suppressing errors" do
+ Settings4.non_existent_key.should be_nil
+ end
+
# This one edge case currently does not pass, because it requires very
# esoteric code in order to make it pass. It was judged not worth fixing,
# as it introduces significant complexity for minor gain.
View
1  spec/spec_helper.rb
@@ -4,6 +4,7 @@
require 'settings'
require 'settings2'
require 'settings3'
+require 'settings4'
# Needed to test Settings3
Object.send :define_method, 'collides' do
Please sign in to comment.
Something went wrong with that request. Please try again.