From 83c1be1ea087db77d2eb4b542ef84e84b59470e5 Mon Sep 17 00:00:00 2001 From: Kevin Deisz Date: Thu, 2 Feb 2017 17:17:52 -0500 Subject: [PATCH] Extract out registry --- .../connection_adapters/odbc_adapter.rb | 1 + lib/odbc_adapter.rb | 15 ------ lib/odbc_adapter/dbms.rb | 22 +------- lib/odbc_adapter/registry.rb | 51 +++++++++++++++++++ test/registry_test.rb | 22 ++++++++ 5 files changed, 75 insertions(+), 36 deletions(-) create mode 100644 lib/odbc_adapter/registry.rb create mode 100644 test/registry_test.rb diff --git a/lib/active_record/connection_adapters/odbc_adapter.rb b/lib/active_record/connection_adapters/odbc_adapter.rb index fdf68026..60c18e45 100644 --- a/lib/active_record/connection_adapters/odbc_adapter.rb +++ b/lib/active_record/connection_adapters/odbc_adapter.rb @@ -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' diff --git a/lib/odbc_adapter.rb b/lib/odbc_adapter.rb index 3dfa3294..194fb562 100644 --- a/lib/odbc_adapter.rb +++ b/lib/odbc_adapter.rb @@ -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 diff --git a/lib/odbc_adapter/dbms.rb b/lib/odbc_adapter/dbms.rb index d5ede8c9..3df2bb42 100644 --- a/lib/odbc_adapter/dbms.rb +++ b/lib/odbc_adapter/dbms.rb @@ -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 diff --git a/lib/odbc_adapter/registry.rb b/lib/odbc_adapter/registry.rb new file mode 100644 index 00000000..39170378 --- /dev/null +++ b/lib/odbc_adapter/registry.rb @@ -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 diff --git a/test/registry_test.rb b/test/registry_test.rb new file mode 100644 index 00000000..b829fd03 --- /dev/null +++ b/test/registry_test.rb @@ -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