diff --git a/lib/geocoder/configuration.rb b/lib/geocoder/configuration.rb index 3381fb183137bd2d39f7109623c694efa71f446d..3e4b80816d79aceb4e9b2f7067f7248d361e83a9 100644 --- a/lib/geocoder/configuration.rb +++ b/lib/geocoder/configuration.rb @@ -1,57 +1,49 @@ +require 'singleton' + module Geocoder class Configuration - - def self.options_and_defaults - [ - # geocoding service timeout (secs) - [:timeout, 3], - - # name of geocoding service (symbol) - [:lookup, :google], - - # ISO-639 language code - [:language, :en], - - # use HTTPS for lookup requests? (if supported) - [:use_https, false], - - # HTTP proxy server (user:pass@host:port) - [:http_proxy, nil], - - # HTTPS proxy server (user:pass@host:port) - [:https_proxy, nil], - - # API key for geocoding service - [:api_key, nil], - - # cache object (must respond to #[], #[]=, and #keys) - [:cache, nil], - - # prefix (string) to use for all cache keys - [:cache_prefix, "geocoder:"], - - # exceptions that should not be rescued by default - # (if you want to implement custom error handling); - # supports SocketError and TimeoutError - [:always_raise, []] - ] + include Singleton + + CONFIGURABLE = [:timeout , :lookup , :language , + :use_https , :http_proxy, :https_proxy , + :api_key , :cache , :cache_prefix, + :always_raise, :units , :method ] + + attr_accessor *CONFIGURABLE + + def initialize + @timeout = 3 # geocoding service timeout (secs) + @lookup = :google # name of geocoding service (symbol) + @language = :en # ISO-639 language code + @use_https = false # use HTTPS for lookup requests? (if supported) + @http_proxy = nil # HTTP proxy server (user:pass@host:port) + @https_proxy = nil # HTTPS proxy server (user:pass@host:port) + @api_key = nil # API key for geocoding service + @cache = nil # cache object (must respond to #[], #[]=, and #keys) + @cache_prefix = "geocoder:" # prefix (string) to use for all cache keys + # exceptions that should not be rescued by default + # (if you want to implement custom error handling); + # supports SocketError and TimeoutError + @always_raise = [] + + # Calculation options + @units = :km # Internationl System standard unit for distance + @method = :spherical # More precise 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") - end + # delegates getters and setters for all configuration settings to the instance + instance_eval(CONFIGURABLE.map do |method| + meth = method.to_s + <<-EOS + def #{meth} + instance.#{meth} + end - ## - # Set all values to default. - # - def self.set_defaults - self.options_and_defaults.each do |o,d| - self.send("#{o}=", d) + def #{meth}=(value) + instance.#{meth} = value end - end + EOS + end.join("\n\n")) end end -Geocoder::Configuration.set_defaults