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], # exceptions that should not be rescued by default
# (if you want to implement custom error handling);
# API key for geocoding service # supports SocketError and TimeoutError
[:api_key, nil], @always_raise = []
# cache object (must respond to #[], #[]=, and #keys) # Calculation options
[:cache, nil], @units = :km # Internationl System standard unit for distance
@method = :spherical # More precise
# 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, []]
]
end end
# define getters and setters for all configuration settings # delegates getters and setters for all configuration settings to the instance
self.options_and_defaults.each do |o,d| instance_eval(CONFIGURABLE.map do |method|
eval("def self.#{o}; @@#{o}; end") meth = method.to_s
eval("def self.#{o}=(obj); @@#{o} = obj; end") <<-EOS
end def #{meth}
instance.#{meth}
end
## def #{meth}=(value)
# Set all values to default. instance.#{meth} = value
#
def self.set_defaults
self.options_and_defaults.each do |o,d|
self.send("#{o}=", d)
end 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.
Finish editing this message first!
Please register or to comment