Skip to content
Snippets Groups Projects
Commit 3e1ee32c authored by Alex Reisner's avatar Alex Reisner
Browse files

Refactor configuration to use hash

instead of instance variables.
parent e2dd011b
No related branches found
No related tags found
No related merge requests found
...@@ -54,44 +54,56 @@ module Geocoder ...@@ -54,44 +54,56 @@ module Geocoder
:distances :distances
] ]
attr_accessor *OPTIONS attr_reader :data
OPTIONS.each do |o|
define_method o do
@data[o]
end
define_method "#{o}=" do |value|
@data[o] = value
end
end
def initialize # :nodoc def initialize # :nodoc
@data = {}
set_defaults set_defaults
end end
def set_defaults def set_defaults
@timeout = 3 # geocoding service timeout (secs) @data = {
@lookup = :google # name of street address geocoding service (symbol) :timeout => 3, # geocoding service timeout (secs)
@ip_lookup = :freegeoip # name of IP address geocoding service (symbol) :lookup => :google, # name of street address geocoding service (symbol)
@language = :en # ISO-639 language code :ip_lookup => :freegeoip, # name of IP address geocoding service (symbol)
@http_headers = {} # HTTP headers for lookup :language => :en, # ISO-639 language code
@use_https = false # use HTTPS for lookup requests? (if supported) :http_headers => {}, # HTTP headers for lookup
@http_proxy = nil # HTTP proxy server (user:pass@host:port) :use_https => false, # use HTTPS for lookup requests? (if supported)
@https_proxy = nil # HTTPS proxy server (user:pass@host:port) :http_proxy => nil, # HTTP proxy server (user:pass@host:port)
@api_key = nil # API key for geocoding service :https_proxy => nil, # HTTPS proxy server (user:pass@host:port)
@cache = nil # cache object (must respond to #[], #[]=, and #keys) :api_key => nil, # API key for geocoding service
@cache_prefix = "geocoder:" # prefix (string) to use for all cache keys :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 # 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 => [],
# calculation options # calculation options
@units = :mi # :mi or :km :units => :mi, # :mi or :km
@distances = :linear # :linear or :spherical :distances => :linear # :linear or :spherical
}
end end
instance_eval(OPTIONS.map do |option| instance_eval(OPTIONS.map do |option|
o = option.to_s o = option.to_s
<<-EOS <<-EOS
def #{o} def #{o}
instance.#{o} instance.data[:#{o}]
end end
def #{o}=(value) def #{o}=(value)
instance.#{o} = value instance.data[:#{o}] = value
end end
EOS EOS
end.join("\n\n")) end.join("\n\n"))
......
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