From 89d02171133e545ce17bb995e5a7e1e984a5ad35 Mon Sep 17 00:00:00 2001
From: James McCarthy <james2mccarthy@gmail.com>
Date: Thu, 21 Jul 2011 15:35:01 +0100
Subject: [PATCH] Made Configuration set defaults when the Configuration class
 loads and removed use of eval. Removed set_defaults from all tests and
 replaced with teardown to reset the defaults.

Signed-off-by: James McCarthy <james2mccarthy@gmail.com>
---
 lib/geocoder/configuration.rb | 29 +++++++++++++++--------------
 test/configuration_test.rb    |  1 +
 test/custom_block_test.rb     |  4 ----
 test/error_handling_test.rb   |  4 ----
 test/geocoder_test.rb         |  4 ----
 test/https_test.rb            |  4 ----
 test/method_aliases_test.rb   |  4 ----
 test/mongoid_test.rb          |  4 ----
 test/proxy_test.rb            |  4 ----
 test/services_test.rb         |  4 ----
 test/test_helper.rb           |  6 ++++++
 11 files changed, 22 insertions(+), 46 deletions(-)

diff --git a/lib/geocoder/configuration.rb b/lib/geocoder/configuration.rb
index 3381fb18..75e7c1b3 100644
--- a/lib/geocoder/configuration.rb
+++ b/lib/geocoder/configuration.rb
@@ -38,20 +38,21 @@ module Geocoder
     end
 
     # define getters and setters for all configuration settings
-    self.options_and_defaults.each do |o,d|
-      eval("def self.#{o}; @@#{o}; end")
-      eval("def self.#{o}=(obj); @@#{o} = obj; end")
+    self.options_and_defaults.each do |option, default|
+      class_eval(<<-END, __FILE__, __LINE__ + 1)
+      
+        @@#{option} = default unless defined? @@#{option}
+        
+        def self.#{option}
+          @@#{option}
+        end
+        
+        def self.#{option}=(obj)
+          @@#{option} = obj
+        end
+      
+      END
     end
 
-    ##
-    # Set all values to default.
-    #
-    def self.set_defaults
-      self.options_and_defaults.each do |o,d|
-        self.send("#{o}=", d)
-      end
-    end
   end
-end
-
-Geocoder::Configuration.set_defaults
+end
\ No newline at end of file
diff --git a/test/configuration_test.rb b/test/configuration_test.rb
index 61593c6b..cece36b7 100644
--- a/test/configuration_test.rb
+++ b/test/configuration_test.rb
@@ -9,4 +9,5 @@ class ConfigurationTest < Test::Unit::TestCase
       Geocoder.search "something dumb"
     end
   end
+  
 end
diff --git a/test/custom_block_test.rb b/test/custom_block_test.rb
index ab2ad9e0..8fab62e8 100644
--- a/test/custom_block_test.rb
+++ b/test/custom_block_test.rb
@@ -3,10 +3,6 @@ require 'test_helper'
 
 class CustomBlockTest < Test::Unit::TestCase
 
-  def setup
-    Geocoder::Configuration.set_defaults
-  end
-
   def test_geocode_with_block_runs_block
     e = Event.new(*venue_params(:msg))
     coords = [40.750354, -73.993371]
diff --git a/test/error_handling_test.rb b/test/error_handling_test.rb
index e60c7b39..134e05a6 100644
--- a/test/error_handling_test.rb
+++ b/test/error_handling_test.rb
@@ -3,10 +3,6 @@ require 'test_helper'
 
 class ErrorHandlingTest < Test::Unit::TestCase
 
-  def setup
-    Geocoder::Configuration.set_defaults
-  end
-
   def test_does_not_choke_on_timeout
     # keep test output clean: suppress timeout warning
     orig = $VERBOSE; $VERBOSE = nil
diff --git a/test/geocoder_test.rb b/test/geocoder_test.rb
index 2a0b0246..fe168200 100644
--- a/test/geocoder_test.rb
+++ b/test/geocoder_test.rb
@@ -3,10 +3,6 @@ require 'test_helper'
 
 class GeocoderTest < Test::Unit::TestCase
 
-  def setup
-    Geocoder::Configuration.set_defaults
-  end
-
   def test_distance_to_returns_float
     v = Venue.new(*venue_params(:msg))
     v.latitude, v.longitude = [40.750354, -73.993371]
diff --git a/test/https_test.rb b/test/https_test.rb
index acba01e6..58219d33 100644
--- a/test/https_test.rb
+++ b/test/https_test.rb
@@ -3,10 +3,6 @@ require 'test_helper'
 
 class HttpsTest < Test::Unit::TestCase
 
-  def setup
-    Geocoder::Configuration.set_defaults
-  end
-
   def test_uses_https_for_secure_query
     Geocoder::Configuration.use_https = true
     g = Geocoder::Lookup::Google.new
diff --git a/test/method_aliases_test.rb b/test/method_aliases_test.rb
index f44c3478..993e9676 100644
--- a/test/method_aliases_test.rb
+++ b/test/method_aliases_test.rb
@@ -3,10 +3,6 @@ require 'test_helper'
 
 class MethodAliasesTest < Test::Unit::TestCase
 
-  def setup
-    Geocoder::Configuration.set_defaults
-  end
-
   def test_distance_from_is_alias_for_distance_to
     v = Venue.new(*venue_params(:msg))
     v.latitude, v.longitude = [40.750354, -73.993371]
diff --git a/test/mongoid_test.rb b/test/mongoid_test.rb
index c4fb8ae0..44b4ca9a 100644
--- a/test/mongoid_test.rb
+++ b/test/mongoid_test.rb
@@ -7,10 +7,6 @@ require 'mongoid_test_helper'
 
 class MongoidTest < Test::Unit::TestCase
 
-  def setup
-    Geocoder::Configuration.set_defaults
-  end
-
   def test_geocoded_check
     p = Place.new(*venue_params(:msg))
     p.location = [40.750354, -73.993371]
diff --git a/test/proxy_test.rb b/test/proxy_test.rb
index fe423605..d701fdce 100644
--- a/test/proxy_test.rb
+++ b/test/proxy_test.rb
@@ -3,10 +3,6 @@ require 'test_helper'
 
 class ProxyTest < Test::Unit::TestCase
 
-  def setup
-    Geocoder::Configuration.set_defaults
-  end
-
   def test_uses_proxy_when_specified
     Geocoder::Configuration.http_proxy = 'localhost'
     lookup = Geocoder::Lookup::Google.new
diff --git a/test/services_test.rb b/test/services_test.rb
index 5eb7555c..33941b30 100644
--- a/test/services_test.rb
+++ b/test/services_test.rb
@@ -3,10 +3,6 @@ require 'test_helper'
 
 class ServicesTest < Test::Unit::TestCase 
 
-  def setup
-    Geocoder::Configuration.set_defaults
-  end
-
 
   # --- Google ---
 
diff --git a/test/test_helper.rb b/test/test_helper.rb
index f491504a..bbe1a148 100644
--- a/test/test_helper.rb
+++ b/test/test_helper.rb
@@ -224,6 +224,12 @@ end
 
 
 class Test::Unit::TestCase
+  
+  def teardown
+    Geocoder.send(:remove_const, :Configuration)
+    load "geocoder/configuration.rb"
+  end
+  
   def venue_params(abbrev)
     {
       :msg => ["Madison Square Garden", "4 Penn Plaza, New York, NY"]
-- 
GitLab