Class: Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/geolocal/configuration.rb

Constant Summary collapse

OPTIONS =
[ :provider, :module, :file, :tmpdir, :ipv4, :ipv6, :quiet, :countries ]

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/geolocal/configuration.rb', line 5

def initialize
  # configuration defaults
  @provider = 'Geolocal::Provider::DB_IP'
  @module = 'Geolocal'
  @file = nil       # default is computed
  @tmpdir = 'tmp/geolocal'
  @ipv4 = true
  @ipv6 = true
  @quiet = false
  @countries = {}
end

Instance Method Details

#fileObject

if not set, defaults to lib/module-name



18
19
20
# File 'lib/geolocal/configuration.rb', line 18

def file
  @file || "lib/#{module_file @module}.rb"
end

#load_providerObject



32
33
34
35
# File 'lib/geolocal/configuration.rb', line 32

def load_provider
  require_provider_file
  @provider.split('::').reduce(Module, :const_get)
end

#module_file(modname) ⇒ Object



42
43
44
# File 'lib/geolocal/configuration.rb', line 42

def module_file modname
  modname.gsub('::', '/').gsub(/([a-z])([A-Z])/, '\1_\2').downcase
end

#require_provider_fileObject



22
23
24
25
26
27
28
29
30
# File 'lib/geolocal/configuration.rb', line 22

def require_provider_file
  begin
    # normal ruby/gem load path
    Kernel.require module_file(@provider)
  rescue LoadError
    # used when running source code locally
    Kernel.require "./lib/#{module_file(@provider)}.rb"
  end
end

#to_hashObject



37
38
39
40
# File 'lib/geolocal/configuration.rb', line 37

def to_hash
  # returned keys will always be symbols
  OPTIONS.reduce({}) { |a,v| a.merge! v => self.send(v) }
end