Class: ROM::Gateway Abstract
- Inherits:
-
Object
- Object
- ROM::Gateway
- Extended by:
- Dry::Core::ClassAttributes, Notifications::Listener
- Defined in:
- lib/rom/gateway.rb
Overview
Abstract gateway class
Every adapter needs to inherit from this class and implement required interface
Direct Known Subclasses
Instance Attribute Summary collapse
- #config ⇒ Object readonly
- #connection ⇒ Object readonly
Class Method Summary collapse
-
.adapter ⇒ Object
Get or set gateway's adapter identifier.
-
.class_from_symbol(type) ⇒ Class
private
Get gateway subclass for a specific adapter.
-
.setup(gateway_or_scheme, *args) ⇒ Gateway
Set up a gateway.
-
.subscribe(event_id, query = EMPTY_HASH, &block) ⇒ Object
extended
from Notifications::Listener
Subscribe to events.
Instance Method Summary collapse
-
#adapter ⇒ Symbol
Returns the adapter, defined for the class.
-
#command(klass, relation:, **opts) ⇒ Command
Build a command instance.
-
#disconnect ⇒ Object
Disconnect is optional and it's a no-op by default.
-
#logger ⇒ NilClass
A generic interface for returning default logger.
-
#name ⇒ Symbol
Configured gateway name used in the registry.
-
#transaction(**opts, &block) ⇒ Object
Runs a block inside a transaction.
-
#use_logger ⇒ Object
abstract
A generic interface for setting up a logger.
Instance Attribute Details
#config ⇒ Object (readonly)
42 43 44 |
# File 'lib/rom/gateway.rb', line 42 def config @config end |
#connection ⇒ Object (readonly)
46 47 48 |
# File 'lib/rom/gateway.rb', line 46 def connection @connection end |
Class Method Details
.adapter ⇒ Symbol .gateway(adapter) ⇒ Object
Get or set gateway's adapter identifier
38 |
# File 'lib/rom/gateway.rb', line 38 defines :adapter |
.class_from_symbol(type) ⇒ Class
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Get gateway subclass for a specific adapter
138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/rom/gateway.rb', line 138 def self.class_from_symbol(type) adapter = ROM.adapters.fetch(type) do begin require "rom/#{type}" rescue LoadError raise AdapterLoadError, "Failed to load adapter rom/#{type}" end ROM.adapters.fetch(type) end adapter.const_get(:Gateway) end |
.setup(type, *args) ⇒ Gateway .setup(gateway) ⇒ Gateway
Set up a gateway
rubocop:disable Metrics/AbcSize, Metrics/PerceivedComplexity
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/rom/gateway.rb', line 92 def self.setup(gateway_or_scheme, *args) case gateway_or_scheme when Gateway unless args.empty? raise ArgumentError, "Can't accept arguments when passing an instance" end gateway_or_scheme when String raise ArgumentError, <<-STRING.gsub(/^ {10}/, "") URIs without an explicit scheme are not supported anymore. See https://github.com/rom-rb/rom/blob/main/CHANGELOG.md STRING when Symbol klass = class_from_symbol(gateway_or_scheme) if klass.instance_method(:initialize).arity.zero? klass.new elsif args.size.equal?(1) && args.first.respond_to?(:args) if args.first.respond_to?(:args) setup(gateway_or_scheme, *args.first.args) else klass.new(**config) end elsif args.last.is_a?(Hash) klass.new(*args[0..-2], **args.last) else klass.new(*args) end else gateway_or_scheme end end |
.subscribe(event_id, query = EMPTY_HASH, &block) ⇒ Object Originally defined in module Notifications::Listener
Subscribe to events
Instance Method Details
#adapter ⇒ Symbol
Returns the adapter, defined for the class
166 167 168 169 170 171 |
# File 'lib/rom/gateway.rb', line 166 def adapter self.class.adapter || raise( MissingAdapterIdentifierError, "gateway class +#{self}+ is missing the adapter identifier" ) end |
#command(klass, relation:, **opts) ⇒ Command
Build a command instance
221 222 223 |
# File 'lib/rom/gateway.rb', line 221 def command(klass, relation:, **opts) klass.build(relation, **opts) end |
#disconnect ⇒ Object
Disconnect is optional and it's a no-op by default
199 200 201 |
# File 'lib/rom/gateway.rb', line 199 def disconnect # noop end |
#logger ⇒ NilClass
A generic interface for returning default logger
Adapters should implement this method as handling loggers is different across adapters. This is a no-op by default and returns nil.
192 193 194 |
# File 'lib/rom/gateway.rb', line 192 def logger # noop end |
#name ⇒ Symbol
Configured gateway name used in the registry
157 158 159 |
# File 'lib/rom/gateway.rb', line 157 def name config.id end |
#transaction(**opts, &block) ⇒ Object
Runs a block inside a transaction. The underlying transaction engine is adapter-specific
212 213 214 |
# File 'lib/rom/gateway.rb', line 212 def transaction(**opts, &block) transaction_runner(**opts).run(**opts, &block) end |
#use_logger ⇒ Object
A generic interface for setting up a logger
This is not a required interface, it's a no-op by default
180 181 182 |
# File 'lib/rom/gateway.rb', line 180 def use_logger(*) # noop end |