Skip to content
This repository
Browse code

Merge pull request #27 from wpeterson/master

Bundler and Suppressing Errors
  • Loading branch information...
commit ff02a801f72ab9ed7659fa34ee23039b79a569ae 2 parents 4519763 + f108bda
Ben Johnson yourewelcome authored
3  .gitignore
@@ -5,4 +5,5 @@ pkg/*
5 5 coverage/*
6 6 doc/*
7 7 benchmarks/*
8   -
  8 +.bundle
  9 +vendor/bundle
6 Gemfile
... ... @@ -0,0 +1,6 @@
  1 +source "http://rubygems.org"
  2 +
  3 +gem 'rake', '0.8.7'
  4 +gem 'jeweler'
  5 +gem 'rspec', :require => 'spec'
  6 +gem 'ruby-debug'
36 Gemfile.lock
... ... @@ -0,0 +1,36 @@
  1 +GEM
  2 + remote: http://rubygems.org/
  3 + specs:
  4 + columnize (0.3.6)
  5 + diff-lcs (1.1.3)
  6 + git (1.2.5)
  7 + jeweler (1.6.4)
  8 + bundler (~> 1.0)
  9 + git (>= 1.2.5)
  10 + rake
  11 + linecache (0.46)
  12 + rbx-require-relative (> 0.0.4)
  13 + rake (0.8.7)
  14 + rbx-require-relative (0.0.5)
  15 + rspec (2.8.0)
  16 + rspec-core (~> 2.8.0)
  17 + rspec-expectations (~> 2.8.0)
  18 + rspec-mocks (~> 2.8.0)
  19 + rspec-core (2.8.0)
  20 + rspec-expectations (2.8.0)
  21 + diff-lcs (~> 1.1.2)
  22 + rspec-mocks (2.8.0)
  23 + ruby-debug (0.10.4)
  24 + columnize (>= 0.1)
  25 + ruby-debug-base (~> 0.10.4.0)
  26 + ruby-debug-base (0.10.4)
  27 + linecache (>= 0.3)
  28 +
  29 +PLATFORMS
  30 + ruby
  31 +
  32 +DEPENDENCIES
  33 + jeweler
  34 + rake (= 0.8.7)
  35 + rspec
  36 + ruby-debug
16 README.rdoc
Source Rendered
@@ -114,6 +114,22 @@ Modifying our model example:
114 114 This would allow you to specify a custom value for per_page just for posts, or
115 115 to fall back to your default value if not specified.
116 116
  117 +=== 5. Suppressing Exceptions Conditionally
  118 +
  119 +Raising exceptions for missing settings helps highlight configuration problems. However, in a
  120 +Rails app it may make sense to suppress this in production and return nil for missing settings.
  121 +While it's useful to stop and highlight an error in development or test environments, this is
  122 +often not the right answer for production.
  123 +
  124 + class Settings < Settingslogic
  125 + source "#{Rails.root}/config/application.yml"
  126 + namespace Rails.env
  127 + suppress_errors Rails.env.production?
  128 + end
  129 +
  130 + >> Settings.non_existent_key
  131 + => nil
  132 +
117 133 == Note on Sinatra / Capistrano / Vlad
118 134
119 135 Each of these frameworks uses a +set+ convention for settings, which actually defines methods
22 lib/settingslogic.rb
@@ -35,7 +35,15 @@ def namespace(value = nil)
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 @@ def initialize(hash_or_file = self.class.source, section = nil)
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 @@ def initialize(hash_or_file = self.class.source, section = nil)
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 @@ def create_accessor_for(key, val=nil)
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
4 spec/settings4.rb
... ... @@ -0,0 +1,4 @@
  1 +class Settings4 < Settingslogic
  2 + source "#{File.dirname(__FILE__)}/settings.yml"
  3 + suppress_errors true
  4 +end
4 spec/settingslogic_spec.rb
@@ -107,6 +107,10 @@ class NoSource < Settingslogic; end
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.
7 spec/spec_helper.rb
... ... @@ -1,18 +1,15 @@
1   -require 'spec'
2   -require 'rubygems'
3   -require 'ruby-debug' if RUBY_VERSION < '1.9' # ruby-debug does not work on 1.9.1 yet
4   -
5 1 $LOAD_PATH.unshift(File.dirname(__FILE__))
6 2 $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
7 3 require 'settingslogic'
8 4 require 'settings'
9 5 require 'settings2'
10 6 require 'settings3'
  7 +require 'settings4'
11 8
12 9 # Needed to test Settings3
13 10 Object.send :define_method, 'collides' do
14 11 'collision'
15 12 end
16 13
17   -Spec::Runner.configure do |config|
  14 +RSpec.configure do |config|
18 15 end

0 comments on commit ff02a80

Please sign in to comment.
Something went wrong with that request. Please try again.