Skip to content
Snippets Groups Projects
Commit 7276de0b authored by Anderson Bravalheri's avatar Anderson Bravalheri
Browse files

Configurations changed to provide a DSL (similar to Rails) to configure...

Configurations changed to provide a DSL (similar to Rails) to configure Geocoder. A singleton pattern was used to allow the code *Geocoder.configure { |config| ... }*, and delegation was used to maintain compatibility. New configuration options (units and method) for calculations were implemented, with default value: km and spherical, respectively.
parent df0ee956
No related branches found
No related tags found
No related merge requests found
require 'singleton'
module Geocoder module Geocoder
class Configuration class Configuration
include Singleton
def self.options_and_defaults
[ CONFIGURABLE = [:timeout , :lookup , :language ,
# geocoding service timeout (secs) :use_https , :http_proxy, :https_proxy ,
[:timeout, 3], :api_key , :cache , :cache_prefix,
:always_raise, :units , :method ]
# name of geocoding service (symbol)
[:lookup, :google], attr_accessor *CONFIGURABLE
# ISO-639 language code def initialize
[:language, :en], @timeout = 3 # geocoding service timeout (secs)
@lookup = :google # name of geocoding service (symbol)
# use HTTPS for lookup requests? (if supported) @language = :en # ISO-639 language code
[:use_https, false], @use_https = false # use HTTPS for lookup requests? (if supported)
@http_proxy = nil # HTTP proxy server (user:pass@host:port)
# HTTP proxy server (user:pass@host:port) @https_proxy = nil # HTTPS proxy server (user:pass@host:port)
[:http_proxy, nil], @api_key = nil # API key for geocoding service
@cache = nil # cache object (must respond to #[], #[]=, and #keys)
# HTTPS proxy server (user:pass@host:port) @cache_prefix = "geocoder:" # prefix (string) to use for all cache keys
[: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 # exceptions that should not be rescued by default
# (if you want to implement custom error handling); # (if you want to implement custom error handling);
# supports SocketError and TimeoutError # supports SocketError and TimeoutError
[:always_raise, []] @always_raise = []
]
end
# define getters and setters for all configuration settings # Calculation options
self.options_and_defaults.each do |o,d| @units = :km # Internationl System standard unit for distance
eval("def self.#{o}; @@#{o}; end") @method = :spherical # More precise
eval("def self.#{o}=(obj); @@#{o} = obj; end")
end end
## # delegates getters and setters for all configuration settings to the instance
# Set all values to default. instance_eval(CONFIGURABLE.map do |method|
# meth = method.to_s
def self.set_defaults <<-EOS
self.options_and_defaults.each do |o,d| def #{meth}
self.send("#{o}=", d) instance.#{meth}
end end
def #{meth}=(value)
instance.#{meth} = value
end end
EOS
end.join("\n\n"))
end end
end end
Geocoder::Configuration.set_defaults
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment