Class: ActiveCypher::ConnectionAdapters::Registry
- Inherits:
-
Object
- Object
- ActiveCypher::ConnectionAdapters::Registry
- 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
-
.adapters ⇒ Object
The sacred scroll of adapters.
-
.adapters_dup ⇒ Hash
Get all registered adapters (for those who like to peek behind the curtain).
-
.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.
-
.create_from_config(config, options = {}) ⇒ AbstractAdapter
Conjure an adapter from a configuration hash.
-
.create_from_url(url, options = {}) ⇒ AbstractAdapter
Summon an adapter from a connection URL.
-
.register(adapter_type, adapter_class) ⇒ Object
Register an adapter class for a specific database type.
Class Method Details
.adapters ⇒ Object
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_dup ⇒ Hash
Get all registered adapters (for those who like to peek behind the curtain).
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.
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, ) 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.
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, = {}) 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() adapter_class.new(full_config) end |
.create_from_url(url, options = {}) ⇒ AbstractAdapter
Summon an adapter from a connection URL.
34 35 36 37 38 39 40 |
# File 'lib/active_cypher/connection_adapters/registry.rb', line 34 def create_from_url(url, = {}) resolver = ActiveCypher::ConnectionUrlResolver.new(url) config = resolver.to_hash return nil unless config create_from_config(config, ) 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.
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 |