Skip to content

Commit

Permalink
Extract out registry
Browse files Browse the repository at this point in the history
  • Loading branch information
kddnewton committed Feb 2, 2017
1 parent e370a57 commit 83c1be1
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 36 deletions.
1 change: 1 addition & 0 deletions lib/active_record/connection_adapters/odbc_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
require 'odbc_adapter/column'
require 'odbc_adapter/column_metadata'
require 'odbc_adapter/dbms'
require 'odbc_adapter/registry'
require 'odbc_adapter/type_caster'
require 'odbc_adapter/version'

Expand Down
15 changes: 0 additions & 15 deletions lib/odbc_adapter.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,2 @@
# Requiring with this pattern to mirror ActiveRecord
require 'active_record/connection_adapters/odbc_adapter'

module ODBCAdapter
class << self
def dbms_registry
@dbms_registry ||= {
/my.*sql/i => :MySQL,
/postgres/i => :PostgreSQL
}
end

def register(pattern, superclass = Object, &block)
dbms_registry[pattern] = Class.new(superclass, &block)
end
end
end
22 changes: 1 addition & 21 deletions lib/odbc_adapter/dbms.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,31 +20,11 @@ def initialize(connection)
end

def adapter_class
return adapter unless adapter.is_a?(Symbol)
require "odbc_adapter/adapters/#{adapter.downcase}_odbc_adapter"
Adapters.const_get(:"#{adapter}ODBCAdapter")
ODBCAdapter.adapter_for(field_for(ODBC::SQL_DBMS_NAME))
end

def field_for(field)
fields[field]
end

private

# Maps a DBMS name to a symbol
# Different ODBC drivers might return different names for the same DBMS
def adapter
@adapter ||=
begin
reported = field_for(ODBC::SQL_DBMS_NAME).downcase.gsub(/\s/, '')
found =
ODBCAdapter.dbms_registry.detect do |pattern, adapter|
adapter if reported =~ pattern
end

raise ArgumentError, "ODBCAdapter: Unsupported database (#{reported})" if found.nil?
found.last
end
end
end
end
51 changes: 51 additions & 0 deletions lib/odbc_adapter/registry.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
module ODBCAdapter
class Registry
attr_reader :dbs

def initialize
@dbs = {
/my.*sql/i => :MySQL,
/postgres/i => :PostgreSQL
}
end

def adapter_for(reported_name)
reported_name = reported_name.downcase.gsub(/\s/, '')
found =
dbs.detect do |pattern, adapter|
adapter if reported_name =~ pattern
end

raise ArgumentError, "ODBCAdapter: Unsupported database (#{reported_name})" if found.nil?
normalize_adapter(found.last)
end

def register(pattern, superclass = Object, &block)
dbs[pattern] = Class.new(superclass, &block)
end

private

def normalize_adapter(adapter)
return adapter unless adapter.is_a?(Symbol)
require "odbc_adapter/adapters/#{adapter.downcase}_odbc_adapter"
Adapters.const_get(:"#{adapter}ODBCAdapter")
end
end

class << self
def adapter_for(reported_name)
registry.adapter_for(reported_name)
end

def register(pattern, superclass = Object, &block)
registry.register(pattern, superclass, &block)
end

private

def registry
@registry ||= Registry.new
end
end
end
22 changes: 22 additions & 0 deletions test/registry_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require 'test_helper'

class RegistryTest < Minitest::Test
def test_register
registry = ODBCAdapter::Registry.new

require File.join('odbc_adapter', 'adapters', 'mysql_odbc_adapter')
registry.register(/foobar/, ODBCAdapter::Adapters::MySQLODBCAdapter) do
def initialize
end

def quoted_true
'foobar'
end
end

adapter = registry.adapter_for('Foo Bar')
assert_kind_of Class, adapter
assert_equal ODBCAdapter::Adapters::MySQLODBCAdapter, adapter.superclass
assert_equal 'foobar', adapter.new.quoted_true
end
end

0 comments on commit 83c1be1

Please sign in to comment.