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
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
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