class SequelRails::DbConfig

Constants

ADAPTER_MAPPING

Public Class Methods

new(raw, opts = {}) click to toggle source
# File lib/sequel_rails/db_config.rb, line 6
def initialize(raw, opts = {})
  merge! raw
  self[:port] = port.to_i if include? :port
  normalize_adapter if include? :adapter
  normalize_db opts[:root] if include? :database
  self[:max_connections] = pool if include? :pool
end

Public Instance Methods

method_missing(key, *a) click to toggle source

allow easier access

Calls superclass method
# File lib/sequel_rails/db_config.rb, line 15
def method_missing(key, *a)
  return self[key] if a.empty? && include?(key)
  super
end
respond_to_missing?(key, include_private = false) click to toggle source
Calls superclass method
# File lib/sequel_rails/db_config.rb, line 20
def respond_to_missing?(key, include_private = false)
  include?(key) || super
end
url() click to toggle source
# File lib/sequel_rails/db_config.rb, line 24
def url
  # the gsub transforms foo:/bar
  # (which jdbc doesn't like)
  # into foo:///bar
  self[:url] || make_url.to_s.gsub(%r{:/(?=\w)}, ':///')
end

Private Instance Methods

build_url(cfg) click to toggle source
# File lib/sequel_rails/db_config.rb, line 69
def build_url(cfg)
  if (adapter = cfg['adapter']) =~ /sqlite/ &&
     (database = cfg['database']) =~ /^:/
    # magic sqlite databases
    return URI::Generic.build(:scheme => adapter, :opaque => database)
  end

  # these are handled separately
  params = cfg.reject { |k, _| non_params.include? k }

  if (v = params['search_path'])
    # make sure there's no whitespace
    v = v.split(',').map(&:strip) unless v.respond_to? :join
    params['search_path'] = v.join(',')
  end

  path = cfg['database'].to_s
  path = "/#{path}" if path =~ %r{^(?!/)}

  q = URI.encode_www_form(params)
  q = nil if q.empty?

  URI::Generic.build(
    :scheme => cfg['adapter'],
    :host => cfg['host'],
    :port => cfg['port'],
    :path => path,
    :query => q
  )
end
jdbcify_adapter() click to toggle source
# File lib/sequel_rails/db_config.rb, line 43
def jdbcify_adapter
  return if adapter =~ /^jdbc:/
  self[:adapter] = 'postgresql' if adapter == 'postgres'
  self[:adapter] = 'jdbc:' + adapter
end
make_url() click to toggle source
# File lib/sequel_rails/db_config.rb, line 57
def make_url
  if adapter =~ /^(jdbc|do):/
    scheme, subadapter = adapter.split ':'
    URI::Generic.build(
      :scheme => scheme,
      :opaque => build_url(to_hash.merge('adapter' => subadapter)).to_s
    )
  else
    build_url to_hash
  end
end
non_params() click to toggle source
# File lib/sequel_rails/db_config.rb, line 100
def non_params
  %w(adapter host port database servers)
end
normalize_adapter() click to toggle source
# File lib/sequel_rails/db_config.rb, line 38
def normalize_adapter
  self[:adapter] = ADAPTER_MAPPING[adapter.to_s] || adapter.to_s
  jdbcify_adapter if SequelRails.jruby?
end
normalize_db(root) click to toggle source
# File lib/sequel_rails/db_config.rb, line 49
def normalize_db(root)
  return unless include? :adapter
  return unless root
  return unless adapter.include?('sqlite') && database != ':memory:'
  # sqlite expects path as the database name
  self[:database] = File.expand_path database.to_s, root
end