From 6254489bd84ddeb2e380722aa1103ed507e24b83 Mon Sep 17 00:00:00 2001 From: Winfield Peterson Date: Thu, 5 Jan 2012 15:46:59 -0500 Subject: [PATCH] Adds ability to suppress all errors. --- lib/settingslogic.rb | 22 ++++++++++++++++++---- spec/settings4.rb | 4 ++++ spec/settingslogic_spec.rb | 4 ++++ spec/spec_helper.rb | 1 + 4 files changed, 27 insertions(+), 4 deletions(-) create mode 100644 spec/settings4.rb diff --git a/lib/settingslogic.rb b/lib/settingslogic.rb index 022b2f2..cd932e7 100644 --- a/lib/settingslogic.rb +++ b/lib/settingslogic.rb @@ -35,7 +35,15 @@ class Settingslogic < Hash @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 @@ class Settingslogic < Hash 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 @@ class Settingslogic < Hash # 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 @@ class Settingslogic < Hash 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 diff --git a/spec/settings4.rb b/spec/settings4.rb new file mode 100644 index 0000000..66f618e --- /dev/null +++ b/spec/settings4.rb @@ -0,0 +1,4 @@ +class Settings4 < Settingslogic + source "#{File.dirname(__FILE__)}/settings.yml" + suppress_errors true +end \ No newline at end of file diff --git a/spec/settingslogic_spec.rb b/spec/settingslogic_spec.rb index e95a0e6..261eb7e 100644 --- a/spec/settingslogic_spec.rb +++ b/spec/settingslogic_spec.rb @@ -107,6 +107,10 @@ describe "Settingslogic" do 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. diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 2b12058..366d6d2 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -4,6 +4,7 @@ require 'settingslogic' require 'settings' require 'settings2' require 'settings3' +require 'settings4' # Needed to test Settings3 Object.send :define_method, 'collides' do -- libgit2 0.21.0