Class: Eql::AdapterFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/eql/adapter_factory.rb

Overview

AdapterFactory class detects adapters and their helpers

Constant Summary collapse

HELPERS_MODULE_NAME =

Returns:

  • (Symbol)
:ContextHelpers

Class Method Summary collapse

Class Method Details

.adapter_helpers(adapter) ⇒ Array<Module>

Get adapter’s helper modules

Parameters:

Returns:

  • (Array<Module>)


36
37
38
39
40
41
42
# File 'lib/eql/adapter_factory.rb', line 36

def adapter_helpers(adapter)
  adapter.class.ancestors.each_with_object([]) do |klass, ret|
    next unless klass.is_a?(Class)
    next unless klass.const_defined?(HELPERS_MODULE_NAME)
    ret.unshift klass.const_get(HELPERS_MODULE_NAME)
  end
end

.adaptersHash{Symbol => Eql::Adapters::Base}

Registered adapters

Returns:



15
16
17
# File 'lib/eql/adapter_factory.rb', line 15

def adapters
  @adapters ||= {}
end

.factory(conn) ⇒ Eql::Adapters::Base

Detect adapter class

Parameters:

  • conn (Object, nil)

    DB connection or a cursor

Returns:

Raises:

  • (RuntimeError)

    when can’t find adapter’s class



53
54
55
56
57
58
59
60
# File 'lib/eql/adapter_factory.rb', line 53

def factory(conn)
  return Eql.config.default_adapter unless conn
  adapter = adapters.values.find do |a|
    a.match?(conn) if a.is_a?(Class) && a.respond_to?(:match?)
  end
  raise "Unable to detect adapter for #{conn.inspect}" unless adapter
  adapter
end

.register_adapter(key, klass) ⇒ Object

Register a new adapter

Parameters:



25
26
27
# File 'lib/eql/adapter_factory.rb', line 25

def register_adapter(key, klass)
  adapters[key] = klass
end