Class: ActiveCypher::ConnectionAdapters::Registry

Inherits:
Object
  • Object
show all
Defined in:
lib/active_cypher/connection_adapters/registry.rb

Overview

Registry: Because every adapter wants to feel special, and every ORM needs a secret society. This class is the adapter speakeasy—register your adapter, get on the list, and maybe, just maybe, you’ll get to connect to a database tonight. Under the hood, it’s just a hash, a dash of Ruby mischief, and the occasional existential dread when you realize your adapter isn’t registered.

Class Method Summary collapse

Class Method Details

.adaptersObject

The sacred scroll of adapters. Not inherited, not shared—just ours.



12
13
14
# File 'lib/active_cypher/connection_adapters/registry.rb', line 12

def adapters
  @adapters ||= {}
end

.adapters_dupHash

Get all registered adapters (for those who like to peek behind the curtain).

Returns:

  • (Hash)

    The hash of registered adapters



26
27
28
# File 'lib/active_cypher/connection_adapters/registry.rb', line 26

def adapters_dup
  adapters.dup
end

.create_driver_from_url(url, pool_size: 5, options: {}) ⇒ Bolt::Driver

Creates a Bolt driver from a connection URL, because sometimes you want to skip the foreplay and go straight to disappointment.

Parameters:

  • url (String)

    Connection URL

  • pool_size (Integer) (defaults to: 5)

    Connection pool size

  • options (Hash) (defaults to: {})

    Additional options

Returns:

  • (Bolt::Driver)

    The configured driver, or a ticket to the debugging underworld.



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/active_cypher/connection_adapters/registry.rb', line 69

def create_driver_from_url(url, pool_size: 5, options: {})
  resolver = ActiveCypher::ConnectionUrlResolver.new(url)
  config = resolver.to_hash
  return nil unless config

  adapter = create_from_config(config, options)
  return nil unless adapter

  # Always use 'bolt' scheme for driver creation, regardless of adapter
  uri = "bolt://#{config[:host]}:#{config[:port]}"
  auth_token = {
    scheme: 'basic',
    principal: config[:username],
    credentials: config[:password]
  }
  # Get SSL connection params from resolver
  ssl_params = resolver.ssl_connection_params

  ActiveCypher::Bolt::Driver.new(
    uri: uri,
    adapter: adapter,
    auth_token: auth_token,
    pool_size: pool_size,
    **ssl_params
  )
end

.create_from_config(config, options = {}) ⇒ AbstractAdapter

Conjure an adapter from a configuration hash.

Parameters:

  • config (Hash)

    Configuration hash with adapter, host, port, etc.

  • options (Hash) (defaults to: {})

    Additional options for the connection

Returns:

  • (AbstractAdapter)

    An instance of the appropriate adapter, or a cryptic error if you angered the registry spirits.

Raises:



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/active_cypher/connection_adapters/registry.rb', line 46

def create_from_config(config, options = {})
  adapter_type = config[:adapter].to_s.downcase
  adapter_class = adapters[adapter_type]
  unless adapter_class
    # Try to require the adapter file dynamically
    begin
      require "active_cypher/connection_adapters/#{adapter_type}_adapter"
    rescue LoadError
      # Ignore, will raise below
    end
    adapter_class = adapters[adapter_type]
  end
  raise ActiveCypher::ConnectionError, "No adapter registered for '#{adapter_type}'. The registry is silent (and so is require)." unless adapter_class

  full_config = config.merge(options)
  adapter_class.new(full_config)
end

.create_from_url(url, options = {}) ⇒ AbstractAdapter

Summon an adapter from a connection URL.

Parameters:

  • url (String)

    Connection URL (e.g., “neo4j://user:pass@localhost:7687”)

  • options (Hash) (defaults to: {})

    Additional options for the connection

Returns:



34
35
36
37
38
39
40
# File 'lib/active_cypher/connection_adapters/registry.rb', line 34

def create_from_url(url, options = {})
  resolver = ActiveCypher::ConnectionUrlResolver.new(url)
  config = resolver.to_hash
  return nil unless config

  create_from_config(config, options)
end

.register(adapter_type, adapter_class) ⇒ Object

Register an adapter class for a specific database type. Because every adapter wants to be chosen, but only a few make the cut.

Parameters:

  • adapter_type (String)

    The adapter type name (e.g., ‘neo4j’, ‘memgraph’)

  • adapter_class (Class)

    The adapter class to register



20
21
22
# File 'lib/active_cypher/connection_adapters/registry.rb', line 20

def register(adapter_type, adapter_class)
  adapters[adapter_type.to_s.downcase] = adapter_class
end