class ROM::Gateway
Abstract gateway class
Every adapter needs to inherit from this class and implement required interface
@abstract
@api public
Attributes
@!attribute [r] connection
@return [Object] The gateway's connection object (type varies across adapters)
Public Class Methods
Get gateway subclass for a specific adapter
@param [Symbol] type Adapter identifier
@return [Class]
@api private
# File lib/rom/gateway.rb, line 116 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
Set up a gateway
@overload setup(type, *args)
Sets up a single-gateway given a gateway type. For custom gateways, create an instance and pass it directly. @example module SuperDB class Gateway < ROM::Gateway def initialize(options) end end end ROM.register_adapter(:super_db, SuperDB) Gateway.setup(:super_db, some: 'options') # SuperDB::Gateway.new(some: 'options') is called @param [Symbol] type Registered gateway identifier @param [Array] args Additional gateway options
@overload setup(gateway)
Set up a gateway instance @example module SuperDB class Gateway < ROM::Gateway def initialize(options) end end end ROM.register_adapter(:super_db, SuperDB) Gateway.setup(SuperDB::Gateway.new(some: 'options')) @param [Gateway] gateway
@return [Gateway] a specific gateway subclass
@api public
# File lib/rom/gateway.rb, line 83 def self.setup(gateway_or_scheme, *args) case 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/master/CHANGELOG.md STRING when Symbol klass = class_from_symbol(gateway_or_scheme) if klass.instance_method(:initialize).arity.zero? klass.new else klass.new(*args) end else raise ArgumentError, "Can't accept arguments when passing an instance" unless args.empty? gateway_or_scheme end end
Public Instance Methods
Returns the adapter, defined for the class
@return [Symbol]
@api public
# File lib/rom/gateway.rb, line 135 def adapter self.class.adapter || raise( MissingAdapterIdentifierError, "gateway class +#{self}+ is missing the adapter identifier" ) end
Disconnect is optional and it's a no-op by default
@api public
# File lib/rom/gateway.rb, line 168 def disconnect # noop end
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.
@return [NilClass]
@api public
# File lib/rom/gateway.rb, line 161 def logger # noop end
Runs a block inside a transaction. The underlying transaction engine is adapter-specific
@param [Hash] opts Transaction
options
@return The result of yielding the block or nil
if
the transaction was rolled back
@api public
# File lib/rom/gateway.rb, line 181 def transaction(opts = EMPTY_HASH, &block) transaction_runner(opts).run(opts, &block) end
A generic interface for setting up a logger
This is not a required interface, it's a no-op by default
@abstract
@api public
# File lib/rom/gateway.rb, line 149 def use_logger(*) # noop end
Private Instance Methods
@api private
# File lib/rom/gateway.rb, line 188 def transaction_runner(_) Transaction::NoOp end